@@ -21,7 +21,7 @@ inference and type checking of the operator functions.
21
21
Pipe a value through a series of operator functions.
22
22
23
23
``` ts
24
- import { pipe } from " @core/pipe" ;
24
+ import { pipe } from " @core/pipe/pipe " ;
25
25
26
26
const result = pipe (
27
27
1 ,
@@ -36,7 +36,7 @@ Or use `async` module to pipe a value through a series of asynchronous operator
36
36
functions.
37
37
38
38
``` ts
39
- import { pipe } from " @core/pipe/async" ;
39
+ import { pipe } from " @core/pipe/async/pipe " ;
40
40
41
41
const result = await pipe (
42
42
1 ,
@@ -47,6 +47,65 @@ const result = await pipe(
47
47
console .log (result ); // "4"
48
48
```
49
49
50
+ If you want to create a new function that composes multiple operators, use
51
+ ` compose ` like below.
52
+
53
+ ``` ts
54
+ import { compose } from " @core/pipe/compose" ;
55
+
56
+ const operator = compose (
57
+ (v : number ) => v + 1 , // The first operator must be typed explicitly
58
+ (v ) => v * 2 , // inferred as (v: number) => number
59
+ (v ) => v .toString (), // inferred as (v: number) => string
60
+ );
61
+ console .log (operator (1 )); // "4"
62
+ ```
63
+
64
+ Or use ` async ` module to compose multiple asynchronous operators.
65
+
66
+ ``` ts
67
+ import { compose } from " @core/pipe/async/compose" ;
68
+
69
+ const operator = compose (
70
+ (v : number ) => Promise .resolve (v + 1 ), // The first operator must be typed explicitly
71
+ (v ) => Promise .resolve (v * 2 ), // inferred as (v: number) => number | Promise<number>
72
+ (v ) => Promise .resolve (v .toString ()), // inferred as (v: number) => string | Promise<string>
73
+ );
74
+ console .log (await operator (1 )); // "4"
75
+ ```
76
+
77
+ ## Difference
78
+
79
+ The ` pipe ` function in the root module is equivalent to function calls without
80
+ ` await ` like below.
81
+
82
+ ``` ts
83
+ import { pipe } from " @core/pipe/pipe" ;
84
+
85
+ const a = (v : unknown ) => v ;
86
+ const b = (v : unknown ) => v ;
87
+ const c = (v : unknown ) => v ;
88
+
89
+ // Equivalent
90
+ console .log (pipe (1 , a , b , c )); // 1
91
+ console .log (c (b (a (1 )))); // 1
92
+ ```
93
+
94
+ The ` pipe ` function in the ` async ` module is equivalent to function calls with
95
+ ` await ` like below.
96
+
97
+ ``` ts
98
+ import { pipe } from " @core/pipe/async/pipe" ;
99
+
100
+ const a = (v : unknown ) => Promise .resolve (v );
101
+ const b = (v : unknown ) => Promise .resolve (v );
102
+ const c = (v : unknown ) => Promise .resolve (v );
103
+
104
+ // Equivalent
105
+ console .log (await pipe (1 , a , b , c )); // 1
106
+ console .log (await c (await b (await a (1 )))); // 1
107
+ ```
108
+
50
109
## License
51
110
52
111
The code follows MIT license written in [ LICENSE] ( ./LICENSE ) . Contributors need
0 commit comments