Skip to content

Commit f3acd2b

Browse files
refactor(common): Reorganize helper functions
1 parent 7cf5cd4 commit f3acd2b

File tree

3 files changed

+42
-39
lines changed

3 files changed

+42
-39
lines changed

src/common/common.ts

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/** @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";
35

46
let angular = (<any> window).angular;
57
export const fromJson = angular && angular.fromJson || _fromJson;
@@ -11,10 +13,6 @@ export const equals = angular && angular.equals || _equals;
1113
export const identity = (x) => x;
1214
export const noop = () => undefined;
1315

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-
1816
type Mapper<X, T> = (x: X, key?: (string|number)) => T;
1917
export interface TypedMap<T> { [key: string]: T; }
2018
export type Predicate<X> = (X) => boolean;
@@ -87,7 +85,8 @@ export function bindFunctions(from, to, bindTo, fnNames: string[] = Object.keys(
8785
* prototypal inheritance helper.
8886
* Creates a new object which has `parent` object as its prototype, and then copies the properties from `extra` onto it
8987
*/
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);
9190

9291
/**
9392
* 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, []);
401400
* oneString.filter(assertPredicate(isNumber, "Not all numbers")); // throws Error(""Not all numbers"");
402401
* ```
403402
*/
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> {
405404
return (obj: T) => {
406-
if (!fn(obj)) throw new Error(errMsg);
405+
if (!fn(obj)) throw new Error(isFunction(errMsg) ? (<Function> errMsg)(obj) : errMsg);
407406
return true;
408407
};
409408
}
@@ -467,29 +466,6 @@ export function applyPairs(memo: TypedMap<any>, keyValTuple: any[]) {
467466
return memo;
468467
}
469468

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-
493469
export function fnToString(fn: IInjectable) {
494470
let _fn = pattern([
495471
[isArray, arr => arr.slice(-1)[0]],
@@ -559,8 +535,11 @@ function _forEach(obj: (any[]|any), cb, _this) {
559535
Object.keys(obj).forEach(key => cb(obj[key], key));
560536
}
561537

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);
564543
}
565544

566545
function _equals(o1, o2) {

src/common/predicates.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
/** Predicates @module common */
2+
import {and, not, pipe, prop} from "./hof";
23

3-
/** reimplementation of common.not */
4-
const not = (fn) => (x) => !fn(x);
4+
const toStr = Object.prototype.toString;
55
const tis = (t) => (x) => typeof(x) === t;
6-
export const isDefined = not(tis('undefined'));
6+
export const isUndefined = tis('undefined');
7+
export const isDefined = not(isUndefined);
8+
export const isNull = o => o === null;
79
export const isFunction = tis('function');
810
export const isNumber = tis('number');
911
export const isString = tis('string');
1012
export const isObject = (x) => x !== null && typeof x === 'object';
1113
export const isArray = Array.isArray;
12-
13-
let toStr = Object.prototype.toString;
1414
export const isDate = (x) => toStr.call(x) === '[object Date]';
1515
export const isRegExp = (x) => toStr.call(x) === '[object RegExp]';
16+
17+
/**
18+
* Predicate which checks if a value is injectable
19+
*
20+
* A value is "injectable" if it is a function, or if it is an ng1 array-notation-style array
21+
* where all the elements in the array are Strings, except the last one, which is a Function
22+
*/
23+
export function isInjectable(val) {
24+
if (isArray(val) && val.length) {
25+
let head = val.slice(0, -1), tail = val.slice(-1);
26+
return !(head.filter(not(isString)).length || tail.filter(not(isFunction)).length);
27+
}
28+
return isFunction(val);
29+
}
30+
31+
/**
32+
* Predicate which checks if a value looks like a Promise
33+
*
34+
* It is probably a Promise if it's an object, and it has a `then` property which is a Function
35+
*/
36+
export const isPromise = and(isObject, pipe(prop('then'), isFunction));
37+

src/common/trace.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/** @module common */ /** for typedoc */
2-
import {isNull, isPromise, isNumber, fnToString, maxLength, padString, isInjectable, is, invoke, not, val, pattern, parse, isDefined, identity} from "../common/common";
2+
import {fnToString, maxLength, padString, identity} from "../common/common";
3+
import {is, invoke, not, val, pattern, parse} from "../common/hof";
4+
import {isNull, isPromise, isNumber, isInjectable, isDefined} from "../common/predicates";
35
import {Resolvable} from "../resolve/resolvable";
46
import {Transition} from "../transition/transition";
57
import {TransitionRejection} from "../transition/rejectFactory";

0 commit comments

Comments
 (0)