compose

说明

从右往左执行处理器函数, 与 pipe() 方法相反

源码

用法

compose.test.ts
1
import { compose } from "../../utility/function/compose";
2
3
describe('compose', () => {
4
test('compose should invoke function from right to left', () => {
5
function func1() {
6
return func2() * 2;
7
}
8
function func2() {
9
return func3() * 3;
10
}
11
function func3() {
12
return 4;
13
}
14
15
function func4() {
16
return func5() + '*' + 'yang';
17
}
18
function func5() {
19
return func6() + '*' + 'zhao';
20
}
21
function func6() {
22
return 'duan';
23
}
24
25
const received = {
26
s1: [func1, func2, func3],
27
s2: [func4, func5, func6],
28
};
29
const expected = {
30
p1: 24,
31
p2: 'duan*zhao*yang',
32
};
33
34
const r1 = compose(...received.s1);
35
const r2 = compose(...received.s2);
36
37
expect(r1).toBe(expected.p1);
38
expect(r2).toBe(expected.p2);
39
});
40
});
Copied!
最近更新 1yr ago
复制链接