-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.ts
158 lines (146 loc) · 4.57 KB
/
template.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Types inspired by https://dev.to/ecyrbe/how-to-use-advanced-typescript-to-define-a-pipe-function-381h
type AnyFn = (v: any) => any;
type LastFnReturnType<F extends AnyFn[], Else = never> = F extends [
...any[],
(...arg: any) => infer R
]
? R
: AnyFn[] extends F
? any
: Else;
type PipeArgs<F extends AnyFn[], Acc extends AnyFn[] = []> = F extends [
(v: infer A) => infer B
]
? [...Acc, (v: A) => B]
: F extends [(v: infer A) => any, ...infer Tail]
? Tail extends [(arg: infer B) => any, ...any[]]
? PipeArgs<Tail, [...Acc, (v: A) => B]>
: Acc
: Acc;
type PipeAsyncArgs<F extends AnyFn[], Acc extends AnyFn[] = []> = F extends [
(v: infer A) => infer B
]
? [...Acc, (v: A) => B]
: F extends [(v: infer A) => any, ...infer Tail]
? Tail extends [(arg: infer B) => any, ...any[]]
? PipeAsyncArgs<Tail, [...Acc, (v: A) => B | Promise<B>]>
: Acc
: Acc;
/**
* Pipes a value through a series of functions. `pipe(v, f, g)` is equivalent
* to `g(f(v))`, but often much more readable.
*
* > [!NOTE]
* >
* > If you want to use more than #{overload-count} functions, you'll either
* > have to provide explicit type annotations, or put a `toPipable` at the
* > end of one pipe, and then continue with the `.pipe()` method:
* > ```ts
* > const result = pipe(value, f, g, ..., toPipable).pipe(h, j, ...)
* > ```
*
* @example
* ```typescript
* const values = pipe(
* document.querySelectorAll('a'),
* els => map(els, el => idMap.get(el.id)),
* vs => filter(vs, v => v !== undefined),
* Array.from<string>
* );
*
* // the same as:
* const values = Array.from(
* filter(
* map(
* document.querySelectorAll('a'),
* el => idMap.get(el.id)
* ),
* v => v !== undefined
* )
* )
* ```
*/
//#[function-overload]
export function pipe<F extends AnyFn[]>(
arg: Parameters<F[0]>[0],
...fns: PipeArgs<F> extends F ? F : PipeArgs<F>
): LastFnReturnType<F>;
export function pipe(value: any, ...fns: AnyFn[]): any {
return fns.reduce((v, f) => f(v), value);
}
/**
* Pipes a value through a series of asynchronous functions, awaiting
* each result before passing it into the next function.
* `await pipeAsync(v, f, g)` is equivalent to `await g(await f(await v))`.
*
* @example
* ```ts
* const name = pipeAsync(
* `https://example.com/${endpoint}`,
* url => fetch(url),
* res => res.json(),
* ({ user }) => user.name,
* )
* ```
*/
//#[async-function-overload]
export function pipeAsync<F extends AnyFn[]>(
arg: Parameters<F[0]>[0] | Promise<Parameters<F[0]>[0]>,
...fns: PipeAsyncArgs<F> extends F ? F : PipeAsyncArgs<F>
): Promise<Awaited<LastFnReturnType<F>>>;
export async function pipeAsync(value: any, ...fns: AnyFn[]): Promise<any> {
for (const f of fns) value = f(await value);
return value;
}
export interface PipeOf<A> {
/**
* Pipes the object through the provided functions and returns the last result.
* `v.pipe(f, g)` is equivalent to `g(f(v))`.
*
* > [!NOTE]
* >
* > If you want to use more than #{overload-count} functions, you'll either
* > have to provide explicit type annotations, or put a `toPipable` at the
* > end of one pipe, and then continue with the `.pipe()` method:
* > ```ts
* > const result = value.pipe(f, g, ..., toPipable).pipe(h, j, ...)
* > ```
*/
//#[method-overload]
pipe<F extends AnyFn[], FirstFn extends (v: A) => Parameters<F[0]>[0]>(
firstFn: FirstFn,
...fns: PipeArgs<F> extends F ? F : PipeArgs<F>
): LastFnReturnType<F, ReturnType<FirstFn>>;
/**
* Pipes the object through the provided asynchronous functions, awaiting
* each result before passing it into the next function.
*
* > [!NOTE]
* >
* > If you want to use more than #{overload-count} functions, you'll either
* > have to provide explicit type annotations, or put a `toPipable` at the
* > end of one pipe, and then continue with the `.pipeAsync()` method:
* > ```ts
* > const result = value.pipeAsync(f, g, ..., toPipable).pipeAsync(h, j, ...)
* > ```
*/
//#[async-method-overload]
pipeAsync<
F extends AnyFn[],
FirstFn extends (v: A) => Parameters<F[0]>[0] | Promise<Parameters<F[0]>[0]>
>(
firstFn: FirstFn,
...fns: PipeAsyncArgs<F> extends F ? F : PipeAsyncArgs<F>
): Promise<Awaited<LastFnReturnType<F, ReturnType<FirstFn>>>>;
}
export type Pipable<T> = T & PipeOf<Pipable<T>>;
export function toPipable<T extends object>(obj: T): Pipable<T> {
return Object.assign(obj, {
pipe(...fns: AnyFn[]) {
return pipe(obj, ...fns);
},
pipeAsync(...fns: AnyFn[]) {
return pipeAsync(obj, ...fns);
},
});
}