@oscarpalmer/atoms 0.147.0 → 0.149.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,433 @@
1
+ import {flow} from '../../function/work';
2
+ import {isError, isOk} from '../../internal/result';
3
+ import type {GenericCallback} from '../../models';
4
+ import {attempt} from '../index';
5
+ import type {Result, UnwrapValue} from '../models';
6
+
7
+ // #region Types
8
+
9
+ /**
10
+ * A synchronous Flow, a function that attempts to pipe values through a series of functions
11
+ */
12
+ export type AttemptFlow<Callback extends GenericCallback, Value> = (
13
+ ...args: Parameters<Callback>
14
+ ) => Result<UnwrapValue<Value>>;
15
+
16
+ /**
17
+ * An asynchronous Flow, a function that attempts to pipe values through a series of functions
18
+ */
19
+ export type AttemptFlowPromise<Callback extends GenericCallback, Value> = (
20
+ ...args: Parameters<Callback>
21
+ ) => Promise<Result<UnwrapValue<Value>>>;
22
+
23
+ // #endregion
24
+
25
+ // #region Functions
26
+
27
+ // #region Asynchronous
28
+
29
+ /**
30
+ * Create an asynchronous Flow, a function that attempts to pipe a value through a series of functions
31
+ * @returns Flow function
32
+ */
33
+ function attemptAsyncFlow<Fn extends GenericCallback>(
34
+ fn: Fn,
35
+ ): AttemptFlowPromise<Fn, ReturnType<Fn>>;
36
+
37
+ /**
38
+ * Create an asynchronous Flow, a function that pipes values through a series of functions
39
+ * @returns Flow function
40
+ */
41
+ function attemptAsyncFlow<First extends GenericCallback, Second>(
42
+ first: First,
43
+ second: (value: Awaited<UnwrapValue<ReturnType<First>>>) => Second,
44
+ ): AttemptFlowPromise<First, Second>;
45
+
46
+ /**
47
+ * Create an asynchronous Flow, a function that pipes values through a series of functions
48
+ * @returns Flow function
49
+ */
50
+ function attemptAsyncFlow<First extends GenericCallback, Second, Third>(
51
+ first: First,
52
+ second: (value: Awaited<UnwrapValue<ReturnType<First>>>) => Second,
53
+ third: (value: Awaited<UnwrapValue<Second>>) => Third,
54
+ ): AttemptFlowPromise<First, Third>;
55
+
56
+ /**
57
+ * Create an asynchronous Flow, a function that pipes values through a series of functions
58
+ * @returns Flow function
59
+ */
60
+ function attemptAsyncFlow<First extends GenericCallback, Second, Third, Fourth>(
61
+ first: First,
62
+ second: (value: Awaited<UnwrapValue<ReturnType<First>>>) => Second,
63
+ third: (value: Awaited<UnwrapValue<Second>>) => Third,
64
+ fourth: (value: Awaited<UnwrapValue<Third>>) => Fourth,
65
+ ): AttemptFlowPromise<First, Fourth>;
66
+
67
+ /**
68
+ * Create an asynchronous Flow, a function that pipes values through a series of functions
69
+ * @returns Flow function
70
+ */
71
+ function attemptAsyncFlow<First extends GenericCallback, Second, Third, Fourth, Fifth>(
72
+ first: First,
73
+ second: (value: Awaited<UnwrapValue<ReturnType<First>>>) => Second,
74
+ third: (value: Awaited<UnwrapValue<Second>>) => Third,
75
+ fourth: (value: Awaited<UnwrapValue<Third>>) => Fourth,
76
+ fifth: (value: Awaited<UnwrapValue<Fourth>>) => Fifth,
77
+ ): AttemptFlowPromise<First, Fifth>;
78
+
79
+ /**
80
+ * Create an asynchronous Flow, a function that pipes values through a series of functions
81
+ * @returns Flow function
82
+ */
83
+ function attemptAsyncFlow<First extends GenericCallback, Second, Third, Fourth, Fifth, Sixth>(
84
+ first: First,
85
+ second: (value: Awaited<UnwrapValue<ReturnType<First>>>) => Second,
86
+ third: (value: Awaited<UnwrapValue<Second>>) => Third,
87
+ fourth: (value: Awaited<UnwrapValue<Third>>) => Fourth,
88
+ fifth: (value: Awaited<UnwrapValue<Fourth>>) => Fifth,
89
+ sixth: (value: Awaited<UnwrapValue<Fifth>>) => Sixth,
90
+ ): AttemptFlowPromise<First, Sixth>;
91
+
92
+ /**
93
+ * Create an asynchronous Flow, a function that pipes values through a series of functions
94
+ * @returns Flow function
95
+ */
96
+ function attemptAsyncFlow<
97
+ First extends GenericCallback,
98
+ Second,
99
+ Third,
100
+ Fourth,
101
+ Fifth,
102
+ Sixth,
103
+ Seventh,
104
+ >(
105
+ first: First,
106
+ second: (value: Awaited<UnwrapValue<ReturnType<First>>>) => Second,
107
+ third: (value: Awaited<UnwrapValue<Second>>) => Third,
108
+ fourth: (value: Awaited<UnwrapValue<Third>>) => Fourth,
109
+ fifth: (value: Awaited<UnwrapValue<Fourth>>) => Fifth,
110
+ sixth: (value: Awaited<UnwrapValue<Fifth>>) => Sixth,
111
+ seventh: (value: Awaited<UnwrapValue<Sixth>>) => Seventh,
112
+ ): AttemptFlowPromise<First, Seventh>;
113
+
114
+ /**
115
+ * Create an asynchronous Flow, a function that pipes values through a series of functions
116
+ * @returns Flow function
117
+ */
118
+ function attemptAsyncFlow<
119
+ First extends GenericCallback,
120
+ Second,
121
+ Third,
122
+ Fourth,
123
+ Fifth,
124
+ Sixth,
125
+ Seventh,
126
+ Eighth,
127
+ >(
128
+ first: First,
129
+ second: (value: Awaited<UnwrapValue<ReturnType<First>>>) => Second,
130
+ third: (value: Awaited<UnwrapValue<Second>>) => Third,
131
+ fourth: (value: Awaited<UnwrapValue<Third>>) => Fourth,
132
+ fifth: (value: Awaited<UnwrapValue<Fourth>>) => Fifth,
133
+ sixth: (value: Awaited<UnwrapValue<Fifth>>) => Sixth,
134
+ seventh: (value: Awaited<UnwrapValue<Sixth>>) => Seventh,
135
+ eighth: (value: Awaited<UnwrapValue<Seventh>>) => Eighth,
136
+ ): AttemptFlowPromise<First, Eighth>;
137
+
138
+ /**
139
+ * Create an asynchronous Flow, a function that pipes values through a series of functions
140
+ * @returns Flow function
141
+ */
142
+ function attemptAsyncFlow<
143
+ First extends GenericCallback,
144
+ Second,
145
+ Third,
146
+ Fourth,
147
+ Fifth,
148
+ Sixth,
149
+ Seventh,
150
+ Eighth,
151
+ Ninth,
152
+ >(
153
+ first: First,
154
+ second: (value: Awaited<UnwrapValue<ReturnType<First>>>) => Second,
155
+ third: (value: Awaited<UnwrapValue<Second>>) => Third,
156
+ fourth: (value: Awaited<UnwrapValue<Third>>) => Fourth,
157
+ fifth: (value: Awaited<UnwrapValue<Fourth>>) => Fifth,
158
+ sixth: (value: Awaited<UnwrapValue<Fifth>>) => Sixth,
159
+ seventh: (value: Awaited<UnwrapValue<Sixth>>) => Seventh,
160
+ eighth: (value: Awaited<UnwrapValue<Seventh>>) => Eighth,
161
+ ninth: (value: Awaited<UnwrapValue<Eighth>>) => Ninth,
162
+ ): AttemptFlowPromise<First, Ninth>;
163
+
164
+ /**
165
+ * Create an asynchronous Flow, a function that pipes values through a series of functions
166
+ * @returns Flow function
167
+ */
168
+ function attemptAsyncFlow<
169
+ First extends GenericCallback,
170
+ Second,
171
+ Third,
172
+ Fourth,
173
+ Fifth,
174
+ Sixth,
175
+ Seventh,
176
+ Eighth,
177
+ Ninth,
178
+ Tenth,
179
+ >(
180
+ first: First,
181
+ second: (value: Awaited<UnwrapValue<ReturnType<First>>>) => Second,
182
+ third: (value: Awaited<UnwrapValue<Second>>) => Third,
183
+ fourth: (value: Awaited<UnwrapValue<Third>>) => Fourth,
184
+ fifth: (value: Awaited<UnwrapValue<Fourth>>) => Fifth,
185
+ sixth: (value: Awaited<UnwrapValue<Fifth>>) => Sixth,
186
+ seventh: (value: Awaited<UnwrapValue<Sixth>>) => Seventh,
187
+ eighth: (value: Awaited<UnwrapValue<Seventh>>) => Eighth,
188
+ ninth: (value: Awaited<UnwrapValue<Eighth>>) => Ninth,
189
+ tenth: (value: Awaited<UnwrapValue<Ninth>>) => Tenth,
190
+ ): AttemptFlowPromise<First, Tenth>;
191
+
192
+ /**
193
+ * Create an asynchronous Flow, a function that pipes values through a series of functions
194
+ * @returns Flow function
195
+ */
196
+ function attemptAsyncFlow<Fn extends GenericCallback>(
197
+ fn: Fn,
198
+ ...fns: Array<(value: Awaited<UnwrapValue<ReturnType<Fn>>>) => unknown>
199
+ ): AttemptFlowPromise<Fn, ReturnType<Fn>>;
200
+
201
+ /**
202
+ * Create an asynchronous Flow, a function that pipes values through a series of functions
203
+ * @returns Flow function
204
+ */
205
+ function attemptAsyncFlow(
206
+ ...fns: GenericCallback[]
207
+ ): (...args: unknown[]) => Promise<Result<unknown>>;
208
+
209
+ function attemptAsyncFlow(
210
+ ...fns: GenericCallback[]
211
+ ): (...args: unknown[]) => Promise<Result<unknown>> {
212
+ let Flow: GenericCallback;
213
+
214
+ return (...args: unknown[]) =>
215
+ attempt.async(() => {
216
+ Flow ??= flow.async(...fns);
217
+
218
+ return Flow(
219
+ ...args.map(value => {
220
+ if (isError(value)) {
221
+ throw value.error;
222
+ }
223
+
224
+ return isOk(value) ? value.value : value;
225
+ }),
226
+ );
227
+ });
228
+ }
229
+
230
+ // #endregion
231
+
232
+ // #region Synchronous
233
+
234
+ /**
235
+ * Create a Flow, a function that attempts to pipe values through a function
236
+ * @returns Flow function
237
+ */
238
+ export function attemptFlow<Fn extends GenericCallback>(fn: Fn): AttemptFlow<Fn, ReturnType<Fn>>;
239
+
240
+ /**
241
+ * Create a Flow, a function that attempts to pipe values through a series of functions
242
+ * @returns Flow function
243
+ */
244
+ export function attemptFlow<First extends GenericCallback, Second>(
245
+ first: First,
246
+ second: (value: UnwrapValue<ReturnType<First>>) => Second,
247
+ ): AttemptFlow<First, Second>;
248
+
249
+ /**
250
+ * Create a Flow, a function that attempts to pipe values through a series of functions
251
+ * @returns Flow function
252
+ */
253
+ export function attemptFlow<First extends GenericCallback, Second, Third>(
254
+ first: First,
255
+ second: (value: UnwrapValue<ReturnType<First>>) => Second,
256
+ third: (value: UnwrapValue<Second>) => Third,
257
+ ): AttemptFlow<First, Third>;
258
+
259
+ /**
260
+ * Create a Flow, a function that attempts to pipe values through a series of functions
261
+ * @returns Flow function
262
+ */
263
+ export function attemptFlow<First extends GenericCallback, Second, Third, Fourth>(
264
+ first: First,
265
+ second: (value: UnwrapValue<ReturnType<First>>) => Second,
266
+ third: (value: UnwrapValue<Second>) => Third,
267
+ fourth: (value: UnwrapValue<Third>) => Fourth,
268
+ ): AttemptFlow<First, Fourth>;
269
+
270
+ /**
271
+ * Create a Flow, a function that attempts to pipe values through a series of functions
272
+ * @returns Flow function
273
+ */
274
+ export function attemptFlow<First extends GenericCallback, Second, Third, Fourth, Fifth>(
275
+ first: First,
276
+ second: (value: UnwrapValue<ReturnType<First>>) => Second,
277
+ third: (value: UnwrapValue<Second>) => Third,
278
+ fourth: (value: UnwrapValue<Third>) => Fourth,
279
+ fifth: (value: UnwrapValue<Fourth>) => Fifth,
280
+ ): AttemptFlow<First, Fifth>;
281
+
282
+ /**
283
+ * Create a Flow, a function that attempts to pipe values through a series of functions
284
+ * @returns Flow function
285
+ */
286
+ export function attemptFlow<First extends GenericCallback, Second, Third, Fourth, Fifth, Sixth>(
287
+ first: First,
288
+ second: (value: UnwrapValue<ReturnType<First>>) => Second,
289
+ third: (value: UnwrapValue<Second>) => Third,
290
+ fourth: (value: UnwrapValue<Third>) => Fourth,
291
+ fifth: (value: UnwrapValue<Fourth>) => Fifth,
292
+ sixth: (value: UnwrapValue<Fifth>) => Sixth,
293
+ ): AttemptFlow<First, Sixth>;
294
+
295
+ /**
296
+ * Create a Flow, a function that attempts to pipe values through a series of functions
297
+ * @returns Flow function
298
+ */
299
+ export function attemptFlow<
300
+ First extends GenericCallback,
301
+ Second,
302
+ Third,
303
+ Fourth,
304
+ Fifth,
305
+ Sixth,
306
+ Seventh,
307
+ >(
308
+ first: First,
309
+ second: (value: UnwrapValue<ReturnType<First>>) => Second,
310
+ third: (value: UnwrapValue<Second>) => Third,
311
+ fourth: (value: UnwrapValue<Third>) => Fourth,
312
+ fifth: (value: UnwrapValue<Fourth>) => Fifth,
313
+ sixth: (value: UnwrapValue<Fifth>) => Sixth,
314
+ seventh: (value: UnwrapValue<Sixth>) => Seventh,
315
+ ): AttemptFlow<First, Seventh>;
316
+
317
+ /**
318
+ * Create a Flow, a function that attempts to pipe values through a series of functions
319
+ * @returns Flow function
320
+ */
321
+ export function attemptFlow<
322
+ First extends GenericCallback,
323
+ Second,
324
+ Third,
325
+ Fourth,
326
+ Fifth,
327
+ Sixth,
328
+ Seventh,
329
+ Eighth,
330
+ >(
331
+ first: First,
332
+ second: (value: UnwrapValue<ReturnType<First>>) => Second,
333
+ third: (value: UnwrapValue<Second>) => Third,
334
+ fourth: (value: UnwrapValue<Third>) => Fourth,
335
+ fifth: (value: UnwrapValue<Fourth>) => Fifth,
336
+ sixth: (value: UnwrapValue<Fifth>) => Sixth,
337
+ seventh: (value: UnwrapValue<Sixth>) => Seventh,
338
+ eighth: (value: UnwrapValue<Seventh>) => Eighth,
339
+ ): AttemptFlow<First, Eighth>;
340
+
341
+ /**
342
+ * Create a Flow, a function that attempts to pipe values through a series of functions
343
+ * @returns Flow function
344
+ */
345
+ export function attemptFlow<
346
+ First extends GenericCallback,
347
+ Second,
348
+ Third,
349
+ Fourth,
350
+ Fifth,
351
+ Sixth,
352
+ Seventh,
353
+ Eighth,
354
+ Ninth,
355
+ >(
356
+ first: First,
357
+ second: (value: UnwrapValue<ReturnType<First>>) => Second,
358
+ third: (value: UnwrapValue<Second>) => Third,
359
+ fourth: (value: UnwrapValue<Third>) => Fourth,
360
+ fifth: (value: UnwrapValue<Fourth>) => Fifth,
361
+ sixth: (value: UnwrapValue<Fifth>) => Sixth,
362
+ seventh: (value: UnwrapValue<Sixth>) => Seventh,
363
+ eighth: (value: UnwrapValue<Seventh>) => Eighth,
364
+ ninth: (value: UnwrapValue<Eighth>) => Ninth,
365
+ ): AttemptFlow<First, Ninth>;
366
+
367
+ /**
368
+ * Create a Flow, a function that attempts to pipe values through a series of functions
369
+ * @returns Flow function
370
+ */
371
+ export function attemptFlow<
372
+ First extends GenericCallback,
373
+ Second,
374
+ Third,
375
+ Fourth,
376
+ Fifth,
377
+ Sixth,
378
+ Seventh,
379
+ Eighth,
380
+ Ninth,
381
+ Tenth,
382
+ >(
383
+ first: First,
384
+ second: (value: UnwrapValue<ReturnType<First>>) => Second,
385
+ third: (value: UnwrapValue<Second>) => Third,
386
+ fourth: (value: UnwrapValue<Third>) => Fourth,
387
+ fifth: (value: UnwrapValue<Fourth>) => Fifth,
388
+ sixth: (value: UnwrapValue<Fifth>) => Sixth,
389
+ seventh: (value: UnwrapValue<Sixth>) => Seventh,
390
+ eighth: (value: UnwrapValue<Seventh>) => Eighth,
391
+ ninth: (value: UnwrapValue<Eighth>) => Ninth,
392
+ tenth: (value: UnwrapValue<Ninth>) => Tenth,
393
+ ): AttemptFlow<First, Tenth>;
394
+
395
+ /**
396
+ * Create a Flow, a function that attempts to pipe values through a series of functions
397
+ * @returns Flow function
398
+ */
399
+ export function attemptFlow<First extends GenericCallback>(
400
+ first: First,
401
+ ...fns: Array<(value: UnwrapValue<ReturnType<First>>) => unknown>
402
+ ): AttemptFlow<First, ReturnType<First>>;
403
+
404
+ /**
405
+ * Create a Flow, a function that attempts to pipe values through a series of functions
406
+ * @returns Flow function
407
+ */
408
+ export function attemptFlow(...fns: GenericCallback[]): (...args: unknown[]) => Result<unknown>;
409
+
410
+ export function attemptFlow(...fns: GenericCallback[]): (...args: unknown[]) => Result<unknown> {
411
+ let Flow: GenericCallback;
412
+
413
+ return (...args: unknown[]) =>
414
+ attempt(() => {
415
+ Flow ??= flow(...fns);
416
+
417
+ return Flow(
418
+ ...args.map(value => {
419
+ if (isError(value)) {
420
+ throw value.error;
421
+ }
422
+
423
+ return isOk(value) ? value.value : value;
424
+ }),
425
+ );
426
+ });
427
+ }
428
+
429
+ attemptFlow.async = attemptAsyncFlow;
430
+
431
+ // #endregion
432
+
433
+ // #endregion