export type JSError = Error export const MAX_SAFE_INTEGER = 9007199254740991 export function is(x: any, cls: any): boolean { const p = cls.prototype x = !!x ? Object.getPrototypeOf(x) : null while (true) { if (x === null) { return false } else if (x === p) { return true } x = Object.getPrototypeOf(x) } } export function noop(): void { /* no-op */ } export function identity(x: T): T { return x } export function disableNoUnusedWarning(a?: any, b?: any, c?: any, d?: any, e?: any) {} // tslint:disable-next-line:ban-types export function isFunction(x: any): x is Function { return !!(x && x.constructor && x.call && x.apply) } export function isNaN(x: any): boolean { return x !== x } export function isNumber(x: any): x is number { return typeof x === "number" && !isNaN(x) } export const isInteger: (x: any) => x is number = (Number.isInteger as any) || ((x: any): boolean => { return isNumber(x) && x !== Infinity && x !== -Infinity && Math.floor(x) === x }) // tslint:disable-next-line:array-type export function isArray(x: any): x is Array { return !!x && x.constructor === Array } // tslint:disable-next-line:ban-types export function isObject(x: any): x is Object { if (!!x && typeof x === "object") { const proto = Object.getPrototypeOf(x) return proto === Object.prototype || proto === null } else { return false } } export function slice(arr: T[]): T[] { return arr.slice() } export function every(pred: (x: T) => boolean, xs: T[]): boolean { let n = xs.length while (n--) { if (!pred(xs[n])) return false } return true } export function find(pred: (x: T) => boolean, xs: T[]): T | undefined { for (let i = 0, n = xs.length; i < n; i++) { if (pred(xs[i])) return xs[i] } return undefined } export function findIndex(pred: (x: T) => boolean, xs: T[]): number { for (let i = 0, n = xs.length; i < n; i++) { if (pred(xs[i])) return i } return -1 } export function constantly(x: T): () => T { return () => x } export function pipe(a: A, a2b: (a: A) => B): B export function pipe(a: A, a2b: (a: A) => B, b2c: (b: B) => C): C export function pipe(a: A, a2b: (a: A) => B, b2c: (b: B) => C, c2d: (c: C) => D): D export function pipe( a: A, a2b: (a: A) => B, b2c: (b: B) => C, c2d: (c: C) => D, d2e: (d: D) => E, ): E export function pipe( a: A, a2b: (a: A) => B, b2c: (b: B) => C, c2d: (c: C) => D, d2e: (d: D) => E, e2f: (e: E) => F, ): F export function pipe( a: A, a2b: (a: A) => B, b2c: (b: B) => C, c2d: (c: C) => D, d2e: (d: D) => E, e2f: (e: E) => F, f2g: (f: F) => G, ): G export function pipe( a: A, a2b: (a: A) => B, b2c: (b: B) => C, c2d: (c: C) => D, d2e: (d: D) => E, e2f: (e: E) => F, f2g: (f: F) => G, g2h: (g: G) => H, ): H export function pipe( a: A, a2b: (a: A) => B, b2c: (b: B) => C, c2d: (c: C) => D, d2e: (d: D) => E, e2f: (e: E) => F, f2g: (f: F) => G, g2h: (g: G) => H, h2i: (h: H) => I, ): I export function pipe( a: A, a2b: (a: A) => B, b2c: (b: B) => C, c2d: (c: C) => D, d2e: (d: D) => E, e2f: (e: E) => F, f2g: (f: F) => G, g2h: (g: G) => H, h2i: (h: H) => I, i2j: (i: I) => J, ): J export function pipe( a: A, a2b: (a: A) => B, b2c: (b: B) => C, c2d: (c: C) => D, d2e: (d: D) => E, e2f: (e: E) => F, f2g: (f: F) => G, g2h: (g: G) => H, h2i: (h: H) => I, i2j: (i: I) => J, j2k: (j: J) => K, ): K export function pipe(x: any, ...fns: any[]): any { for (let i = 0, n = fns.length; i < n; i++) { x = fns[i](x) } return x } interface CF2 { (t1: T1): (t2: T2) => R (t1: T1, t2: T2): R } interface CF3 { (t1: T1): CF2 (t1: T1, t2: T2): (t3: T3) => R (t1: T1, t2: T2, t3: T3): R } // tslint:disable:no-shadowed-variable export function curry2(f: (t1: any, t2: any) => any): CF2 { function c2(t1: any, t2: any): any { switch (arguments.length) { case 0: case 1: return (t2: any): any => { return f(t1, t2) } default: return f(t1, t2) } } return c2 as any } export function curry3(f: (t1: any, t2: any, t3: any) => any): CF3 { function c3(t1: any, t2: any, t3: any): any { switch (arguments.length) { case 0: case 1: return curry2( (t2: any, t3: any): any => { return f(t1, t2, t3) }, ) case 2: return (t3: any): any => { return f(t1, t2, t3) } default: return f(t1, t2, t3) } } return c3 as any }