36 lines
927 B
JavaScript
36 lines
927 B
JavaScript
|
"use strict";
|
||
|
|
||
|
exports.__esModule = true;
|
||
|
exports["default"] = compose;
|
||
|
/**
|
||
|
* 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))).
|
||
|
*/
|
||
|
|
||
|
function compose() {
|
||
|
for (var _len = arguments.length, funcs = Array(_len), _key = 0; _key < _len; _key++) {
|
||
|
funcs[_key] = arguments[_key];
|
||
|
}
|
||
|
|
||
|
if (funcs.length === 0) {
|
||
|
return function (arg) {
|
||
|
return arg;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
if (funcs.length === 1) {
|
||
|
return funcs[0];
|
||
|
}
|
||
|
|
||
|
return funcs.reduce(function (a, b) {
|
||
|
return function () {
|
||
|
return a(b.apply(undefined, arguments));
|
||
|
};
|
||
|
});
|
||
|
}
|