1
1
import _ from 'lodash' ;
2
+ import { Project } from 'app/types/index' ;
2
3
3
- function arrayIsEqual ( arr , other , deep ) {
4
+ function arrayIsEqual ( arr ?: any [ ] , other ?: any [ ] , deep ?: boolean ) : boolean {
4
5
// if the other array is a falsy value, return
5
6
if ( ! arr && ! other ) {
6
7
return true ;
@@ -18,7 +19,7 @@ function arrayIsEqual(arr, other, deep) {
18
19
return arr . every ( ( val , idx ) => valueIsEqual ( val , other [ idx ] , deep ) ) ;
19
20
}
20
21
21
- export function valueIsEqual ( value , other , deep ) {
22
+ export function valueIsEqual ( value ?: any , other ?: any , deep ?: boolean ) : boolean {
22
23
if ( value === other ) {
23
24
return true ;
24
25
} else if ( _ . isArray ( value ) || _ . isArray ( other ) ) {
@@ -33,8 +34,8 @@ export function valueIsEqual(value, other, deep) {
33
34
return false ;
34
35
}
35
36
36
- function objectMatchesSubset ( obj , other , deep ) {
37
- let k ;
37
+ function objectMatchesSubset ( obj ?: object , other ?: object , deep ?: boolean ) : boolean {
38
+ let k : string ;
38
39
39
40
if ( obj === other ) {
40
41
return true ;
@@ -61,21 +62,14 @@ function objectMatchesSubset(obj, other, deep) {
61
62
return true ;
62
63
}
63
64
64
- // XXX(dcramer): the previous mechanism of using _.map here failed
65
- // miserably if a param was named 'length'
66
- export function objectToArray ( obj ) {
67
- const result = [ ] ;
68
- for ( const key in obj ) {
69
- result . push ( [ key , obj [ key ] ] ) ;
70
- }
71
- return result ;
72
- }
65
+ // TODO: instances of objectToArray should be refactored
66
+ export const objectToArray = Object . entries ;
73
67
74
- export function intcomma ( x ) {
68
+ export function intcomma ( x : number ) : string {
75
69
return x . toString ( ) . replace ( / \B (? = ( \d { 3 } ) + (? ! \d ) ) / g, ',' ) ;
76
70
}
77
71
78
- export function sortArray ( arr , score_fn ) {
72
+ export function sortArray < T > ( arr : Array < T > , score_fn : ( entry : T ) => string ) : Array < T > {
79
73
arr . sort ( ( a , b ) => {
80
74
const a_score = score_fn ( a ) ,
81
75
b_score = score_fn ( b ) ;
@@ -94,7 +88,7 @@ export function sortArray(arr, score_fn) {
94
88
return arr ;
95
89
}
96
90
97
- export function objectIsEmpty ( obj ) {
91
+ export function objectIsEmpty ( obj : object ) : boolean {
98
92
for ( const prop in obj ) {
99
93
if ( obj . hasOwnProperty ( prop ) ) {
100
94
return false ;
@@ -104,55 +98,55 @@ export function objectIsEmpty(obj) {
104
98
return true ;
105
99
}
106
100
107
- export function trim ( str ) {
101
+ export function trim ( str : string ) : string {
108
102
return str . replace ( / ^ \s + | \s + $ / g, '' ) ;
109
103
}
110
104
111
105
/**
112
106
* Replaces slug special chars with a space
113
107
*/
114
- export function explodeSlug ( slug ) {
108
+ export function explodeSlug ( slug : string ) : string {
115
109
return trim ( slug . replace ( / [ - _ ] + / g, ' ' ) ) ;
116
110
}
117
111
118
- export function defined ( item ) {
112
+ export function defined ( item : any ) : boolean {
119
113
return ! _ . isUndefined ( item ) && item !== null ;
120
114
}
121
115
122
- export function nl2br ( str ) {
116
+ export function nl2br ( str : string ) : string {
123
117
return str . replace ( / (?: \r \n | \r | \n ) / g, '<br />' ) ;
124
118
}
125
119
126
120
/**
127
121
* This function has a critical security impact, make sure to check all usages before changing this function.
128
122
* In some parts of our code we rely on that this only really is a string starting with http(s).
129
123
*/
130
- export function isUrl ( str ) {
124
+ export function isUrl ( str : any ) : boolean {
131
125
return (
132
126
! ! str &&
133
127
_ . isString ( str ) &&
134
128
( str . indexOf ( 'http://' ) === 0 || str . indexOf ( 'https://' ) === 0 )
135
129
) ;
136
130
}
137
131
138
- export function escape ( str ) {
132
+ export function escape ( str : string ) : string {
139
133
return str
140
134
. replace ( / & / g, '&' )
141
135
. replace ( / < / g, '<' )
142
136
. replace ( / > / g, '>' ) ;
143
137
}
144
138
145
- export function percent ( value , totalValue , precise ) {
139
+ export function percent ( value : number , totalValue : number ) : number {
146
140
return ( value / totalValue ) * 100 ;
147
141
}
148
142
149
- export function toTitleCase ( str ) {
143
+ export function toTitleCase ( str : string ) : string {
150
144
return str . replace ( / \w \S * / g, txt => {
151
145
return txt . charAt ( 0 ) . toUpperCase ( ) + txt . substr ( 1 ) . toLowerCase ( ) ;
152
146
} ) ;
153
147
}
154
148
155
- export function formatBytes ( bytes ) {
149
+ export function formatBytes ( bytes : number ) : string {
156
150
const units = [ 'KB' , 'MB' , 'GB' , 'TB' , 'PB' , 'EB' , 'ZB' , 'YB' ] ;
157
151
const thresh = 1024 ;
158
152
if ( bytes < thresh ) {
@@ -167,7 +161,7 @@ export function formatBytes(bytes) {
167
161
return bytes . toFixed ( 1 ) + ' ' + units [ u ] ;
168
162
}
169
163
170
- export function getShortVersion ( version ) {
164
+ export function getShortVersion ( version : string ) : string {
171
165
if ( version . length < 12 ) {
172
166
return version ;
173
167
}
@@ -184,37 +178,37 @@ export function getShortVersion(version) {
184
178
return version ;
185
179
}
186
180
187
- export function parseRepo ( repo ) {
188
- if ( ! repo ) {
189
- return repo ;
190
- } else {
181
+ export function parseRepo < T > ( repo : T ) : T {
182
+ if ( typeof repo === 'string' ) {
191
183
const re = / (?: g i t h u b \. c o m | b i t b u c k e t \. o r g ) \/ ( [ ^ \/ ] + \/ [ ^ \/ ] + ) / i;
192
184
const match = repo . match ( re ) ;
193
185
const parsedRepo = match ? match [ 1 ] : repo ;
194
- return parsedRepo ;
186
+ return parsedRepo as any ;
195
187
}
188
+
189
+ return repo ;
196
190
}
197
191
198
192
/**
199
193
* Converts a multi-line textarea input value into an array,
200
194
* eliminating empty lines
201
195
*/
202
- export function extractMultilineFields ( value ) {
196
+ export function extractMultilineFields ( value : string ) : Array < string > {
203
197
return value
204
198
. split ( '\n' )
205
199
. map ( f => trim ( f ) )
206
200
. filter ( f => f !== '' ) ;
207
201
}
208
202
209
- function projectDisplayCompare ( a , b ) {
203
+ function projectDisplayCompare ( a : Project , b : Project ) : number {
210
204
if ( a . isBookmarked !== b . isBookmarked ) {
211
205
return a . isBookmarked ? - 1 : 1 ;
212
206
}
213
207
return a . slug . localeCompare ( b . slug ) ;
214
208
}
215
209
216
210
// Sort a list of projects by bookmarkedness, then by id
217
- export function sortProjects ( projects ) {
211
+ export function sortProjects ( projects : Array < Project > ) : Array < Project > {
218
212
return projects . sort ( projectDisplayCompare ) ;
219
213
}
220
214
@@ -225,21 +219,29 @@ export const buildTeamId = id => `team:${id}`;
225
219
/**
226
220
* Removes the organization / project scope prefix on feature names.
227
221
*/
228
- export function descopeFeatureName ( feature ) {
229
- return typeof feature . match !== 'function'
230
- ? feature
231
- : feature . match ( / (?: ^ (?: p r o j e c t s | o r g a n i z a t i o n s ) : ) ? ( .* ) / ) . pop ( ) ;
222
+ export function descopeFeatureName < T > ( feature : T ) : T | string {
223
+ if ( typeof feature !== 'string' ) {
224
+ return feature ;
225
+ }
226
+
227
+ const results = feature . match ( / (?: ^ (?: p r o j e c t s | o r g a n i z a t i o n s ) : ) ? ( .* ) / ) ;
228
+
229
+ if ( results && results . length > 0 ) {
230
+ return results . pop ( ) ! ;
231
+ }
232
+
233
+ return feature ;
232
234
}
233
235
234
- export function isWebpackChunkLoadingError ( error ) {
236
+ export function isWebpackChunkLoadingError ( error : Error ) : boolean {
235
237
return (
236
238
error &&
237
239
typeof error . message === 'string' &&
238
240
error . message . toLowerCase ( ) . includes ( 'loading chunk' )
239
241
) ;
240
242
}
241
243
242
- export function deepFreeze ( object ) {
244
+ export function deepFreeze ( object : { [ x : string ] : any } ) {
243
245
// Retrieve the property names defined on object
244
246
const propNames = Object . getOwnPropertyNames ( object ) ;
245
247
// Freeze properties before freezing self
0 commit comments