UNPKG

965 BJavaScriptView Raw
1/**
2 * Composes single-argument functions from right to left. The rightmost
3 * function can take multiple arguments as it provides the signature for
4 * the resulting composite function.
5 *
6 * @param {...Function} funcs The functions to compose.
7 * @returns {Function} A function obtained by composing the argument functions
8 * from right to left. For example, compose(f, g, h) is identical to doing
9 * (...args) => f(g(h(...args))).
10 */
11
12export default function compose() {
13 for (var _len = arguments.length, funcs = Array(_len), _key = 0; _key < _len; _key++) {
14 funcs[_key] = arguments[_key];
15 }
16
17 if (funcs.length === 0) {
18 return function (arg) {
19 return arg;
20 };
21 }
22
23 if (funcs.length === 1) {
24 return funcs[0];
25 }
26
27 var last = funcs[funcs.length - 1];
28 var rest = funcs.slice(0, -1);
29 return function () {
30 return rest.reduceRight(function (composed, f) {
31 return f(composed);
32 }, last.apply(undefined, arguments));
33 };
34}
\No newline at end of file