2019-11-11 15:29:37 -06:00
|
|
|
/**
|
|
|
|
* Composes single-argument functions from right to left. The rightmost
|
|
|
|
* function can take multiple arguments as it provides the signature for
|
|
|
|
* the resulting composite function.
|
|
|
|
*
|
|
|
|
* @param {...Function} funcs The functions to compose.
|
|
|
|
* @returns {Function} A function obtained by composing the argument functions
|
|
|
|
* from right to left. For example, compose(f, g, h) is identical to doing
|
|
|
|
* (...args) => f(g(h(...args))).
|
|
|
|
*/
|
2019-10-03 16:39:27 -05:00
|
|
|
export function compose(...funcs) {
|
|
|
|
if (funcs.length === 0) {
|
2019-11-11 15:29:37 -06:00
|
|
|
return arg => arg;
|
2019-10-03 16:39:27 -05:00
|
|
|
}
|
|
|
|
if (funcs.length === 1) {
|
2019-11-11 15:29:37 -06:00
|
|
|
return funcs[0];
|
2019-10-03 16:39:27 -05:00
|
|
|
}
|
2019-11-11 15:29:37 -06:00
|
|
|
return funcs.reduce((a, b) => (...args) => a(b(...args)));
|
2019-10-03 16:39:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function fastApply(holder, fn, args) {
|
|
|
|
if (holder || args.length > 5) {
|
|
|
|
return fn.apply(holder, args);
|
|
|
|
}
|
|
|
|
if (0 === args.length) {
|
|
|
|
return fn();
|
|
|
|
}
|
|
|
|
if (1 === args.length) {
|
|
|
|
return fn(args[0]);
|
|
|
|
}
|
|
|
|
if (2 === args.length) {
|
|
|
|
return fn(args[0], args[1]);
|
|
|
|
}
|
|
|
|
if (3 === args.length) {
|
|
|
|
return fn(args[0], args[1], args[2]);
|
|
|
|
}
|
|
|
|
if (4 === args.length) {
|
|
|
|
return fn(args[0], args[1], args[2], args[3]);
|
|
|
|
}
|
|
|
|
if (5 === args.length) {
|
|
|
|
return fn(args[0], args[1], args[2], args[3], args[4]);
|
|
|
|
}
|
|
|
|
}
|