UNPKG

tamda

Version:

Practical functional programming library for TypeScript

58 lines 2 kB
/** * Creates a curried version of the supplied function. * * Strict Functional Programming definition and not Ramda's. * * @example * const sum = (a, b) => a + b; * const sumCurried = curry(sum); * sumCurried (1) (2); * // 3 */ // tslint:disable-next-line: ban-types export function curry(fn) { // Optimize most common arities switch (fn.length) { case 0: case 1: return fn; case 2: return (a) => (b) => fn(a, b); case 3: return (a) => (b) => (c) => fn(a, b, c); case 4: // prettier-ignore return (a) => (b) => (c) => (d) => fn(a, b, c, d); case 5: // prettier-ignore return (a) => (b) => (c) => (d) => (e) => fn(a, b, c, d, e); case 6: // prettier-ignore return (a) => (b) => (c) => (d) => (e) => (f) => fn(a, b, c, d, e, f); case 7: // prettier-ignore return (a) => (b) => (c) => (d) => (e) => (f) => (g) => fn(a, b, c, d, e, f, g); case 8: // prettier-ignore return (a) => (b) => (c) => (d) => (e) => (f) => (g) => (h) => fn(a, b, c, d, e, f, g, h); case 9: // prettier-ignore return (a) => (b) => (c) => (d) => (e) => (f) => (g) => (h) => (i) => fn(a, b, c, d, e, f, g, h, i); case 10: // prettier-ignore return (a) => (b) => (c) => (d) => (e) => (f) => (g) => (h) => (i) => (j) => fn(a, b, c, d, e, f, g, h, i, j); } // Bigger arities will keep an extra array in memory to keep track of arguments let allArgs = []; function builder() { allArgs.push(...arguments); if (allArgs.length < fn.length) { return builder; } const result = fn.apply(undefined, allArgs); allArgs = []; return result; } return builder; } //# sourceMappingURL=curry.js.map