7
7
import { isFunction , isString , isArray , isRegExp , isDate } from "./predicates" ;
8
8
import { all , any , not , prop , curry } from "./hof" ;
9
9
import { services } from "./coreservices" ;
10
+ import { State } from "../state/stateObject" ;
10
11
11
12
let w : any = typeof window === 'undefined' ? { } : window ;
12
13
let angular = w . angular || { } ;
@@ -16,15 +17,17 @@ export const copy = angular.copy || _copy;
16
17
export const forEach = angular . forEach || _forEach ;
17
18
export const extend = angular . extend || _extend ;
18
19
export const equals = angular . equals || _equals ;
19
- export const identity = ( x ) => x ;
20
- export const noop = ( ) => undefined ;
20
+ export const identity = ( x : any ) => x ;
21
+ export const noop = ( ) => < any > undefined ;
21
22
22
23
export type Mapper < X , T > = ( x : X , key ?: ( string | number ) ) => T ;
23
24
export interface TypedMap < T > { [ key : string ] : T ; }
24
- export type Predicate < X > = ( X ) => boolean ;
25
+ export type Predicate < X > = ( x : X ) => boolean ;
25
26
export type IInjectable = ( Function | any [ ] ) ;
26
27
27
- export var abstractKey = 'abstract' ;
28
+ export interface Obj extends Object {
29
+ [ key : string ] : any
30
+ }
28
31
29
32
/**
30
33
* Binds and copies functions onto an object
@@ -81,7 +84,7 @@ export var abstractKey = 'abstract';
81
84
* @param bindTo The object which the functions will be bound to
82
85
* @param fnNames The function names which will be bound (Defaults to all the functions found on the 'from' object)
83
86
*/
84
- export function bindFunctions ( from , to , bindTo , fnNames : string [ ] = Object . keys ( from ) ) {
87
+ export function bindFunctions ( from : Obj , to : Obj , bindTo : Obj , fnNames : string [ ] = Object . keys ( from ) ) {
85
88
return fnNames . filter ( name => typeof from [ name ] === 'function' )
86
89
. forEach ( name => to [ name ] = from [ name ] . bind ( bindTo ) ) ;
87
90
}
@@ -91,7 +94,7 @@ export function bindFunctions(from, to, bindTo, fnNames: string[] = Object.keys(
91
94
* prototypal inheritance helper.
92
95
* Creates a new object which has `parent` object as its prototype, and then copies the properties from `extra` onto it
93
96
*/
94
- export const inherit = ( parent , extra ) =>
97
+ export const inherit = ( parent : Obj , extra : Obj ) =>
95
98
extend ( new ( extend ( function ( ) { } , { prototype : parent } ) ) ( ) , extra ) ;
96
99
97
100
/**
@@ -117,13 +120,15 @@ export const inherit = (parent, extra) =>
117
120
* pick(obj, "foo", "bar"); // returns { foo: 1, bar: 2 }
118
121
* pick(obj, ["foo", "bar"]); // returns { foo: 1, bar: 2 }
119
122
*/
120
- const restArgs = ( args , idx = 0 ) => Array . prototype . concat . apply ( Array . prototype , Array . prototype . slice . call ( args , idx ) ) ;
123
+ const restArgs = ( args : IArguments , idx = 0 ) =>
124
+ Array . prototype . concat . apply ( Array . prototype , Array . prototype . slice . call ( args , idx ) ) ;
121
125
122
126
/** Given an array, returns true if the object is found in the array, (using indexOf) */
123
- export const inArray = ( array : any [ ] , obj : any ) => array . indexOf ( obj ) !== - 1 ;
127
+ export const inArray = ( array : any [ ] , obj : any ) =>
128
+ array . indexOf ( obj ) !== - 1 ;
124
129
125
130
/** Given an array, and an item, if the item is found in the array, it removes it (in-place). The same array is returned */
126
- export const removeFrom = curry ( ( array : any [ ] , obj ) => {
131
+ export const removeFrom = curry ( ( array : any [ ] , obj : any ) => {
127
132
let idx = array . indexOf ( obj ) ;
128
133
if ( idx >= 0 ) array . splice ( idx , 1 ) ;
129
134
return array ;
@@ -134,7 +139,7 @@ export const removeFrom = curry((array: any[], obj) => {
134
139
* to only those properties of the objects in the defaultsList.
135
140
* Earlier objects in the defaultsList take precedence when applying defaults.
136
141
*/
137
- export function defaults ( opts = { } , ...defaultsList ) {
142
+ export function defaults ( opts = { } , ...defaultsList : Obj [ ] ) {
138
143
let defaults = merge . apply ( null , [ { } ] . concat ( defaultsList ) ) ;
139
144
return extend ( { } , defaults , pick ( opts || { } , Object . keys ( defaults ) ) ) ;
140
145
}
@@ -143,17 +148,17 @@ export function defaults(opts = {}, ...defaultsList) {
143
148
* Merges properties from the list of objects to the destination object.
144
149
* If a property already exists in the destination object, then it is not overwritten.
145
150
*/
146
- export function merge ( dst , ...objs : Object [ ] ) {
147
- forEach ( objs , function ( obj ) {
148
- forEach ( obj , function ( value , key ) {
151
+ export function merge ( dst : Obj , ...objs : Obj [ ] ) {
152
+ forEach ( objs , function ( obj : Obj ) {
153
+ forEach ( obj , function ( value : any , key : string ) {
149
154
if ( ! dst . hasOwnProperty ( key ) ) dst [ key ] = value ;
150
155
} ) ;
151
156
} ) ;
152
157
return dst ;
153
158
}
154
159
155
160
/** Reduce function that merges each element of the list into a single object, using extend */
156
- export const mergeR = ( memo , item ) => extend ( memo , item ) ;
161
+ export const mergeR = ( memo : Obj , item : Obj ) => extend ( memo , item ) ;
157
162
158
163
/**
159
164
* Finds the common ancestor path between two states.
@@ -162,8 +167,8 @@ export const mergeR = (memo, item) => extend(memo, item);
162
167
* @param {Object } second The second state.
163
168
* @return {Array } Returns an array of state names in descending order, not including the root.
164
169
*/
165
- export function ancestors ( first , second ) {
166
- let path = [ ] ;
170
+ export function ancestors ( first : State , second : State ) {
171
+ let path : State [ ] = [ ] ;
167
172
168
173
for ( var n in first . path ) {
169
174
if ( first . path [ n ] !== second . path [ n ] ) break ;
@@ -181,18 +186,18 @@ export function ancestors(first, second) {
181
186
* it defaults to the list of keys in `a`.
182
187
* @return {Boolean } Returns `true` if the keys match, otherwise `false`.
183
188
*/
184
- export function equalForKeys ( a , b , keys : string [ ] = Object . keys ( a ) ) {
189
+ export function equalForKeys ( a : Obj , b : Obj , keys : string [ ] = Object . keys ( a ) ) {
185
190
for ( var i = 0 ; i < keys . length ; i ++ ) {
186
191
let k = keys [ i ] ;
187
192
if ( a [ k ] != b [ k ] ) return false ; // Not '===', values aren't necessarily normalized
188
193
}
189
194
return true ;
190
195
}
191
196
192
- type PickOmitPredicate = ( keys : string [ ] , key ) => boolean ;
193
- function pickOmitImpl ( predicate : PickOmitPredicate , obj ) {
194
- let objCopy = { } , keys = restArgs ( arguments , 2 ) ;
195
- for ( var key in obj ) {
197
+ type PickOmitPredicate = ( keys : string [ ] , key : string ) => boolean ;
198
+ function pickOmitImpl ( predicate : PickOmitPredicate , obj : Obj , ... keys : string [ ] ) {
199
+ let objCopy = { } ;
200
+ for ( let key in obj ) {
196
201
if ( predicate ( keys , key ) ) objCopy [ key ] = obj [ key ] ;
197
202
}
198
203
return objCopy ;
@@ -208,7 +213,7 @@ function pickOmitImpl(predicate: PickOmitPredicate, obj) {
208
213
* @param obj the source object
209
214
* @param propNames an Array of strings, which are the whitelisted property names
210
215
*/
211
- export function pick ( obj , propNames : string [ ] ) : Object ;
216
+ export function pick ( obj : Obj , propNames : string [ ] ) : Obj ;
212
217
/**
213
218
* @example
214
219
* ```
@@ -219,9 +224,11 @@ export function pick(obj, propNames: string[]): Object;
219
224
* @param obj the source object
220
225
* @param propNames 1..n strings, which are the whitelisted property names
221
226
*/
222
- export function pick ( obj , ...propNames : string [ ] ) : Object ;
227
+ export function pick ( obj : Obj , ...propNames : string [ ] ) : Obj ;
223
228
/** Return a copy of the object only containing the whitelisted properties. */
224
- export function pick ( obj ) { return pickOmitImpl . apply ( null , [ inArray ] . concat ( restArgs ( arguments ) ) ) ; }
229
+ export function pick ( obj : Obj ) {
230
+ return pickOmitImpl . apply ( null , [ inArray ] . concat ( restArgs ( arguments ) ) ) ;
231
+ }
225
232
226
233
/**
227
234
* @example
@@ -233,7 +240,7 @@ export function pick(obj) { return pickOmitImpl.apply(null, [inArray].concat(res
233
240
* @param obj the source object
234
241
* @param propNames an Array of strings, which are the blacklisted property names
235
242
*/
236
- export function omit ( obj , propNames : string [ ] ) : Object ;
243
+ export function omit ( obj : Obj , propNames : string [ ] ) : Obj ;
237
244
/**
238
245
* @example
239
246
* ```
@@ -244,29 +251,32 @@ export function omit(obj, propNames: string[]): Object;
244
251
* @param obj the source object
245
252
* @param propNames 1..n strings, which are the blacklisted property names
246
253
*/
247
- export function omit ( obj , ...propNames : string [ ] ) : Object ;
254
+ export function omit ( obj : Obj , ...propNames : string [ ] ) : Obj ;
248
255
/** Return a copy of the object omitting the blacklisted properties. */
249
- export function omit ( obj ) { return pickOmitImpl . apply ( null , [ not ( inArray ) ] . concat ( restArgs ( arguments ) ) ) ; }
256
+ export function omit ( obj : Obj ) {
257
+ let notInArray = ( array , item ) => ! inArray ( array , item ) ;
258
+ return pickOmitImpl . apply ( null , [ notInArray ] . concat ( restArgs ( arguments ) ) ) ;
259
+ }
250
260
251
261
252
262
/** Given an array of objects, maps each element to a named property of the element. */
253
- export function pluck ( collection : any [ ] , propName : string ) : any [ ] ;
263
+ export function pluck ( collection : Obj [ ] , propName : string ) : Obj [ ] ;
254
264
/** Given an object, maps each property of the object to a named property of the property. */
255
265
export function pluck ( collection : { [ key : string ] : any } , propName : string ) : { [ key : string ] : any } ;
256
266
/**
257
267
* Maps an array, or object to a property (by name)
258
268
*/
259
- export function pluck ( collection , propName ) : any {
269
+ export function pluck ( collection : any , propName : string ) : any {
260
270
return map ( collection , < Mapper < any , string > > prop ( propName ) ) ;
261
271
}
262
272
263
273
264
274
/** Given an array of objects, returns a new array containing only the elements which passed the callback predicate */
265
- export function filter < T > ( collection : T [ ] , callback : ( T , key ?) => boolean ) : T [ ] ;
275
+ export function filter < T > ( collection : T [ ] , callback : ( t : T , key ?: number ) => boolean ) : T [ ] ;
266
276
/** Given an object, returns a new object with only those properties that passed the callback predicate */
267
- export function filter < T > ( collection : TypedMap < T > , callback : ( T , key ?) => boolean ) : TypedMap < T > ;
277
+ export function filter < T > ( collection : TypedMap < T > , callback : ( t : T , key ?: string ) => boolean ) : TypedMap < T > ;
268
278
/** Filters an Array or an Object's properties based on a predicate */
269
- export function filter < T > ( collection : T , callback : Function ) : T {
279
+ export function filter < T > ( collection : any , callback : Function ) : T {
270
280
let arr = isArray ( collection ) , result : any = arr ? [ ] : { } ;
271
281
let accept = arr ? x => result . push ( x ) : ( x , key ) => result [ key ] = x ;
272
282
forEach ( collection , function ( item , i ) {
@@ -281,7 +291,7 @@ export function find<T>(collection: TypedMap<T>, callback: Predicate<T>): T;
281
291
/** Given an array of objects, returns the first object which passed the callback predicate */
282
292
export function find < T > ( collection : T [ ] , callback : Predicate < T > ) : T ;
283
293
/** Finds an object from an array, or a property of an object, that matches a predicate */
284
- export function find ( collection , callback ) {
294
+ export function find ( collection : any , callback : any ) {
285
295
let result ;
286
296
287
297
forEach ( collection , function ( item , i ) {
@@ -314,7 +324,8 @@ export function map(collection: any, callback: any): any {
314
324
* let vals = values(foo); // [ 1, 2, 3 ]
315
325
* ```
316
326
*/
317
- export const values : ( < T > ( obj : TypedMap < T > ) => T [ ] ) = ( obj ) => Object . keys ( obj ) . map ( key => obj [ key ] ) ;
327
+ export const values : ( < T > ( obj : TypedMap < T > ) => T [ ] ) = ( obj : Obj ) =>
328
+ Object . keys ( obj ) . map ( key => obj [ key ] ) ;
318
329
319
330
/**
320
331
* Reduce function that returns true if all of the values are truthy.
@@ -329,7 +340,7 @@ export const values: (<T> (obj: TypedMap<T>) => T[]) = (obj) => Object.keys(obj)
329
340
* vals.reduce(allTrueR, true); // false
330
341
* ```
331
342
*/
332
- export const allTrueR = ( memo : boolean , elem ) => memo && elem ;
343
+ export const allTrueR = ( memo : boolean , elem : any ) => memo && elem ;
333
344
334
345
/**
335
346
* Reduce function that returns true if any of the values are truthy.
@@ -344,7 +355,7 @@ export const allTrueR = (memo: boolean, elem) => memo && elem;
344
355
* vals.reduce(anyTrueR, true); // true
345
356
* ```
346
357
*/
347
- export const anyTrueR = ( memo : boolean , elem ) => memo || elem ;
358
+ export const anyTrueR = ( memo : boolean , elem : any ) => memo || elem ;
348
359
349
360
/**
350
361
* Reduce function which un-nests a single level of arrays
@@ -355,7 +366,7 @@ export const anyTrueR = (memo: boolean, elem) => memo || elem;
355
366
* input.reduce(unnestR, []) // [ "a", "b", "c", "d", [ "double, "nested" ] ]
356
367
* ```
357
368
*/
358
- export const unnestR = ( memo : any [ ] , elem ) => memo . concat ( elem ) ;
369
+ export const unnestR = ( memo : any [ ] , elem : any [ ] ) => memo . concat ( elem ) ;
359
370
360
371
/**
361
372
* Reduce function which recursively un-nests all arrays
@@ -367,12 +378,18 @@ export const unnestR = (memo: any[], elem) => memo.concat(elem);
367
378
* input.reduce(unnestR, []) // [ "a", "b", "c", "d", "double, "nested" ]
368
379
* ```
369
380
*/
370
- export const flattenR = ( memo : any [ ] , elem ) => isArray ( elem ) ? memo . concat ( elem . reduce ( flattenR , [ ] ) ) : pushR ( memo , elem ) ;
381
+ export const flattenR = ( memo : any [ ] , elem : any ) =>
382
+ isArray ( elem ) ? memo . concat ( elem . reduce ( flattenR , [ ] ) ) : pushR ( memo , elem ) ;
383
+
371
384
/** Reduce function that pushes an object to an array, then returns the array. Mostly just for [[flattenR]] */
372
- export function pushR ( arr : any [ ] , obj ) { arr . push ( obj ) ; return arr ; }
385
+ export function pushR ( arr : any [ ] , obj : any ) {
386
+ arr . push ( obj ) ;
387
+ return arr ;
388
+ }
373
389
374
390
/** Reduce function that filters out duplicates */
375
- export const uniqR = ( acc , token ) => inArray ( acc , token ) ? acc : pushR ( acc , token ) ;
391
+ export const uniqR = ( acc : any [ ] , token : any ) =>
392
+ inArray ( acc , token ) ? acc : pushR ( acc , token ) ;
376
393
377
394
/**
378
395
* Return a new array with a single level of arrays unnested.
@@ -428,7 +445,8 @@ export function assertPredicate<T>(predicate: Predicate<T>, errMsg: (string|Func
428
445
* pairs({ foo: "FOO", bar: "BAR }) // [ [ "foo", "FOO" ], [ "bar": "BAR" ] ]
429
446
* ```
430
447
*/
431
- export const pairs = ( object ) => Object . keys ( object ) . map ( key => [ key , object [ key ] ] ) ;
448
+ export const pairs = ( obj : Obj ) =>
449
+ Object . keys ( obj ) . map ( key => [ key , obj [ key ] ] ) ;
432
450
433
451
/**
434
452
* Given two or more parallel arrays, returns an array of tuples where
@@ -471,7 +489,7 @@ export function arrayTuples(...arrayArgs: any[]): any[] {
471
489
* ```
472
490
*/
473
491
export function applyPairs ( memo : TypedMap < any > , keyValTuple : any [ ] ) {
474
- let key , value ;
492
+ let key : string , value : any ;
475
493
if ( isArray ( keyValTuple ) ) [ key , value ] = keyValTuple ;
476
494
if ( ! isString ( key ) ) throw new Error ( "invalid parameters to applyPairs" ) ;
477
495
memo [ key ] = value ;
@@ -489,25 +507,29 @@ export function tail<T>(arr: T[]): T {
489
507
* note: This is a shallow copy, while angular.copy is a deep copy.
490
508
* ui-router uses `copy` only to make copies of state parameters.
491
509
*/
492
- function _copy ( src , dest ) {
510
+ function _copy ( src : Obj , dest : Obj ) {
493
511
if ( dest ) Object . keys ( dest ) . forEach ( key => delete dest [ key ] ) ;
494
512
if ( ! dest ) dest = { } ;
495
513
return extend ( dest , src ) ;
496
514
}
497
515
498
- function _forEach ( obj : ( any [ ] | any ) , cb , _this ) {
516
+ /** Naive forEach implementation works with Objects or Arrays */
517
+ function _forEach ( obj : ( any [ ] | any ) , cb : Function , _this : Obj ) {
499
518
if ( isArray ( obj ) ) return obj . forEach ( cb , _this ) ;
500
519
Object . keys ( obj ) . forEach ( key => cb ( obj [ key ] , key ) ) ;
501
520
}
502
521
503
- function _copyProps ( to , from ) { Object . keys ( from ) . forEach ( key => to [ key ] = from [ key ] ) ; return to ; }
504
- function _extend ( toObj , fromObj ) ;
505
- function _extend ( toObj , ...fromObj ) ;
506
- function _extend ( toObj , rest ) {
522
+ function _copyProps ( to : Obj , from : Obj ) {
523
+ Object . keys ( from ) . forEach ( key => to [ key ] = from [ key ] ) ;
524
+ return to ;
525
+ }
526
+ function _extend ( toObj : Obj , fromObj : Obj ) : Obj ;
527
+ function _extend ( toObj : Obj , ...fromObj : Obj [ ] ) : Obj ;
528
+ function _extend ( toObj : Obj ) {
507
529
return restArgs ( arguments , 1 ) . filter ( identity ) . reduce ( _copyProps , toObj ) ;
508
530
}
509
531
510
- function _equals ( o1 , o2 ) {
532
+ function _equals ( o1 : any , o2 : any ) : boolean {
511
533
if ( o1 === o2 ) return true ;
512
534
if ( o1 === null || o2 === null ) return false ;
513
535
if ( o1 !== o1 && o2 !== o2 ) return true ; // NaN === NaN
@@ -523,7 +545,7 @@ function _equals(o1, o2) {
523
545
let predicates = [ isFunction , isArray , isDate , isRegExp ] ;
524
546
if ( predicates . map ( any ) . reduce ( ( b , fn ) => b || ! ! fn ( tup ) , false ) ) return false ;
525
547
526
- let key , keys = { } ;
548
+ let key : string , keys : { [ i : string ] : boolean } = { } ;
527
549
for ( key in o1 ) {
528
550
if ( ! _equals ( o1 [ key ] , o2 [ key ] ) ) return false ;
529
551
keys [ key ] = true ;
@@ -535,7 +557,7 @@ function _equals(o1, o2) {
535
557
return true ;
536
558
}
537
559
538
- function _arraysEq ( a1 , a2 ) {
560
+ function _arraysEq ( a1 : any [ ] , a2 : any [ ] ) {
539
561
if ( a1 . length !== a2 . length ) return false ;
540
562
return arrayTuples ( a1 , a2 ) . reduce ( ( b , t ) => b && _equals ( t [ 0 ] , t [ 1 ] ) , true ) ;
541
563
}
0 commit comments