UNPKG

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