This repository has been archived by the owner on May 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fns.ts
363 lines (342 loc) · 10.7 KB
/
fns.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// deno-lint-ignore-file no-explicit-any
import type { Fn, Lazy, Nil, UnknownFn } from "./types.ts";
/*******************************************************************************
* isNotNil
*
* Takes a value and returns true if the value is not null or undefined. Also
* acts as a type guard.
******************************************************************************/
export const isNotNil = <A>(a: A): a is NonNullable<A> =>
a !== null && a !== undefined;
/*******************************************************************************
* isNil
*
* Takes a value and returns false if the value is not null or undefined. Also
* acts as a type guard.
******************************************************************************/
export const isNil = (a: unknown): a is Nil => a === null || a === undefined;
/*******************************************************************************
* isRecord
*
* Takes a value and returns false if the value is a Record. Also
* acts as a type guard.
******************************************************************************/
export const isRecord = (
a: unknown,
): a is Record<string | number | symbol, unknown> =>
isNotNil(a) && typeof a === "object";
/*******************************************************************************
* Identity
*
* Takes a value and returns that same value
******************************************************************************/
export const identity = <A>(a: A): A => a;
/*******************************************************************************
* Compose
*
* Takes two functions with matching types and composes them into a new
* function.
******************************************************************************/
export const compose = <B, C>(fbc: Fn<[B], C>) =>
<A>(fab: Fn<[A], B>): Fn<[A], C> => (a: A): C => fbc(fab(a));
/*******************************************************************************
* Constant
*
* Creates a constant function around the value a
******************************************************************************/
export const constant = <A>(a: A): Lazy<A> => () => a;
/*******************************************************************************
* Memoize
*
* A naive memoization function with no cache release mechanism
******************************************************************************/
export const memoize = <A, B>(f: (a: A) => B): (a: A) => B => {
const cache = new Map();
return (a) => {
if (cache.has(a)) {
return cache.get(a);
}
const b = f(a);
cache.set(a, b);
return b;
};
};
/*******************************************************************************
* TypeOf
*
* An extended typeOf function that returns "null" for null instead of "object"
******************************************************************************/
export const typeOf = (
x: unknown,
):
| "string"
| "number"
| "bigint"
| "boolean"
| "symbol"
| "undefined"
| "object"
| "function"
| "null" => (x === null ? "null" : typeof x);
/*******************************************************************************
* Intersect
*
* Takes two types and returns their intersection (if it is possible)
******************************************************************************/
export const intersect = <A, B>(a: A, b: B): A & B => {
if (a !== undefined && b !== undefined) {
const tx = typeOf(a);
const ty = typeOf(b);
if (tx === "object" || ty === "object") {
return Object.assign({}, a, b);
}
}
return b as A & B;
};
/*******************************************************************************
* HasOwnProperty
*
* An alias for Object.prototype.hasOwnProperty
******************************************************************************/
export const hasOwnProperty = Object.prototype.hasOwnProperty;
/*******************************************************************************
* Apply
*
* Takes a group of arguments and curries them so that a function can be appied
* to them later (pipeable Function.apply)
******************************************************************************/
export const apply = <AS extends unknown[], B>(...as: AS) =>
(fn: Fn<AS, B>): B => fn(...as);
/*******************************************************************************
* Call
*
* Takes a function and returns that function (pipeable Function.call)
******************************************************************************/
export const call = <AS extends unknown[], B>(fn: Fn<AS, B>) =>
(...as: AS) => fn(...as);
/*******************************************************************************
* Apply1
*
* A special case of apply for functions that only take a single argument
******************************************************************************/
export const apply1 = <A, B>(a: A, fn: Fn<[A], B>): B => fn(a);
/*******************************************************************************
* Absurd
*
* A function that should never be called, useful for some theoretical type
* implementations
******************************************************************************/
export function absurd<A>(_: never): A {
throw new Error("Called `absurd` function which should be uncallable");
}
/*******************************************************************************
* Hole
*
* The hole const is used for type hole based programming
******************************************************************************/
export const _: <T>() => T = absurd as any;
/*******************************************************************************
* Wait
*
* The wait function returns a Promise<void> that resolves after ms milliseconds
******************************************************************************/
export const wait = (ms: number) => new Promise((res) => setTimeout(res, ms));
/*******************************************************************************
* Pipe
*
* The pipe takes a value as the first argument and composes it with subsequent
* function arguments, returning the result of the last function passed in
*
* Original pipe function pulled from fp-ts and modified
* https://github.com/gcanti/fp-ts/blob/master/src/pipeable.ts
******************************************************************************/
export type PipeFn = {
<A>(a: A): A;
<A, B>(a: A, ab: (a: A) => B): B;
<A, B, C>(a: A, ab: (a: A) => B, bc: (b: B) => C): C;
<A, B, C, D>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
): D;
<A, B, C, D, E>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
): E;
<A, B, C, D, E, F>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
): F;
<A, B, C, D, E, F, G>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
): G;
<A, B, C, D, E, F, G, H>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
): H;
<A, B, C, D, E, F, G, H, I>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
): I;
<A, B, C, D, E, F, G, H, I, J>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
ij: (i: I) => J,
): J;
<A, B, C, D, E, F, G, H, I, J, K>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
ij: (i: I) => J,
jk: (j: J) => K,
): K;
<A, B, C, D, E, F, G, H, I, J, K, L>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
ij: (i: I) => J,
jk: (j: J) => K,
kl: (K: K) => L,
): L;
<A, B, C, D, E, F, G, H, I, J, K, L>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
ij: (i: I) => J,
jk: (j: J) => K,
kl: (K: K) => L,
end: never,
): L;
};
export const pipe: PipeFn = (a: unknown, ...fns: UnknownFn[]): unknown =>
fns.reduce(apply1, a);
/*******************************************************************************
* Flow
*
* The flow function is a variadic extension of compose, where each subsequent
* flow argument is the next function in a compose chain.
*
* Original flow function pulled from fp-ts and modified
* https://github.com/gcanti/fp-ts/blob/master/src/functions.ts
******************************************************************************/
type FlowFn = {
<A extends ReadonlyArray<unknown>, B>(ab: (...a: A) => B): (...a: A) => B;
<A extends ReadonlyArray<unknown>, B, C>(
ab: (...a: A) => B,
bc: (b: B) => C,
): (...a: A) => C;
<A extends ReadonlyArray<unknown>, B, C, D>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
): (...a: A) => D;
<A extends ReadonlyArray<unknown>, B, C, D, E>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
): (...a: A) => E;
<A extends ReadonlyArray<unknown>, B, C, D, E, F>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
): (...a: A) => F;
<A extends ReadonlyArray<unknown>, B, C, D, E, F, G>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
): (...a: A) => G;
<A extends ReadonlyArray<unknown>, B, C, D, E, F, G, H>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
): (...a: A) => H;
<A extends ReadonlyArray<unknown>, B, C, D, E, F, G, H, I>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
): (...a: A) => I;
<A extends ReadonlyArray<unknown>, B, C, D, E, F, G, H, I, J>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
ij: (i: I) => J,
): (...a: A) => J;
};
export const flow: FlowFn = <AS extends unknown[], B>(
a: (...as: AS) => B,
...fns: Fn<[any], any>[]
): (...as: AS) => unknown => {
return (...args: AS): unknown => fns.reduce(apply1, a(...args));
};