1
1
/** @module common */ /** for typedoc */
2
- import { isDefined , isFunction , isNumber , isString , isObject , isArray , isRegExp , isDate } from "./predicates" ;
2
+
3
+ import { isFunction , isString , isArray , isRegExp , isDate } from "./predicates" ;
4
+ import { all , pattern , any , not , prop , curry , val } from "./hof" ;
3
5
4
6
let angular = ( < any > window ) . angular ;
5
7
export const fromJson = angular && angular . fromJson || _fromJson ;
@@ -11,10 +13,6 @@ export const equals = angular && angular.equals || _equals;
11
13
export const identity = ( x ) => x ;
12
14
export const noop = ( ) => undefined ;
13
15
14
- export * from "./hof" ;
15
- import { all , pattern , and , any , not , prop , curry , pipe , val } from "./hof" ;
16
- export { isDefined , isFunction , isNumber , isString , isObject , isArray } ;
17
-
18
16
type Mapper < X , T > = ( x : X , key ?: ( string | number ) ) => T ;
19
17
export interface TypedMap < T > { [ key : string ] : T ; }
20
18
export type Predicate < X > = ( X ) => boolean ;
@@ -87,7 +85,8 @@ export function bindFunctions(from, to, bindTo, fnNames: string[] = Object.keys(
87
85
* prototypal inheritance helper.
88
86
* Creates a new object which has `parent` object as its prototype, and then copies the properties from `extra` onto it
89
87
*/
90
- export const inherit = ( parent , extra ) => extend ( new ( extend ( function ( ) { } , { prototype : parent } ) ) ( ) , extra ) ;
88
+ export const inherit = ( parent , extra ) =>
89
+ extend ( new ( extend ( function ( ) { } , { prototype : parent } ) ) ( ) , extra ) ;
91
90
92
91
/**
93
92
* Given an arguments object, converts the arguments at index idx and above to an array.
@@ -401,9 +400,9 @@ export const flatten = (arr: any[]) => arr.reduce(flattenR, []);
401
400
* oneString.filter(assertPredicate(isNumber, "Not all numbers")); // throws Error(""Not all numbers"");
402
401
* ```
403
402
*/
404
- export function assertPredicate < T > ( fn : Predicate < T > , errMsg : string = "assert failure" ) : Predicate < T > {
403
+ export function assertPredicate < T > ( fn : Predicate < T > , errMsg : ( string | Function ) = "assert failure" ) : Predicate < T > {
405
404
return ( obj : T ) => {
406
- if ( ! fn ( obj ) ) throw new Error ( errMsg ) ;
405
+ if ( ! fn ( obj ) ) throw new Error ( isFunction ( errMsg ) ? ( < Function > errMsg ) ( obj ) : errMsg ) ;
407
406
return true ;
408
407
} ;
409
408
}
@@ -467,29 +466,6 @@ export function applyPairs(memo: TypedMap<any>, keyValTuple: any[]) {
467
466
return memo ;
468
467
}
469
468
470
- /**
471
- * Predicate which checks if a value is injectable
472
- *
473
- * A value is "injectable" if it is a function, or if it is an ng1 array-notation-style array
474
- * where all the elements in the array are Strings, except the last one, which is a Function
475
- */
476
- export function isInjectable ( val ) {
477
- if ( isArray ( val ) && val . length ) {
478
- let head = val . slice ( 0 , - 1 ) , tail = val . slice ( - 1 ) ;
479
- return ! ( head . filter ( not ( isString ) ) . length || tail . filter ( not ( isFunction ) ) . length ) ;
480
- }
481
- return isFunction ( val ) ;
482
- }
483
- /** Predicate which checks if a value is `=== null` */
484
- export const isNull = o => o === null ;
485
-
486
- /**
487
- * Predicate which checks if a value looks like a Promise
488
- *
489
- * It is probably a Promise if it's an object, and it has a `then` property which is a Function
490
- */
491
- export const isPromise = and ( isObject , pipe ( prop ( 'then' ) , isFunction ) ) ;
492
-
493
469
export function fnToString ( fn : IInjectable ) {
494
470
let _fn = pattern ( [
495
471
[ isArray , arr => arr . slice ( - 1 ) [ 0 ] ] ,
@@ -559,8 +535,11 @@ function _forEach(obj: (any[]|any), cb, _this) {
559
535
Object . keys ( obj ) . forEach ( key => cb ( obj [ key ] , key ) ) ;
560
536
}
561
537
562
- function _extend ( to , from ) {
563
- return ! from ? to : Object . keys ( from ) . reduce ( ( m , key ) => { m [ key ] = from [ key ] ; return m ; } , to ) ;
538
+ function _copyProps ( to , from ) { Object . keys ( from ) . forEach ( key => to [ key ] = from [ key ] ) ; return to ; }
539
+ function _extend ( toObj , fromObj ) ;
540
+ function _extend ( toObj , ...fromObj ) ;
541
+ function _extend ( toObj , rest ) {
542
+ return restArgs ( arguments , 1 ) . filter ( identity ) . reduce ( _copyProps , toObj ) ;
564
543
}
565
544
566
545
function _equals ( o1 , o2 ) {
0 commit comments