forked from Tinkoff/utils.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose.ts
More file actions
27 lines (25 loc) · 671 Bytes
/
compose.ts
File metadata and controls
27 lines (25 loc) · 671 Bytes
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
import { compose } from '../typings/types';
/**
* Performs right-to-left function composition. The rightmost function may have
* any arity; the remaining functions must be unary.
*
* **Note:** The result of compose is not automatically curried.
*
* @param {...Function} fns
* @return {Function}
* @example
*
* var f = compose(x => x + 1, x => -x, Math.pow);
*
* f(3, 4); // -(3^4) + 1
*/
export default ((...fns) =>
(...args) => {
const n = fns.length - 1;
let result = fns[n](...args);
for (let i = n - 1; i >= 0; i--) {
result = fns[i](result);
}
return result;
}
) as typeof compose