UNPKG

870 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 return funcs.reduce(function (a, b) {
28 return function () {
29 return a(b.apply(undefined, arguments));
30 };
31 });
32}
\No newline at end of file