UNPKG

652 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(...funcs) {
13 if (funcs.length === 0) {
14 return arg => arg
15 }
16
17 if (funcs.length === 1) {
18 return funcs[0]
19 }
20
21 return funcs.reduce((a, b) => (...args) => a(b(...args)))
22}