-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcompose.ts
28 lines (24 loc) · 869 Bytes
/
compose.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Borrowed from [redux project](https://github.com/reduxjs/redux)
*
* 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(...funcs: Function[]): Function {
if (funcs.length === 0) {
return (arg: unknown) => arg;
}
if (funcs.length === 1) {
return funcs[0];
}
return funcs.reduce((a, b) => (...args: unknown[]) => a(b(...args)));
}
const noop = () => {};
export const pipe = (...fns: Function[]) => compose(...fns)(noop);
export default compose;