updeep-remeda/lib/util/curry.js
Aaron Jensen 3ce001a4f6 Replace lodash curry with our own
lodash's curry is great and does a lot, but we don't need it all. This
is quite a bit faster.

Currently, it does away with placeholder support.

updeep curry partial call x 9,951,410 ops/sec ±1.61% (62 runs sampled)
lodash curry partial call x 988,949 ops/sec ±1.36% (62 runs sampled)
2015-08-11 23:58:54 -07:00

53 lines
1.2 KiB
JavaScript

/* eslint no-shadow:0 */
export function curry1(fn) {
return function curried(a, b, c) {
const n = arguments.length;
if (n >= 1) return fn(a, b, c);
return curried;
};
}
export function curry2(fn) {
return function curried(a, b, c, d) {
const n = arguments.length;
if (n >= 2) return fn(a, b, c, d);
if (n === 1) return curry1((b, c, d) => fn(a, b, c, d));
return curried;
};
}
export function curry3(fn) {
return function curried(a, b, c, d, e) {
const n = arguments.length;
if (n >= 3) return fn(a, b, c, d, e);
if (n === 2) return curry1((c, d, e) => fn(a, b, c, d, e));
if (n === 1) return curry2((b, c, d, e) => fn(a, b, c, d, e));
return curried;
};
}
export function curry4(fn) {
return function curried(a, b, c, d, e, f) {
const n = arguments.length;
if (n >= 4) return fn(a, b, c, d, e, f);
if (n === 3) return curry1((d, e, f) => fn(a, b, c, d, e, f));
if (n === 2) return curry2((c, d, e, f) => fn(a, b, c, d, e, f));
if (n === 1) return curry3((b, c, d, e, f) => fn(a, b, c, d, e, f));
return curried;
};
}
export default function curry(fn, length = fn.length) {
return [
fn,
curry1,
curry2,
curry3,
curry4,
][length](fn);
}