import * as Type from './Type'; const noop: (...args: any[]) => void = () => { }; const noarg: (f: () => T) => (...args: any[]) => void = (f) => () => f(); /** Compose a unary function with an n-ary function */ const compose = (fa: (v: U) => V, fb: (...x: T) => U): (...x: T) => V => { return (...args: T) => { return fa(fb.apply(null, args)); }; }; /** Compose two unary functions. Similar to compose, but avoids using Function.prototype.apply. */ const compose1 = (fbc: (b: B) => C, fab: (a: A) => B) => (a: A): C => fbc(fab(a)); const constant = (value: T): () => T => { return () => { return value; }; }; const constantAsync = (value: T): () => Promise => { return async () => value; }; const identity = (x: T): T => { return x; }; const identityAsync = async (x: T): Promise => { return x; }; const tripleEquals = (a: T, b: T): boolean => { return a === b; }; function curry (fn: (...restArgs: REST) => OUT): (...restArgs: REST) => OUT; function curry (fn: (a: A, ...restArgs: REST) => OUT, a: A): (...restArgs: REST) => OUT; function curry (fn: (a: A, b: B, ...restArgs: REST) => OUT, a: A, b: B): (...restArgs: REST) => OUT; function curry (fn: (a: A, b: B, c: C, ...restArgs: REST) => OUT, a: A, b: B, c: C): (...restArgs: REST) => OUT; function curry (fn: (a: A, b: B, c: C, d: D, ...restArgs: REST) => OUT, a: A, b: B, c: C, d: D): (...restArgs: REST) => OUT; function curry (fn: (a: A, b: B, c: C, d: D, e: E, ...restArgs: REST) => OUT, a: A, b: B, c: C, d: D, e: E): (...restArgs: REST) => OUT; function curry (fn: (a: A, b: B, c: C, d: D, e: E, f: F, ...restArgs: REST) => OUT, a: A, b: B, c: C, d: D, e: E, f: F): (...restArgs: REST) => OUT; function curry (fn: (a: A, b: B, c: C, d: D, e: E, f: F, g: G, ...restArgs: REST) => OUT, a: A, b: B, c: C, d: D, e: E, f: F, g: G): (...restArgs: REST) => OUT; function curry (fn: (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, ...restArgs: REST) => OUT, a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H): (...restArgs: REST) => OUT; function curry (fn: (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, ...restArgs: REST) => OUT, a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I): (...restArgs: REST) => OUT; function curry (fn: (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, ...restArgs: REST) => OUT, a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J): (...restArgs: REST) => OUT; function curry (fn: (...allArgs: any[]) => OUT, ...initialArgs: any[]): (...restArgs: any[]) => OUT { return (...restArgs: any[]) => { const all = initialArgs.concat(restArgs); return fn.apply(null, all); }; } const not = (f: (t: T) => boolean) => (t: T): boolean => !f(t); const die = (msg: string) => { return (): never => { throw new Error(msg); }; }; const apply = (f: () => T): T => { return f(); }; const call = (f: () => any): void => { f(); }; const never: (...args: any[]) => false = constant(false); const always: (...args: any[]) => true = constant(true); const neverAsync: (...args: any[]) => Promise = constantAsync(false); const alwaysAsync: (...args: any[]) => Promise = constantAsync(true); /* Used to weaken types */ const weaken = (b: B): A => b; type Fun = (x: X) => Y; const pipe: { (a: A, ab: Fun): B; (a: A, ab: Fun, bc: Fun): C; (a: A, ab: Fun, bc: Fun, cd: Fun): D; (a: A, ab: Fun, bc: Fun, cd: Fun, de: Fun): E; (a: A, ab: Fun, bc: Fun, cd: Fun, de: Fun, ef: Fun): F; (a: A, ab: Fun, bc: Fun, cd: Fun, de: Fun, ef: Fun, fg: Fun): G; (a: A, ab: Fun, bc: Fun, cd: Fun, de: Fun, ef: Fun, fg: Fun, gh: Fun): H; } = (a: A, ab: Fun, bc?: Fun, cd?: Fun, de?: Fun, ef?: Fun, fg?: Fun, gh?: Fun): B | C | D | E | F | G | H => { const b = ab(a); if (Type.isNullable(bc)) { return b; } const c = bc(b); if (Type.isNullable(cd)) { return c; } const d = cd(c); if (Type.isNullable(de)) { return d; } const e = de(d); if (Type.isNullable(ef)) { return e; } const f = ef(e); if (Type.isNullable(fg)) { return f; } const g = fg(f); if (Type.isNullable(gh)) { return g; } return gh(g); }; export { noop, noarg, compose, compose1, constant, constantAsync, identity, identityAsync, tripleEquals, curry, not, die, apply, call, never, neverAsync, always, alwaysAsync, weaken, pipe };