t-tasks 1.2.1 → 2.1.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,1913 @@
1
+ //#region src/maybe.d.ts
2
+ /**
3
+ * Just a value
4
+ *
5
+ * Maybe data type specialization representing an existing value
6
+ *
7
+ * @template R underlying value
8
+ */
9
+ export interface Just<R> {
10
+ readonly just: R;
11
+ /**
12
+ * Maybe peeker function
13
+ *
14
+ * Applied to 'just value' returns self invoking op(value) in process
15
+ * Applied to 'nothing' returns self without invoking callback
16
+ *
17
+ * @param op function to be invoked with underlying value
18
+ * @returns self
19
+ */
20
+ tap(op: (value: R) => void): this;
21
+ /**
22
+ * Maybe fallback peeker function
23
+ *
24
+ * Applied to 'just value' returns self without invoking callback
25
+ * Applied to 'nothing' returns self invoking op() in process
26
+ *
27
+ * @param op function to be invoked
28
+ * @returns self
29
+ */
30
+ orTap(op: () => void): this;
31
+ /**
32
+ * Maybe pattern matching peeker function
33
+ *
34
+ * Applied to 'just value' returns self invoking op.just(value) in process
35
+ * Applied to 'nothing' returns self invoking op.nothing() in process
36
+ *
37
+ * @param op.just function to be invoked with underlying value in case of just
38
+ * @param op.nothing function to be invoked in case of nothing
39
+ * @returns self
40
+ */
41
+ matchTap(op: {
42
+ just: (value: R) => void;
43
+ nothing: () => void;
44
+ }): this;
45
+ /**
46
+ * Maybe transformer function
47
+ *
48
+ * Applied to 'just value' returns 'just op(value)'
49
+ * Applied to 'nothing' returns self without invoking transformer
50
+ *
51
+ * @template R2 transformer function's return type
52
+ * @param op transformer to be invoked with underlying value
53
+ * @returns 'just op(value)' or 'nothing'
54
+ */
55
+ map<R2>(this: Just<R>, op: (value: R) => R2): Just<R2>;
56
+ map<R2>(this: Maybe<R>, op: (value: R) => R2): Maybe<R2>;
57
+ /**
58
+ * Maybe fallback transformer function
59
+ *
60
+ * Applied to 'just value' returns self without invoking transformer
61
+ * Applied to 'nothing' returns 'just op()'
62
+ *
63
+ * @template R2 transformer function's return type
64
+ * @param op function to be invoked
65
+ * @returns 'just value' or 'just op()'
66
+ */
67
+ orMap<R2>(this: Just<R>, op: () => R2): Just<R>;
68
+ orMap<R2>(this: Maybe<R>, op: () => R2): Just<R | R2>;
69
+ /**
70
+ * Maybe pattern matching extractor function
71
+ *
72
+ * Applied to 'just value' returns op.just(value)
73
+ * Applied to 'nothing' returns op.nothing()
74
+ *
75
+ * @template R2 just extractor function's return type
76
+ * @template R3 nothing extractor function's return type
77
+ * @param op.just extractor to be invoked with underlying value in case of just
78
+ * @param op.nothing extractor to be invoked in case of nothing
79
+ * @returns op.just(value) or op.nothing()
80
+ */
81
+ match<R2, R3 = R2>(this: Just<R>, op: {
82
+ just: (value: R) => R2;
83
+ nothing: () => R3;
84
+ }): R2;
85
+ match<R2, R3 = R2>(this: Maybe<R>, op: {
86
+ just: (value: R) => R2;
87
+ nothing: () => R3;
88
+ }): R2 | R3;
89
+ /**
90
+ * Maybe pattern matching transformer function
91
+ *
92
+ * Applied to 'just value' returns 'just op.just(value)'
93
+ * Applied to 'nothing' returns 'just op.nothing()'
94
+ *
95
+ * @template R2 just transformer function's return type
96
+ * @template R3 nothing transformer function's return type
97
+ * @param op.just transformer to be invoked with underlying value in case of just
98
+ * @param op.nothing transformer to be invoked in case of nothing
99
+ * @returns 'just op.just(value)' or 'just op.nothing()'
100
+ */
101
+ matchMap<R2, R3 = R2>(this: Just<R>, op: {
102
+ just: (value: R) => R2;
103
+ nothing: () => R3;
104
+ }): Just<R2>;
105
+ matchMap<R2, R3 = R2>(this: Maybe<R>, op: {
106
+ just: (value: R) => R2;
107
+ nothing: () => R3;
108
+ }): Just<R2 | R3>;
109
+ /**
110
+ * Maybe composition function
111
+ *
112
+ * Applied to 'just value' returns 'op(value)'
113
+ * Applied to 'nothing' returns self without invoking composition function
114
+ *
115
+ * @template R2 transformer function result's underlying type
116
+ * @param op transformer to be invoked with underlying value
117
+ * @returns 'op(value)' or 'nothing'
118
+ */
119
+ chain<R2>(this: Just<R>, op: (value: R) => Just<R2>): Just<R2>;
120
+ chain<R2>(this: Just<R>, op: (value: R) => Nothing<R2>): Nothing<never>;
121
+ chain<R2>(this: Just<R>, op: (value: R) => Maybe<R2>): Maybe<R2>;
122
+ chain<R2>(this: Maybe<R>, op: (value: R) => Just<R2>): Maybe<R2>;
123
+ chain<R2>(this: Maybe<R>, op: (value: R) => Nothing<R2>): Nothing<never>;
124
+ chain<R2>(this: Maybe<R>, op: (value: R) => Maybe<R2>): Maybe<R2>;
125
+ /**
126
+ * Maybe fallback composition function
127
+ *
128
+ * Applied to 'just value' returns self without invoking composition function
129
+ * Applied to 'nothing' returns op()
130
+ *
131
+ * @template R2 transformer function result's underlying type
132
+ * @param op transformer to be invoked
133
+ * @returns 'just value' or 'op()'
134
+ */
135
+ orChain<R2>(this: Just<R>, op: () => Just<R2>): Just<R>;
136
+ orChain<R2>(this: Just<R>, op: () => Nothing<R2>): Just<R>;
137
+ orChain<R2>(this: Just<R>, op: () => Maybe<R2>): Just<R>;
138
+ orChain<R2>(this: Maybe<R>, op: () => Just<R2>): Just<R | R2>;
139
+ orChain<R2>(this: Maybe<R>, op: () => Nothing<R2>): Maybe<R>;
140
+ orChain<R2>(this: Maybe<R>, op: () => Maybe<R2>): Maybe<R | R2>;
141
+ /**
142
+ * Maybe pattern matching composition function
143
+ *
144
+ * Applied to 'just value' returns 'op.just(value)'
145
+ * Applied to 'nothing' returns op.nothing()
146
+ *
147
+ * @template R2 just transformer function result's underlying type
148
+ * @template R3 nothing transformer function result's underlying type
149
+ * @param op.just transformer to be invoked with underlying value in case of just
150
+ * @param op.nothing transformer to be invoked in case of nothing
151
+ * @returns 'op.just(value)' or 'op.nothing()'
152
+ */
153
+ matchChain<R2, R3 = R2>(this: Just<R>, op: {
154
+ just: (value: R) => Just<R2>;
155
+ nothing: () => Just<R3>;
156
+ }): Just<R2>;
157
+ matchChain<R2, R3 = R2>(this: Just<R>, op: {
158
+ just: (value: R) => Just<R2>;
159
+ nothing: () => Nothing<R3>;
160
+ }): Just<R2>;
161
+ matchChain<R2, R3 = R2>(this: Just<R>, op: {
162
+ just: (value: R) => Nothing<R2>;
163
+ nothing: () => Just<R3>;
164
+ }): Nothing<never>;
165
+ matchChain<R2, R3 = R2>(this: Just<R>, op: {
166
+ just: (value: R) => Nothing<R2>;
167
+ nothing: () => Nothing<R3>;
168
+ }): Nothing<never>;
169
+ matchChain<R2, R3 = R2>(this: Just<R>, op: {
170
+ just: (value: R) => Just<R2>;
171
+ nothing: () => Maybe<R3>;
172
+ }): Just<R2>;
173
+ matchChain<R2, R3 = R2>(this: Just<R>, op: {
174
+ just: (value: R) => Nothing<R2>;
175
+ nothing: () => Maybe<R3>;
176
+ }): Nothing<never>;
177
+ matchChain<R2, R3 = R2>(this: Just<R>, op: {
178
+ just: (value: R) => Maybe<R2>;
179
+ nothing: () => Just<R3>;
180
+ }): Maybe<R2>;
181
+ matchChain<R2, R3 = R2>(this: Just<R>, op: {
182
+ just: (value: R) => Maybe<R2>;
183
+ nothing: () => Nothing<R3>;
184
+ }): Maybe<R2>;
185
+ matchChain<R2, R3 = R2>(this: Just<R>, op: {
186
+ just: (value: R) => Maybe<R2>;
187
+ nothing: () => Maybe<R3>;
188
+ }): Maybe<R2>;
189
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
190
+ just: (value: R) => Just<R2>;
191
+ nothing: () => Just<R3>;
192
+ }): Just<R2 | R3>;
193
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
194
+ just: (value: R) => Just<R2>;
195
+ nothing: () => Nothing<R3>;
196
+ }): Maybe<R2>;
197
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
198
+ just: (value: R) => Nothing<R2>;
199
+ nothing: () => Just<R3>;
200
+ }): Maybe<R3>;
201
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
202
+ just: (value: R) => Nothing<R2>;
203
+ nothing: () => Nothing<R3>;
204
+ }): Nothing<never>;
205
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
206
+ just: (value: R) => Just<R2>;
207
+ nothing: () => Maybe<R3>;
208
+ }): Maybe<R2 | R3>;
209
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
210
+ just: (value: R) => Nothing<R2>;
211
+ nothing: () => Maybe<R3>;
212
+ }): Maybe<R3>;
213
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
214
+ just: (value: R) => Maybe<R2>;
215
+ nothing: () => Just<R3>;
216
+ }): Maybe<R2 | R3>;
217
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
218
+ just: (value: R) => Maybe<R2>;
219
+ nothing: () => Nothing<R3>;
220
+ }): Maybe<R2>;
221
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
222
+ just: (value: R) => Maybe<R2>;
223
+ nothing: () => Maybe<R3>;
224
+ }): Maybe<R2 | R3>;
225
+ /**
226
+ * Maybe type guard for 'just'
227
+ *
228
+ * @returns 'true' in case wrapped value exists (and resolves this type to be 'just')
229
+ *
230
+ * @example
231
+ * ```typescript
232
+ * if (maybe.isJust()) {
233
+ * console.log(maybe.just);
234
+ * }
235
+ * ```
236
+ */
237
+ isJust(): this is Just<R>;
238
+ /**
239
+ * Maybe type guard for 'nothing'
240
+ *
241
+ * @returns 'true' in case wrapped value is 'nothing' (and resolves this type to be 'nothing')
242
+ *
243
+ * @example
244
+ * ```typescript
245
+ * if (maybe.isNothing()) {
246
+ * console.log('nothing');
247
+ * }
248
+ * ```
249
+ */
250
+ isNothing(): this is Nothing<R>;
251
+ /**
252
+ * Wrap Maybe to singleton generator
253
+ *
254
+ * Useful in order to avoid ambiguous yield types
255
+ *
256
+ * @returns generator of Maybe wrapping this
257
+ */
258
+ generator: MaybeGeneratorFunction<[], R, Just<R>, R>;
259
+ }
260
+ /**
261
+ * Absolutely Nothing
262
+ *
263
+ * Maybe data type specialization representing an absence of any value
264
+ *
265
+ * @template R underlying value
266
+ */
267
+ export interface Nothing<R> {
268
+ /**
269
+ * Maybe peeker function
270
+ *
271
+ * Applied to 'just value' returns self invoking op(value) in process
272
+ * Applied to 'nothing' returns self without invoking callback
273
+ *
274
+ * @param op function to be invoked with underlying value
275
+ * @returns self
276
+ */
277
+ tap(op: (value: R) => void): this;
278
+ /**
279
+ * Maybe fallback peeker function
280
+ *
281
+ * Applied to 'just value' returns self without invoking callback
282
+ * Applied to 'nothing' returns self invoking op() in process
283
+ *
284
+ * @param op function to be invoked
285
+ * @returns self
286
+ */
287
+ orTap(op: () => void): this;
288
+ /**
289
+ * Maybe pattern matching peeker function
290
+ *
291
+ * Applied to 'just value' returns self invoking op.just(value) in process
292
+ * Applied to 'nothing' returns self invoking op.nothing() in process
293
+ *
294
+ * @param op.just function to be invoked with underlying value in case of just
295
+ * @param op.nothing function to be invoked in case of nothing
296
+ * @returns self
297
+ */
298
+ matchTap(op: {
299
+ just: (value: R) => void;
300
+ nothing: () => void;
301
+ }): this;
302
+ /**
303
+ * Maybe transformer function
304
+ *
305
+ * Applied to 'just value' returns 'just op(value)'
306
+ * Applied to 'nothing' returns self without invoking transformer
307
+ *
308
+ * @template R2 transformer function's return type
309
+ * @param op transformer to be invoked with underlying value
310
+ * @returns 'just op(value)' or 'nothing'
311
+ */
312
+ map<R2>(this: Nothing<R>, op: (value: R) => R2): Nothing<never>;
313
+ map<R2>(this: Maybe<R>, op: (value: R) => R2): Maybe<R2>;
314
+ /**
315
+ * Maybe fallback transformer function
316
+ *
317
+ * Applied to 'just value' returns self without invoking transformer
318
+ * Applied to 'nothing' returns 'just op()'
319
+ *
320
+ * @template R2 transformer function's return type
321
+ * @param op function to be invoked
322
+ * @returns 'just value' or 'just op()'
323
+ */
324
+ orMap<R2>(this: Nothing<R>, op: () => R2): Just<R2>;
325
+ orMap<R2>(this: Maybe<R>, op: () => R2): Just<R | R2>;
326
+ /**
327
+ * Maybe pattern matching extractor function
328
+ *
329
+ * Applied to 'just value' returns op.just(value)
330
+ * Applied to 'nothing' returns op.nothing()
331
+ *
332
+ * @template R2 just extractor function's return type
333
+ * @template R3 nothing extractor function's return type
334
+ * @param op.just extractor to be invoked with underlying value in case of just
335
+ * @param op.nothing extractor to be invoked in case of nothing
336
+ * @returns op.just(value) or op.nothing()
337
+ */
338
+ match<R2, R3 = R2>(this: Nothing<R>, op: {
339
+ just: (value: R) => R2;
340
+ nothing: () => R3;
341
+ }): R3;
342
+ match<R2, R3 = R2>(this: Maybe<R>, op: {
343
+ just: (value: R) => R2;
344
+ nothing: () => R3;
345
+ }): R2 | R3;
346
+ /**
347
+ * Maybe pattern matching transformer function
348
+ *
349
+ * Applied to 'just value' returns 'just op.just(value)'
350
+ * Applied to 'nothing' returns 'just op.nothing()'
351
+ *
352
+ * @template R2 just transformer function's return type
353
+ * @template R3 nothing transformer function's return type
354
+ * @param op.just transformer to be invoked with underlying value in case of just
355
+ * @param op.nothing transformer to be invoked in case of nothing
356
+ * @returns 'just op.just(value)' or 'just op.nothing()'
357
+ */
358
+ matchMap<R2, R3 = R2>(this: Nothing<R>, op: {
359
+ just: (value: R) => R2;
360
+ nothing: () => R3;
361
+ }): Just<R3>;
362
+ matchMap<R2, R3 = R2>(this: Maybe<R>, op: {
363
+ just: (value: R) => R2;
364
+ nothing: () => R3;
365
+ }): Just<R2 | R3>;
366
+ /**
367
+ * Maybe composition function
368
+ *
369
+ * Applied to 'just value' returns 'op(value)'
370
+ * Applied to 'nothing' returns self without invoking composition function
371
+ *
372
+ * @template R2 transformer function result's underlying type
373
+ * @param op transformer to be invoked with underlying value
374
+ * @returns 'op(value)' or 'nothing'
375
+ */
376
+ chain<R2>(this: Nothing<R>, op: (value: R) => Just<R2>): Nothing<never>;
377
+ chain<R2>(this: Nothing<R>, op: (value: R) => Nothing<R2>): Nothing<never>;
378
+ chain<R2>(this: Nothing<R>, op: (value: R) => Maybe<R2>): Nothing<never>;
379
+ chain<R2>(this: Maybe<R>, op: (value: R) => Just<R2>): Maybe<R2>;
380
+ chain<R2>(this: Maybe<R>, op: (value: R) => Nothing<R2>): Nothing<never>;
381
+ chain<R2>(this: Maybe<R>, op: (value: R) => Maybe<R2>): Maybe<R2>;
382
+ /**
383
+ * Maybe fallback composition function
384
+ *
385
+ * Applied to 'just value' returns self without invoking composition function
386
+ * Applied to 'nothing' returns op()
387
+ *
388
+ * @template R2 transformer function result's underlying type
389
+ * @param op transformer to be invoked
390
+ * @returns 'just value' or 'op()'
391
+ */
392
+ orChain<R2>(this: Nothing<R>, op: () => Just<R2>): Just<R2>;
393
+ orChain<R2>(this: Nothing<R>, op: () => Nothing<R2>): Nothing<never>;
394
+ orChain<R2>(this: Nothing<R>, op: () => Maybe<R2>): Maybe<R2>;
395
+ orChain<R2>(this: Maybe<R>, op: () => Just<R2>): Just<R | R2>;
396
+ orChain<R2>(this: Maybe<R>, op: () => Nothing<R2>): Maybe<R>;
397
+ orChain<R2>(this: Maybe<R>, op: () => Maybe<R2>): Maybe<R | R2>;
398
+ /**
399
+ * Maybe pattern matching composition function
400
+ *
401
+ * Applied to 'just value' returns 'op.just(value)'
402
+ * Applied to 'nothing' returns op.nothing()
403
+ *
404
+ * @template R2 just transformer function result's underlying type
405
+ * @template R3 nothing transformer function result's underlying type
406
+ * @param op.just transformer to be invoked with underlying value in case of just
407
+ * @param op.nothing transformer to be invoked in case of nothing
408
+ * @returns 'op.just(value)' or 'op.nothing()'
409
+ */
410
+ matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
411
+ just: (value: R) => Just<R2>;
412
+ nothing: () => Just<R3>;
413
+ }): Just<R3>;
414
+ matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
415
+ just: (value: R) => Just<R2>;
416
+ nothing: () => Nothing<R3>;
417
+ }): Nothing<never>;
418
+ matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
419
+ just: (value: R) => Nothing<R2>;
420
+ nothing: () => Just<R3>;
421
+ }): Just<R3>;
422
+ matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
423
+ just: (value: R) => Nothing<R2>;
424
+ nothing: () => Nothing<R3>;
425
+ }): Nothing<never>;
426
+ matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
427
+ just: (value: R) => Just<R2>;
428
+ nothing: () => Maybe<R3>;
429
+ }): Maybe<R3>;
430
+ matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
431
+ just: (value: R) => Nothing<R2>;
432
+ nothing: () => Maybe<R3>;
433
+ }): Maybe<R3>;
434
+ matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
435
+ just: (value: R) => Maybe<R2>;
436
+ nothing: () => Just<R3>;
437
+ }): Just<R3>;
438
+ matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
439
+ just: (value: R) => Maybe<R2>;
440
+ nothing: () => Nothing<R3>;
441
+ }): Nothing<never>;
442
+ matchChain<R2, R3 = R2>(this: Nothing<R>, op: {
443
+ just: (value: R) => Maybe<R2>;
444
+ nothing: () => Maybe<R3>;
445
+ }): Maybe<R3>;
446
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
447
+ just: (value: R) => Just<R2>;
448
+ nothing: () => Just<R3>;
449
+ }): Just<R2 | R3>;
450
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
451
+ just: (value: R) => Just<R2>;
452
+ nothing: () => Nothing<R3>;
453
+ }): Maybe<R2>;
454
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
455
+ just: (value: R) => Nothing<R2>;
456
+ nothing: () => Just<R3>;
457
+ }): Maybe<R3>;
458
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
459
+ just: (value: R) => Nothing<R2>;
460
+ nothing: () => Nothing<R3>;
461
+ }): Nothing<never>;
462
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
463
+ just: (value: R) => Just<R2>;
464
+ nothing: () => Maybe<R3>;
465
+ }): Maybe<R2 | R3>;
466
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
467
+ just: (value: R) => Nothing<R2>;
468
+ nothing: () => Maybe<R3>;
469
+ }): Maybe<R3>;
470
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
471
+ just: (value: R) => Maybe<R2>;
472
+ nothing: () => Just<R3>;
473
+ }): Maybe<R2 | R3>;
474
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
475
+ just: (value: R) => Maybe<R2>;
476
+ nothing: () => Nothing<R3>;
477
+ }): Maybe<R2>;
478
+ matchChain<R2, R3 = R2>(this: Maybe<R>, op: {
479
+ just: (value: R) => Maybe<R2>;
480
+ nothing: () => Maybe<R3>;
481
+ }): Maybe<R2 | R3>;
482
+ /**
483
+ * Maybe type guard for 'just'
484
+ *
485
+ * @returns 'true' in case wrapped value exists (and resolves this type to be 'just')
486
+ *
487
+ * @example
488
+ * ```typescript
489
+ * if (maybe.isJust()) {
490
+ * console.log(maybe.just);
491
+ * }
492
+ * ```
493
+ */
494
+ isJust(): this is Just<R>;
495
+ /**
496
+ * Maybe type guard for 'nothing'
497
+ *
498
+ * @returns 'true' in case wrapped value is 'nothing' (and resolves this type to be 'nothing')
499
+ *
500
+ * @example
501
+ * ```typescript
502
+ * if (maybe.isNothing()) {
503
+ * console.log('nothing');
504
+ * }
505
+ * ```
506
+ */
507
+ isNothing(): this is Nothing<R>;
508
+ /**
509
+ * Wrap Maybe to singleton generator
510
+ *
511
+ * Useful in order to avoid ambiguous yield types
512
+ *
513
+ * @returns generator of Maybe wrapping this
514
+ */
515
+ generator: MaybeGeneratorFunction<[], R, Nothing<R>, R>;
516
+ }
517
+ /**
518
+ * Generic Maybe monad interface
519
+ *
520
+ * As per classic Maybe monad implementation can either contain just a value or contain nothing
521
+ * Used throughout the library to represent optional return type, specifically return type of cancelled tasks
522
+ *
523
+ * @template R underlying value
524
+ */
525
+ export type Maybe<R> = Just<R> | Nothing<R>;
526
+ export declare namespace Just {
527
+ /**
528
+ * Standalone Just value extractor
529
+ *
530
+ * Useful for passing as a function to collection transformers
531
+ *
532
+ * @template R underlying value type
533
+ * @param just Just instance
534
+ * @returns underlying value
535
+ */
536
+ function just<R>(just: Just<R>): R;
537
+ }
538
+ export declare namespace Nothing {}
539
+ /**
540
+ * Maybe generator
541
+ *
542
+ * @template T yielded underlying type
543
+ * @template TT yielded Maybe type
544
+ * @template R returned underlying type
545
+ */
546
+ export type MaybeGenerator<T, TT extends Maybe<T>, R> = Generator<TT, R, T>;
547
+ /**
548
+ * Function returning Maybe generator (generator function)
549
+ *
550
+ * @template A argument types
551
+ * @template T yielded underlying type
552
+ * @template TT yielded Maybe type
553
+ * @template R returned underlying type
554
+ */
555
+ export type MaybeGeneratorFunction<A extends unknown[], T, TT extends Maybe<T>, R> = (...args: A) => MaybeGenerator<T, TT, R>;
556
+ export declare namespace Maybe {
557
+ /**
558
+ * Non-empty monad constructor
559
+ *
560
+ * @template R underlying type
561
+ * @param value underlying value
562
+ * @returns 'just value'
563
+ */
564
+ function just<R>(value: R): Just<R>;
565
+ /**
566
+ * Empty monad constructor
567
+ *
568
+ * @returns 'nothing'
569
+ */
570
+ function nothing<R = never>(): Nothing<R>;
571
+ /**
572
+ * Maybe constructor from optional value
573
+ *
574
+ * Resolves to 'nothing' in case of undefined value and 'just value' otherwise
575
+ *
576
+ * @template R underlying type
577
+ * @param value possibly undefined value
578
+ * @returns either 'just value' or 'nothing'
579
+ */
580
+ function fromOptional(value: undefined): Nothing<never>;
581
+ function fromOptional<R>(value: Exclude<R, undefined>): Just<R>;
582
+ function fromOptional<R>(value: R | undefined): Maybe<R>;
583
+ /**
584
+ * Maybe constructor from optional or nullable value
585
+ *
586
+ * Resolves to 'nothing' in case of undefined or null value and 'just value' otherwise
587
+ *
588
+ * @template R underlying type
589
+ * @param value possibly undefined or null value
590
+ * @returns either 'just value' or 'nothing'
591
+ */
592
+ function fromNullable(value: undefined): Nothing<never>;
593
+ function fromNullable(value: null): Nothing<never>;
594
+ function fromNullable<R>(value: Exclude<R, null | undefined>): Just<R>;
595
+ function fromNullable<R>(value: R | null | undefined): Maybe<R>;
596
+ /**
597
+ * Standalone Maybe value extractor
598
+ *
599
+ * Applied to 'just value' returns underlying value without using fallback
600
+ * Applied to 'nothing' returns fallback
601
+ *
602
+ * @template R underlying value type
603
+ * @template R2 fallback value type
604
+ * @param maybe Maybe instance
605
+ * @param fallback value to be returned in case of 'nothing'
606
+ * @returns underlying value or fallback
607
+ */
608
+ function fromMaybe<R>(maybe: Just<R>, fallback: unknown): R;
609
+ function fromMaybe<R, R2>(maybe: Nothing<R>, fallback: R2): R2;
610
+ function fromMaybe<R, R2>(maybe: Maybe<R>, fallback: R2): R | R2;
611
+ /**
612
+ * Standalone type guard for 'just'
613
+ *
614
+ * Useful for passing as a predicate to collection transformers
615
+ *
616
+ * @template R underlying value type
617
+ * @param maybe Maybe instance
618
+ * @returns 'true' in case wrapped value exists (and resolves argument type to be 'just')
619
+ */
620
+ function isJust<R>(maybe: Maybe<R>): maybe is Just<R>;
621
+ /**
622
+ * Standalone type guard for 'nothing'
623
+ *
624
+ * Useful for passing as a predicate to collection transformers
625
+ *
626
+ * @template R underlying value type
627
+ * @param maybe Maybe instance
628
+ * @returns 'true' in case wrapped value is 'nothing' (and resolves argument type to be 'nothing')
629
+ */
630
+ function isNothing<R>(maybe: Maybe<R>): maybe is Nothing<R>;
631
+ /**
632
+ * Standalone list predicate
633
+ *
634
+ * @template R underlying value type
635
+ * @param maybes a list of Maybe
636
+ * @returns true in case every list element is Just
637
+ */
638
+ function everyJust<R>(maybes: Maybe<R>[]): maybes is Just<R>[];
639
+ /**
640
+ * Standalone list predicate
641
+ *
642
+ * @template R underlying value type
643
+ * @param maybes a list of Maybe
644
+ * @returns true in case at least one list element is Just
645
+ */
646
+ function someJust<R>(maybes: Maybe<R>[]): boolean;
647
+ /**
648
+ * Standalone list predicate
649
+ *
650
+ * @template R underlying value type
651
+ * @param maybes a list of Maybe
652
+ * @returns true in case every list element is Nothing
653
+ */
654
+ function everyNothing<R>(maybes: Maybe<R>[]): maybes is Nothing<never>[];
655
+ /**
656
+ * Standalone list predicate
657
+ *
658
+ * @template R underlying value type
659
+ * @param maybes a list of Maybe
660
+ * @returns true in case at least one list element is Nothing
661
+ */
662
+ function someNothing<R>(maybes: Maybe<R>[]): boolean;
663
+ /**
664
+ * Create compound Maybe from generator function
665
+ *
666
+ * Applying yield to a Maybe within the generator function unwraps the Maybe and returns underlying value in case of success
667
+ * However the convenient option for typescript is to use ```yield* maybe.generator()``` as otherwise one may have to deal with union types
668
+ *
669
+ * @template T yielded underlying type
670
+ * @template TT yielded Maybe type
671
+ * @template R returned underlying type
672
+ * @param maybeGeneratorFunction Maybe generator function
673
+ * @returns Just wrapping the result of generator function or Nothing
674
+ */
675
+ function generate<T, TT extends Maybe<T>, R>(maybeGeneratorFunction: MaybeGeneratorFunction<[], T, TT, R>): Maybe<R>;
676
+ }
677
+ //#endregion
678
+ //#region src/either.d.ts
679
+ /**
680
+ * Right (correct) value of type R
681
+ *
682
+ * Either data type specialization representing a correct value
683
+ *
684
+ * @template R underlying value type
685
+ * @template L underlying error type (needed for type merging, in fact always considered to be never)
686
+ */
687
+ export interface Right<R, L> {
688
+ readonly right: R;
689
+ /**
690
+ * Either peeker function
691
+ *
692
+ * Applied to 'right value' returns self invoking op(value) in process
693
+ * Applied to 'left error' returns self without invoking callback
694
+ *
695
+ * @param op function to be invoked with underlying value
696
+ * @returns self
697
+ */
698
+ tap(op: (value: R) => void): this;
699
+ /**
700
+ * Either fallback peeker function
701
+ *
702
+ * Applied to 'right value' returns self without invoking callback
703
+ * Applied to 'left error' returns self invoking op(error) in process
704
+ *
705
+ * @param op function to be invoked with underlying error
706
+ * @returns self
707
+ */
708
+ orTap(op: (error: L) => void): this;
709
+ /**
710
+ * Either pattern matching peeker function
711
+ *
712
+ * Applied to 'right value' returns self invoking op.right(value) in process
713
+ * Applied to 'left error' returns self invoking op.left(error) in process
714
+ *
715
+ * @param op.right function to be invoked with underlying value in case of 'right'
716
+ * @param op.left function to be invoked with underlying error in case of 'left'
717
+ * @returns self
718
+ */
719
+ matchTap(op: {
720
+ right: (value: R) => void;
721
+ left: (error: L) => void;
722
+ }): this;
723
+ /**
724
+ * Either transformer function
725
+ *
726
+ * Applied to 'right value' returns 'right op(value)'
727
+ * Applied to 'left error' returns self without invoking transformer
728
+ *
729
+ * @template R2 transformer function's return type
730
+ * @param op transformer to be invoked with underlying value
731
+ * @returns 'right op(value)' or 'left error'
732
+ */
733
+ map<R2>(this: Right<R, L>, op: (value: R) => R2): Right<R2, never>;
734
+ map<R2>(this: Either<R, L>, op: (value: R) => R2): Either<R2, L>;
735
+ /**
736
+ * Either fallback transformer function
737
+ *
738
+ * Applied to 'right value' returns self without invoking transformer
739
+ * Applied to 'left error' returns 'right op(error)'
740
+ *
741
+ * @template R2 transformer function's return type
742
+ * @param op transformer to be invoked with underlying error
743
+ * @returns 'right value' or 'right op(error)'
744
+ */
745
+ orMap<R2>(this: Right<R, L>, op: (error: L) => R2): Right<R, never>;
746
+ orMap<R2>(this: Either<R, L>, op: (error: L) => R2): Right<R | R2, never>;
747
+ /**
748
+ * Either pattern matching extractor function
749
+ *
750
+ * Applied to 'right value' returns op.right(value)
751
+ * Applied to 'left error' returns op.left(error)
752
+ *
753
+ * @template R2 right extractor function's return type
754
+ * @template R3 left extractor function's return type
755
+ * @param op.right extractor to be invoked with underlying value in case of 'right'
756
+ * @param op.left extractor to be invoked with underlying error in case of 'left'
757
+ * @returns op.right(value) or op.left(error)
758
+ */
759
+ match<R2, R3 = R2>(this: Right<R, L>, op: {
760
+ right: (value: R) => R2;
761
+ left: (error: L) => R3;
762
+ }): R2;
763
+ match<R2, R3 = R2>(this: Either<R, L>, op: {
764
+ right: (value: R) => R2;
765
+ left: (error: L) => R3;
766
+ }): R2 | R3;
767
+ /**
768
+ * Either pattern matching transformer function
769
+ *
770
+ * Applied to 'right value' returns 'right op.right(value)'
771
+ * Applied to 'left error' returns 'right op.left(error)'
772
+ *
773
+ * @template R2 right transformer function's return type
774
+ * @template R3 left transformer function's return type
775
+ * @param op.right transformer to be invoked with underlying value in case of 'right'
776
+ * @param op.left transformer to be invoked with underlying error in case of 'left'
777
+ * @returns 'right op.right(value)' or 'right op.left(error)'
778
+ */
779
+ matchMap<R2, R3 = R2>(this: Right<R, L>, op: {
780
+ right: (value: R) => R2;
781
+ left: (error: L) => R3;
782
+ }): Right<R2, never>;
783
+ matchMap<R2, R3 = R2>(this: Either<R, L>, op: {
784
+ right: (value: R) => R2;
785
+ left: (error: L) => R3;
786
+ }): Right<R2 | R3, never>;
787
+ /**
788
+ * Either composition function
789
+ *
790
+ * Applied to 'right value' returns 'op(value)'
791
+ * Applied to 'left error' returns self without invoking composition function
792
+ *
793
+ * @template R2 transformer function result's underlying value type
794
+ * @template L2 transformer function result's underlying error type
795
+ * @param op transformer to be invoked with underlying value
796
+ * @returns 'op(value)' or 'left error'
797
+ */
798
+ chain<R2, L2>(this: Right<R, L>, op: (value: R) => Right<R2, L2>): Right<R2, never>;
799
+ chain<R2, L2>(this: Right<R, L>, op: (value: R) => Left<R2, L2>): Left<never, L2>;
800
+ chain<R2, L2>(this: Right<R, L>, op: (value: R) => Either<R2, L2>): Either<R2, L2>;
801
+ chain<R2, L2>(this: Either<R, L>, op: (value: R) => Right<R2, L2>): Either<R2, L>;
802
+ chain<R2, L2>(this: Either<R, L>, op: (value: R) => Left<R2, L2>): Left<never, L | L2>;
803
+ chain<R2, L2>(this: Either<R, L>, op: (value: R) => Either<R2, L2>): Either<R2, L | L2>;
804
+ /**
805
+ * Either fallback composition function
806
+ *
807
+ * Applied to 'right value' returns self without invoking composition function
808
+ * Applied to 'left error' returns 'op(error)'
809
+ *
810
+ * @template R2 transformer function result's underlying value type
811
+ * @template L2 transformer function result's underlying error type
812
+ * @param op transformer to be invoked with underlying error
813
+ * @returns 'right value' or 'op(error)'
814
+ */
815
+ orChain<R2, L2>(this: Right<R, L>, op: (error: L) => Right<R2, L2>): Right<R, never>;
816
+ orChain<R2, L2>(this: Right<R, L>, op: (error: L) => Left<R2, L2>): Right<R, never>;
817
+ orChain<R2, L2>(this: Right<R, L>, op: (error: L) => Either<R2, L2>): Right<R, never>;
818
+ orChain<R2, L2>(this: Either<R, L>, op: (error: L) => Right<R2, L2>): Right<R | R2, never>;
819
+ orChain<R2, L2>(this: Either<R, L>, op: (error: L) => Left<R2, L2>): Either<R, L2>;
820
+ orChain<R2, L2>(this: Either<R, L>, op: (error: L) => Either<R2, L2>): Either<R | R2, L2>;
821
+ /**
822
+ * Either pattern matching composition function
823
+ *
824
+ * Applied to 'right value' returns 'op.right(value)'
825
+ * Applied to 'left error' returns 'op.left(error)'
826
+ *
827
+ * @template R2 right transformer function result's underlying value type
828
+ * @template L2 right transformer function result's underlying error type
829
+ * @template R3 left transformer function result's underlying value type
830
+ * @template L3 left transformer function result's underlying error type
831
+ * @param op.right transformer to be invoked with underlying value in case of 'right'
832
+ * @param op.left transformer to be invoked with underlying error in case of 'left'
833
+ * @returns 'op.right(value)' or 'op.left(error)'
834
+ */
835
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
836
+ right: (value: R) => Right<R2, L2>;
837
+ left: (error: L) => Right<R3, L3>;
838
+ }): Right<R2, never>;
839
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
840
+ right: (value: R) => Left<R2, L2>;
841
+ left: (error: L) => Right<R3, L3>;
842
+ }): Left<never, L2>;
843
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
844
+ right: (value: R) => Right<R2, L2>;
845
+ left: (error: L) => Left<R3, L3>;
846
+ }): Right<R2, never>;
847
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
848
+ right: (value: R) => Left<R2, L2>;
849
+ left: (error: L) => Left<R3, L3>;
850
+ }): Left<never, L2>;
851
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
852
+ right: (value: R) => Either<R2, L2>;
853
+ left: (error: L) => Right<R3, L3>;
854
+ }): Either<R2, L2>;
855
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
856
+ right: (value: R) => Either<R2, L2>;
857
+ left: (error: L) => Left<R3, L3>;
858
+ }): Either<R2, L2>;
859
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
860
+ right: (value: R) => Right<R2, L2>;
861
+ left: (error: L) => Either<R3, L3>;
862
+ }): Right<R2, never>;
863
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
864
+ right: (value: R) => Left<R2, L2>;
865
+ left: (error: L) => Either<R3, L3>;
866
+ }): Left<never, L2>;
867
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Right<R, L>, op: {
868
+ right: (value: R) => Either<R2, L2>;
869
+ left: (error: L) => Either<R3, L3>;
870
+ }): Either<R2, L2>;
871
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
872
+ right: (value: R) => Right<R2, L2>;
873
+ left: (error: L) => Right<R3, L3>;
874
+ }): Right<R2 | R3, never>;
875
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
876
+ right: (value: R) => Left<R2, L2>;
877
+ left: (error: L) => Right<R3, L3>;
878
+ }): Either<R3, L2>;
879
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
880
+ right: (value: R) => Right<R2, L2>;
881
+ left: (error: L) => Left<R3, L3>;
882
+ }): Either<R2, L3>;
883
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
884
+ right: (value: R) => Left<R2, L2>;
885
+ left: (error: L) => Left<R3, L3>;
886
+ }): Left<never, L2 | L3>;
887
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
888
+ right: (value: R) => Either<R2, L2>;
889
+ left: (error: L) => Right<R3, L3>;
890
+ }): Either<R2 | R3, L2>;
891
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
892
+ right: (value: R) => Either<R2, L2>;
893
+ left: (error: L) => Left<R3, L3>;
894
+ }): Either<R2, L2 | L3>;
895
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
896
+ right: (value: R) => Right<R2, L2>;
897
+ left: (error: L) => Either<R3, L3>;
898
+ }): Either<R2 | R3, L3>;
899
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
900
+ right: (value: R) => Left<R2, L2>;
901
+ left: (error: L) => Either<R3, L3>;
902
+ }): Either<R3, L2 | L3>;
903
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
904
+ right: (value: R) => Either<R2, L2>;
905
+ left: (error: L) => Either<R3, L3>;
906
+ }): Either<R2 | R3, L2 | L3>;
907
+ /**
908
+ * Either type guard for 'right'
909
+ *
910
+ * @returns 'true' in case this is 'right value' (and resolves type to be 'right')
911
+ *
912
+ * @example
913
+ * ```typescript
914
+ * if (either.isRight()) {
915
+ * console.log(either.right)
916
+ * }
917
+ * ```
918
+ */
919
+ isRight(): this is Right<R, L>;
920
+ /**
921
+ * Either type guard for 'left'
922
+ *
923
+ * @returns 'true' in case this is 'left error' (and resolves type to be 'left')
924
+ *
925
+ * @example
926
+ * ```typescript
927
+ * if (either.isLeft()) {
928
+ * console.error(either.left)
929
+ * }
930
+ * ```
931
+ */
932
+ isLeft(): this is Left<R, L>;
933
+ /**
934
+ * Wrap Either to singleton generator
935
+ *
936
+ * Useful in order to avoid ambiguous yield types
937
+ *
938
+ * @returns generator of Either wrapping this
939
+ */
940
+ generator: EitherGeneratorFunction<[], R, Right<R, L>, R>;
941
+ }
942
+ /**
943
+ * Left (erroneous) value of type L
944
+ *
945
+ * Either data type specialization representing an erroneous value
946
+ *
947
+ * @template R underlying value type (needed for type merging, in fact always considered to be never)
948
+ * @template L underlying error type
949
+ */
950
+ export interface Left<R, L> {
951
+ readonly left: L;
952
+ /**
953
+ * Either peeker function
954
+ *
955
+ * Applied to 'right value' returns self invoking op(value) in process
956
+ * Applied to 'left error' returns self without invoking callback
957
+ *
958
+ * @param op function to be invoked with underlying value
959
+ * @returns self
960
+ */
961
+ tap(op: (value: R) => void): this;
962
+ /**
963
+ * Either fallback peeker function
964
+ *
965
+ * Applied to 'right value' returns self without invoking callback
966
+ * Applied to 'left error' returns self invoking op(error) in process
967
+ *
968
+ * @param op function to be invoked with underlying error
969
+ * @returns self
970
+ */
971
+ orTap(op: (error: L) => void): this;
972
+ /**
973
+ * Either pattern matching peeker function
974
+ *
975
+ * Applied to 'right value' returns self invoking op.right(value) in process
976
+ * Applied to 'left error' returns self invoking op.left(error) in process
977
+ *
978
+ * @param op.right function to be invoked with underlying value in case of 'right'
979
+ * @param op.left function to be invoked with underlying error in case of 'left'
980
+ * @returns self
981
+ */
982
+ matchTap(op: {
983
+ right: (value: R) => void;
984
+ left: (error: L) => void;
985
+ }): this;
986
+ /**
987
+ * Either transformer function
988
+ *
989
+ * Applied to 'right value' returns 'right op(value)'
990
+ * Applied to 'left error' returns self without invoking transformer
991
+ *
992
+ * @template R2 transformer function's return type
993
+ * @param op transformer to be invoked with underlying value
994
+ * @returns 'right op(value)' or 'left error'
995
+ */
996
+ map<R2>(this: Left<R, L>, op: (value: R) => R2): Left<never, L>;
997
+ map<R2>(this: Either<R, L>, op: (value: R) => R2): Either<R2, L>;
998
+ /**
999
+ * Either fallback transformer function
1000
+ *
1001
+ * Applied to 'right value' returns self without invoking transformer
1002
+ * Applied to 'left error' returns 'right op(error)'
1003
+ *
1004
+ * @template R2 transformer function's return type
1005
+ * @param op transformer to be invoked with underlying error
1006
+ * @returns 'right value' or 'right op(error)'
1007
+ */
1008
+ orMap<R2>(this: Left<R, L>, op: (error: L) => R2): Right<R2, never>;
1009
+ orMap<R2>(this: Either<R, L>, op: (error: L) => R2): Right<R | R2, never>;
1010
+ /**
1011
+ * Either pattern matching extractor function
1012
+ *
1013
+ * Applied to 'right value' returns op.right(value)
1014
+ * Applied to 'left error' returns op.left(error)
1015
+ *
1016
+ * @template R2 right extractor function's return type
1017
+ * @template R3 left extractor function's return type
1018
+ * @param op.right extractor to be invoked with underlying value in case of 'right'
1019
+ * @param op.left extractor to be invoked with underlying error in case of 'left'
1020
+ * @returns op.right(value) or op.left(error)
1021
+ */
1022
+ match<R2, R3 = R2>(this: Left<R, L>, op: {
1023
+ right: (value: R) => R2;
1024
+ left: (error: L) => R3;
1025
+ }): R3;
1026
+ match<R2, R3 = R2>(this: Either<R, L>, op: {
1027
+ right: (value: R) => R2;
1028
+ left: (error: L) => R3;
1029
+ }): R2 | R3;
1030
+ /**
1031
+ * Either pattern matching transformer function
1032
+ *
1033
+ * Applied to 'right value' returns 'right op.right(value)'
1034
+ * Applied to 'left error' returns 'right op.left(error)'
1035
+ *
1036
+ * @template R2 right transformer function's return type
1037
+ * @template R3 left transformer function's return type
1038
+ * @param op.right transformer to be invoked with underlying value in case of 'right'
1039
+ * @param op.left transformer to be invoked with underlying error in case of 'left'
1040
+ * @returns 'right op.right(value)' or 'right op.left(error)'
1041
+ */
1042
+ matchMap<R2, R3 = R2>(this: Left<R, L>, op: {
1043
+ right: (value: R) => R2;
1044
+ left: (error: L) => R3;
1045
+ }): Right<R3, never>;
1046
+ matchMap<R2, R3 = R2>(this: Either<R, L>, op: {
1047
+ right: (value: R) => R2;
1048
+ left: (error: L) => R3;
1049
+ }): Right<R2 | R3, never>;
1050
+ /**
1051
+ * Either composition function
1052
+ *
1053
+ * Applied to 'right value' returns 'op(value)'
1054
+ * Applied to 'left error' returns self without invoking composition function
1055
+ *
1056
+ * @template R2 transformer function result's underlying value type
1057
+ * @template L2 transformer function result's underlying error type
1058
+ * @param op transformer to be invoked with underlying value
1059
+ * @returns 'op(value)' or 'left error'
1060
+ */
1061
+ chain<R2, L2>(this: Left<R, L>, op: (value: R) => Right<R2, L2>): Left<never, L>;
1062
+ chain<R2, L2>(this: Left<R, L>, op: (value: R) => Left<R2, L2>): Left<never, L>;
1063
+ chain<R2, L2>(this: Left<R, L>, op: (value: R) => Either<R2, L2>): Left<never, L>;
1064
+ chain<R2, L2>(this: Either<R, L>, op: (value: R) => Right<R2, L2>): Either<R2, L>;
1065
+ chain<R2, L2>(this: Either<R, L>, op: (value: R) => Left<R2, L2>): Left<never, L | L2>;
1066
+ chain<R2, L2>(this: Either<R, L>, op: (value: R) => Either<R2, L2>): Either<R2, L | L2>;
1067
+ /**
1068
+ * Either fallback composition function
1069
+ *
1070
+ * Applied to 'right value' returns self without invoking composition function
1071
+ * Applied to 'left error' returns 'op(error)'
1072
+ *
1073
+ * @template R2 transformer function result's underlying value type
1074
+ * @template L2 transformer function result's underlying error type
1075
+ * @param op transformer to be invoked with underlying error
1076
+ * @returns 'right value' or 'op(error)'
1077
+ */
1078
+ orChain<R2, L2>(this: Left<R, L>, op: (error: L) => Right<R2, L2>): Right<R2, never>;
1079
+ orChain<R2, L2>(this: Left<R, L>, op: (error: L) => Left<R2, L2>): Left<never, L2>;
1080
+ orChain<R2, L2>(this: Left<R, L>, op: (error: L) => Either<R2, L2>): Either<R2, L2>;
1081
+ orChain<R2, L2>(this: Either<R, L>, op: (error: L) => Right<R2, L2>): Right<R | R2, never>;
1082
+ orChain<R2, L2>(this: Either<R, L>, op: (error: L) => Left<R2, L2>): Either<R, L2>;
1083
+ orChain<R2, L2>(this: Either<R, L>, op: (error: L) => Either<R2, L2>): Either<R | R2, L2>;
1084
+ /**
1085
+ * Either pattern matching composition function
1086
+ *
1087
+ * Applied to 'right value' returns 'op.right(value)'
1088
+ * Applied to 'left error' returns 'op.left(error)'
1089
+ *
1090
+ * @template R2 right transformer function result's underlying value type
1091
+ * @template L2 right transformer function result's underlying error type
1092
+ * @template R3 left transformer function result's underlying value type
1093
+ * @template L3 left transformer function result's underlying error type
1094
+ * @param op.right transformer to be invoked with underlying value in case of 'right'
1095
+ * @param op.left transformer to be invoked with underlying error in case of 'left'
1096
+ * @returns 'op.right(value)' or 'op.left(error)'
1097
+ */
1098
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
1099
+ right: (value: R) => Right<R2, L2>;
1100
+ left: (error: L) => Right<R3, L3>;
1101
+ }): Right<R3, never>;
1102
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
1103
+ right: (value: R) => Left<R2, L2>;
1104
+ left: (error: L) => Right<R3, L3>;
1105
+ }): Right<R3, never>;
1106
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
1107
+ right: (value: R) => Right<R2, L2>;
1108
+ left: (error: L) => Left<R3, L3>;
1109
+ }): Left<never, L3>;
1110
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
1111
+ right: (value: R) => Left<R2, L2>;
1112
+ left: (error: L) => Left<R3, L3>;
1113
+ }): Left<never, L3>;
1114
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
1115
+ right: (value: R) => Either<R2, L2>;
1116
+ left: (error: L) => Right<R3, L3>;
1117
+ }): Right<R3, never>;
1118
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
1119
+ right: (value: R) => Either<R2, L2>;
1120
+ left: (error: L) => Left<R3, L3>;
1121
+ }): Left<never, L3>;
1122
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
1123
+ right: (value: R) => Right<R2, L2>;
1124
+ left: (error: L) => Either<R3, L3>;
1125
+ }): Either<R3, L3>;
1126
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
1127
+ right: (value: R) => Left<R2, L2>;
1128
+ left: (error: L) => Either<R3, L3>;
1129
+ }): Either<R3, L3>;
1130
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Left<R, L>, op: {
1131
+ right: (value: R) => Either<R2, L2>;
1132
+ left: (error: L) => Either<R3, L3>;
1133
+ }): Either<R3, L3>;
1134
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
1135
+ right: (value: R) => Right<R2, L2>;
1136
+ left: (error: L) => Right<R3, L3>;
1137
+ }): Right<R2 | R3, never>;
1138
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
1139
+ right: (value: R) => Left<R2, L2>;
1140
+ left: (error: L) => Right<R3, L3>;
1141
+ }): Either<R3, L2>;
1142
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
1143
+ right: (value: R) => Right<R2, L2>;
1144
+ left: (error: L) => Left<R3, L3>;
1145
+ }): Either<R2, L3>;
1146
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
1147
+ right: (value: R) => Left<R2, L2>;
1148
+ left: (error: L) => Left<R3, L3>;
1149
+ }): Left<never, L2 | L3>;
1150
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
1151
+ right: (value: R) => Either<R2, L2>;
1152
+ left: (error: L) => Right<R3, L3>;
1153
+ }): Either<R2 | R3, L2>;
1154
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
1155
+ right: (value: R) => Either<R2, L2>;
1156
+ left: (error: L) => Left<R3, L3>;
1157
+ }): Either<R2, L2 | L3>;
1158
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
1159
+ right: (value: R) => Right<R2, L2>;
1160
+ left: (error: L) => Either<R3, L3>;
1161
+ }): Either<R2 | R3, L3>;
1162
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
1163
+ right: (value: R) => Left<R2, L2>;
1164
+ left: (error: L) => Either<R3, L3>;
1165
+ }): Either<R3, L2 | L3>;
1166
+ matchChain<R2, L2, R3 = R2, L3 = L2>(this: Either<R, L>, op: {
1167
+ right: (value: R) => Either<R2, L2>;
1168
+ left: (error: L) => Either<R3, L3>;
1169
+ }): Either<R2 | R3, L2 | L3>;
1170
+ /**
1171
+ * Either type guard for 'right'
1172
+ *
1173
+ * @returns 'true' in case this is 'right value' (and resolves type to be 'right')
1174
+ *
1175
+ * @example
1176
+ * ```typescript
1177
+ * if (either.isRight()) {
1178
+ * console.log(either.right)
1179
+ * }
1180
+ * ```
1181
+ */
1182
+ isRight(): this is Right<R, L>;
1183
+ /**
1184
+ * Either type guard for 'left'
1185
+ *
1186
+ * @returns 'true' in case this is 'left error' (and resolves type to be 'left')
1187
+ *
1188
+ * @example
1189
+ * ```typescript
1190
+ * if (either.isLeft()) {
1191
+ * console.error(either.left)
1192
+ * }
1193
+ * ```
1194
+ */
1195
+ isLeft(): this is Left<R, L>;
1196
+ /**
1197
+ * Wrap Either to singleton generator
1198
+ *
1199
+ * Useful in order to avoid ambiguous yield types
1200
+ *
1201
+ * @returns generator of Either wrapping this
1202
+ */
1203
+ generator: EitherGeneratorFunction<[], R, Left<R, L>, R>;
1204
+ }
1205
+ /**
1206
+ * Either data type: either Right value of type R or Left value of type L
1207
+ *
1208
+ * As per classic Either monad implementation can either contain a right (correct) value or a left (erroneous) value
1209
+ * Used throughout the library to represent the result of failable operations, namely failed tasks
1210
+ *
1211
+ * @template R underlying value type
1212
+ * @template L underlying error type
1213
+ */
1214
+ export type Either<R, L> = Right<R, L> | Left<R, L>;
1215
+ export declare namespace Right {
1216
+ /**
1217
+ * Standalone Right value extractor
1218
+ *
1219
+ * Useful for passing as a function to collection transformers
1220
+ *
1221
+ * @template R underlying value type
1222
+ * @template L underlying error type
1223
+ * @param right Right instance
1224
+ * @returns underlying value
1225
+ */
1226
+ function right<R, L>(right: Right<R, L>): R;
1227
+ }
1228
+ export declare namespace Left {
1229
+ /**
1230
+ * Standalone Left error extractor
1231
+ *
1232
+ * Useful for passing as a function to collection transformers
1233
+ *
1234
+ * @template R underlying value type
1235
+ * @template L underlying error type
1236
+ * @param left Left instance
1237
+ * @returns underlying error
1238
+ */
1239
+ function left<R, L>(left: Left<R, L>): L;
1240
+ }
1241
+ /**
1242
+ * Either generator
1243
+ *
1244
+ * @template T yielded underlying type
1245
+ * @template TT yielded Either type
1246
+ * @template R returned underlying type
1247
+ */
1248
+ export type EitherGenerator<T, TT extends Either<T, any>, R> = Generator<TT, R, T>;
1249
+ /**
1250
+ * Function returning Either generator (generator function)
1251
+ *
1252
+ * @template A argument types
1253
+ * @template T yielded underlying type
1254
+ * @template TT yielded Either type
1255
+ * @template R returned underlying type
1256
+ */
1257
+ export type EitherGeneratorFunction<A extends unknown[], T, TT extends Either<T, any>, R> = (...args: A) => EitherGenerator<T, TT, R>;
1258
+ export declare namespace Either {
1259
+ /**
1260
+ * Right monad constructor
1261
+ *
1262
+ * @template R underlying value type
1263
+ * @param value underlying value
1264
+ * @returns 'right value'
1265
+ */
1266
+ function right<R>(value: R): Right<R, never>;
1267
+ /**
1268
+ * Left monad constructor
1269
+ *
1270
+ * @template L underlying error type
1271
+ * @param error underlying error
1272
+ * @returns 'left error'
1273
+ */
1274
+ function left<L>(error: L): Left<never, L>;
1275
+ /**
1276
+ * Either constructor from optional value
1277
+ *
1278
+ * Resolves to 'left error' in case of undefined value and 'right value' otherwise
1279
+ *
1280
+ * @template R underlying value type
1281
+ * @template L underlying error type
1282
+ * @param value possibly undefined value
1283
+ * @param error error to use in case of undefined value
1284
+ * @returns either 'right value' or 'left error'
1285
+ */
1286
+ function fromOptional<L>(value: undefined, error: L): Left<never, L>;
1287
+ function fromOptional<R>(value: Exclude<R, undefined>, error: unknown): Right<R, never>;
1288
+ function fromOptional<R, L>(value: R | undefined, error: L): Either<R, L>;
1289
+ /**
1290
+ * Either constructor from optional or nullable value
1291
+ *
1292
+ * Resolves to 'left error' in case of undefined or null value and 'right value' otherwise
1293
+ *
1294
+ * @template R underlying value type
1295
+ * @template L underlying error type
1296
+ * @param value possibly undefined or null value
1297
+ * @param error error to use in case of undefined or null value
1298
+ * @returns either 'right value' or 'left error'
1299
+ */
1300
+ function fromNullable<L>(value: undefined, error: L): Left<never, L>;
1301
+ function fromNullable<L>(value: null, error: L): Left<never, L>;
1302
+ function fromNullable<R>(value: Exclude<R, null | undefined>, error: unknown): Right<R, never>;
1303
+ function fromNullable<R, L>(value: R | null | undefined, error: L): Either<R, L>;
1304
+ /**
1305
+ * Standalone Either value extractor
1306
+ *
1307
+ * Applied to 'right value' returns underlying value without using fallback
1308
+ * Applied to 'left error' returns fallback
1309
+ *
1310
+ * @template R underlying value type
1311
+ * @template L underlying error type
1312
+ * @template R2 fallback value type
1313
+ * @param either Either instance
1314
+ * @param fallback value to be returned in case of 'left error'
1315
+ * @returns underlying value or fallback
1316
+ */
1317
+ function fromEither<R, L>(either: Right<R, L>, fallback: unknown): R;
1318
+ function fromEither<R, L, R2>(either: Left<R, L>, fallback: R2): R2;
1319
+ function fromEither<R, L, R2>(either: Either<R, L>, fallback: R2): R | R2;
1320
+ /**
1321
+ * Standalone type guard for 'right'
1322
+ *
1323
+ * Useful for passing as a predicate to collection transformers
1324
+ *
1325
+ * @template R underlying value type
1326
+ * @template L underlying error type
1327
+ * @param either Either instance
1328
+ * @returns 'true' in case argument is 'right value' (and resolves type to be 'right')
1329
+ */
1330
+ function isRight<R, L>(either: Either<R, L>): either is Right<R, never>;
1331
+ /**
1332
+ * Standalone type guard for 'left'
1333
+ *
1334
+ * Useful for passing as a predicate to collection transformers
1335
+ *
1336
+ * @template R underlying value type
1337
+ * @template L underlying error type
1338
+ * @param either Either instance
1339
+ * @returns 'true' in case argument is 'left error' (and resolves type to be 'left')
1340
+ */
1341
+ function isLeft<R, L>(either: Either<R, L>): either is Left<never, L>;
1342
+ /**
1343
+ * Standalone list predicate
1344
+ *
1345
+ * @template R underlying value type
1346
+ * @template L underlying error type
1347
+ * @param eithers a list of Either
1348
+ * @returns true in case every list element is Right
1349
+ */
1350
+ function everyRight<R, L>(eithers: Either<R, L>[]): eithers is Right<R, never>[];
1351
+ /**
1352
+ * Standalone list predicate
1353
+ *
1354
+ * @template R underlying value type
1355
+ * @template L underlying error type
1356
+ * @param eithers a list of Either
1357
+ * @returns true in case at least one list element is Right
1358
+ */
1359
+ function someRight<R, L>(eithers: Either<R, L>[]): boolean;
1360
+ /**
1361
+ * Standalone list predicate
1362
+ *
1363
+ * @template R underlying value type
1364
+ * @template L underlying error type
1365
+ * @param eithers a list of Either
1366
+ * @returns true in case every list element is Left
1367
+ */
1368
+ function everyLeft<R, L>(eithers: Either<R, L>[]): eithers is Left<never, L>[];
1369
+ /**
1370
+ * Standalone list predicate
1371
+ *
1372
+ * @template R underlying value type
1373
+ * @template L underlying error type
1374
+ * @param eithers a list of Either
1375
+ * @returns true in case at least one list element is Left
1376
+ */
1377
+ function someLeft<R, L>(eithers: Either<R, L>[]): boolean;
1378
+ /**
1379
+ * Create compound Either from generator function
1380
+ *
1381
+ * Applying yield to an Either within the generator function unwraps the Either and returns underlying value in case of success
1382
+ * However the convenient option for typescript is to use ```yield* either.generator()``` as otherwise one may have to deal with union types
1383
+ *
1384
+ * @template T yielded underlying type
1385
+ * @template TT yielded Either type
1386
+ * @template R returned underlying type
1387
+ * @param eitherGeneratorFunction Either generator function
1388
+ * @returns Right wrapping the result of generator function or Left
1389
+ */
1390
+ function generate<T, TT extends Either<T, any>, R>(eitherGeneratorFunction: EitherGeneratorFunction<[], T, TT, R>): Either<R, any>;
1391
+ }
1392
+ //#endregion
1393
+ //#region src/task.d.ts
1394
+ /**
1395
+ * Shortcut for monadic Either type, where erroneous value is of type any
1396
+ *
1397
+ * @template R underlying type
1398
+ */
1399
+ export type Rejectable<R> = Either<R, any>;
1400
+ /**
1401
+ * Shortcut for underlying task result type
1402
+ *
1403
+ * @template R underlying type
1404
+ */
1405
+ export type Cancelable<R> = Maybe<Rejectable<R>>;
1406
+ /**
1407
+ * Function returning Task
1408
+ *
1409
+ * @template A argument types
1410
+ * @template R returned task resolve type
1411
+ */
1412
+ export type TaskFunction<A extends unknown[], R> = (...args: A) => Task<R>;
1413
+ /**
1414
+ * Task generator
1415
+ *
1416
+ * @template T yielded underlying type
1417
+ * @template TT yielded task type
1418
+ * @template R returned task resolve type
1419
+ *
1420
+ * @example
1421
+ * ```typescript
1422
+ * const generatorFunction = function*(): TaskGenerator<unknown, Task<string>, number> {
1423
+ * const v = yield* someTaskFunction().generator();
1424
+ *
1425
+ * return v.length;
1426
+ * };
1427
+ * ```
1428
+ */
1429
+ export type TaskGenerator<T, TT extends Task<T>, R> = Generator<TT, R, T>;
1430
+ /**
1431
+ * Function returning task generator (generator function)
1432
+ *
1433
+ * @template A argument types
1434
+ * @template T yielded underlying type
1435
+ * @template TT yielded task type
1436
+ * @template R returned task resolve type
1437
+ *
1438
+ * @example
1439
+ * ```typescript
1440
+ * const generatorFunction: TaskGeneratorFunction<[], unknown, Task<string>, number> = function*() {
1441
+ * const v = yield* someTaskFunction().generator();
1442
+ *
1443
+ * return v.length;
1444
+ * };
1445
+ * ```
1446
+ */
1447
+ export type TaskGeneratorFunction<A extends unknown[], T, TT extends Task<T>, R> = (...args: A) => TaskGenerator<T, TT, R>;
1448
+ /**
1449
+ * Task monad interface
1450
+ *
1451
+ * @template R task resolve type
1452
+ */
1453
+ export interface Task<R> extends TaskBase<R> {
1454
+ /**
1455
+ * Invoke callback when task is resolved (and only then)
1456
+ *
1457
+ * @param op callback to invoke
1458
+ * @returns self
1459
+ */
1460
+ tap(op: (value: R) => void): Task<R>;
1461
+ /**
1462
+ * Invoke callback when task is rejected (and only then)
1463
+ *
1464
+ * @param op callback to invoke
1465
+ * @returns self
1466
+ */
1467
+ tapRejected(op: (error: any) => void): Task<R>;
1468
+ /**
1469
+ * Invoke callback when task is canceled (and only then)
1470
+ *
1471
+ * @param op callback to invoke
1472
+ * @returns self
1473
+ */
1474
+ tapCanceled(op: () => void): Task<R>;
1475
+ /**
1476
+ * Invoke a dedicated callback according to task resolution
1477
+ *
1478
+ * @param op.resolved callback to invoke on success
1479
+ * @param op.rejected callback to invoke on failure
1480
+ * @param op.canceled callback to invoke on cancelation
1481
+ * @returns self
1482
+ */
1483
+ matchTap(op: {
1484
+ resolved: (value: R) => void;
1485
+ rejected: (error: any) => void;
1486
+ canceled: () => void;
1487
+ }): Task<R>;
1488
+ /**
1489
+ * Invoke transformer when task is resolved (and only then) and return it's result instead
1490
+ *
1491
+ * @template R2 transformer return type
1492
+ * @param op transformer to invoke
1493
+ * @returns task returning transformer result
1494
+ */
1495
+ map<R2>(op: (value: R) => R2): Task<R2>;
1496
+ /**
1497
+ * Invoke transformer when task is rejected (and only then) and return it's result instead
1498
+ *
1499
+ * @template R2 fallback return type
1500
+ * @param op transformer to invoke
1501
+ * @returns task returning fallback result in case of failure
1502
+ */
1503
+ mapRejected<R2>(op: (error: any) => R2): Task<R | R2>;
1504
+ /**
1505
+ * Invoke transformer when task is canceled (and only then) and return it's result instead
1506
+ *
1507
+ * @template R2 fallback return type
1508
+ * @param op transformer to invoke
1509
+ * @returns task returning fallback result in case of cancelation
1510
+ */
1511
+ mapCanceled<R2>(op: () => R2): Task<R | R2>;
1512
+ /**
1513
+ * Invoke a dedicated transformer according to task resolution
1514
+ *
1515
+ * @template R2 success transformer return type
1516
+ * @template R3 failure transformer return type
1517
+ * @template R4 cancelation transformer return type
1518
+ * @param op.resolved transformer to invoke on success
1519
+ * @param op.rejected transformer to invoke on failure
1520
+ * @param op.canceled transformer to invoke on cancelation
1521
+ * @returns task resolving to a corresponding transformer result
1522
+ */
1523
+ matchMap<R2, R3 = R2, R4 = R3>(op: {
1524
+ resolved: (value: R) => R2;
1525
+ rejected: (error: any) => R3;
1526
+ canceled: () => R4;
1527
+ }): Task<R2 | R3 | R4>;
1528
+ /**
1529
+ * Invoke transformer when task is resolved (and only then) and continue execution with it's result
1530
+ *
1531
+ * @template R2 transformer task resolve type
1532
+ * @param op transformer to invoke
1533
+ * @returns task chaining to transformer result task
1534
+ */
1535
+ chain<R2>(op: (value: R) => Task<R2>): Task<R2>;
1536
+ /**
1537
+ * Invoke transformer when task is rejected (and only then) and continue execution with it's result
1538
+ *
1539
+ * @template R2 fallback task resolve type
1540
+ * @param op transformer to invoke
1541
+ * @returns task chaining to fallback task in case of failure
1542
+ */
1543
+ chainRejected<R2>(op: (error: any) => Task<R2>): Task<R | R2>;
1544
+ /**
1545
+ * Invoke transformer when task is canceled (and only then) and continue execution with it's result
1546
+ *
1547
+ * @template R2 fallback task resolve type
1548
+ * @param op transformer to invoke
1549
+ * @returns task chaining to fallback task in case of cancelation
1550
+ */
1551
+ chainCanceled<R2>(op: () => Task<R2>): Task<R | R2>;
1552
+ /**
1553
+ * Invoke a dedicated transformer according to task resolution and continue execution with it's result
1554
+ *
1555
+ * @template R2 success transformer resolve type
1556
+ * @template R3 failure transformer resolve type
1557
+ * @template R4 cancelation transformer resolve type
1558
+ * @param op.resolved transformer to invoke on success
1559
+ * @param op.rejected transformer to invoke on failure
1560
+ * @param op.canceled transformer to invoke on cancelation
1561
+ * @returns task chaining to a corresponding transformer result
1562
+ */
1563
+ matchChain<R2, R3 = R2, R4 = R3>(op: {
1564
+ resolved: (value: R) => Task<R2>;
1565
+ rejected: (error: any) => Task<R3>;
1566
+ canceled: () => Task<R4>;
1567
+ }): Task<R2 | R3 | R4>;
1568
+ /**
1569
+ * Read the settled result when the task has already finished
1570
+ *
1571
+ * @returns settled result, or null when the task is still pending
1572
+ */
1573
+ resolved: () => Cancelable<R> | null;
1574
+ /**
1575
+ * Return underlying promise in order to await result
1576
+ *
1577
+ * @returns underlying promise
1578
+ */
1579
+ resolve: () => TaskInvoke<R>;
1580
+ /**
1581
+ * Invoke underlying cancel method without error
1582
+ *
1583
+ * @returns 'true' when the task was still pending and cancelation was applied
1584
+ */
1585
+ cancel: () => boolean;
1586
+ /**
1587
+ * Invoke underlying cancel method with error
1588
+ *
1589
+ * @param error error value to be injected from outside
1590
+ * @returns 'true' when the task was still pending and the error was applied
1591
+ */
1592
+ reject: (error: any) => boolean;
1593
+ /**
1594
+ * Wrap task to singleton generator
1595
+ *
1596
+ * Userful in order to avoid ambiguous yied types
1597
+ *
1598
+ * @returns generator of task wrapping the task
1599
+ * @example
1600
+ * ```typescript
1601
+ * const getString = () => Task.resolved('hello');
1602
+ * const getLength = (data: string) => Task.resolved(data.length);
1603
+ *
1604
+ * // ..... //
1605
+ *
1606
+ * Task.generate(function* () {
1607
+ * const data = yield getString(); // data: string | number
1608
+ * const length = yield getLength(data); // length: string | number
1609
+ *
1610
+ * return length;
1611
+ * });
1612
+ *
1613
+ * // ..... //
1614
+ *
1615
+ * Task.generate(function* () {
1616
+ * const data = yield* getString().generator(); // data: string
1617
+ * const length = yield* getLength(data).generator(); // length: number
1618
+ *
1619
+ * return length;
1620
+ * });
1621
+ * ```
1622
+ */
1623
+ generator: TaskGeneratorFunction<[], unknown, Task<R>, R>;
1624
+ }
1625
+ export declare namespace Task {
1626
+ /**
1627
+ * Custom task monad constructor
1628
+ *
1629
+ * @template R returned task's resolve type
1630
+ * @param invoke promise defining task execution
1631
+ * @param cancel cancelation function
1632
+ * @returns task resolving to resolve value of invoke
1633
+ *
1634
+ * @note low-level primitive for creating custom tasks, not intended for general use
1635
+ */
1636
+ function create<R>(invoke: TaskInvoke<R>, cancel: TaskCancel): Task<R>;
1637
+ /**
1638
+ * Invariant task constructor creating resolved task from plain value
1639
+ *
1640
+ * @template R returned task's resolve type
1641
+ * @param value value to be returned upon awaiting
1642
+ * @returns task resolving to specified value
1643
+ */
1644
+ function resolved<R>(value: R): Task<R>;
1645
+ /**
1646
+ * Invariant task constructor creating rejected task from error value
1647
+ *
1648
+ * @template R returned task's resolve type
1649
+ * @param error error to be returned upon awaiting
1650
+ * @returns task rejected with the specified error
1651
+ */
1652
+ function rejected<R>(error: any): Task<R>;
1653
+ /**
1654
+ * Invariant task constructor creating canceled task
1655
+ *
1656
+ * @template R returned task's resolve type
1657
+ * @returns canceled task
1658
+ */
1659
+ function canceled<R>(): Task<R>;
1660
+ /**
1661
+ * Lift from promise to task resolving to that promise result
1662
+ *
1663
+ * Userfull for converting promises to tasks
1664
+ * If converted task is canceled or failed externally return value will be ignored without side-effects
1665
+ * All tasks are no-trowing by default, any occured errors are returned as Left<any>
1666
+ *
1667
+ * @template R returned task resolve type
1668
+ * @param promise promise to be resolved
1669
+ * @returns task resolving to specified promise value
1670
+ *
1671
+ * @example
1672
+ * ```typescript
1673
+ * const task = Task.fromPromise(someAsyncOperation('someData')).map((result) => result.length);
1674
+ * ```
1675
+ */
1676
+ function fromPromise<R>(promise: PromiseLike<R>): Task<R>;
1677
+ /**
1678
+ * Lift from sync function to task resolving to said function result
1679
+ *
1680
+ * Generally, functions are not considered tasks as they cannot be interrupted in any way
1681
+ * However there may be cases when impure function performs actions leading to cancelling or failing the task they are called from
1682
+ * Without such wrapper such cases would lead to ignoring said cancelation and continuilg task chain
1683
+ * This function is mostly used internally and is not expected to be widely used except of some edge scenarios
1684
+ *
1685
+ * If converted task is canceled or failed externally return value will be ignored without side-effects
1686
+ * All tasks are no-trowing by default, any occured errors are returned as Left<any>
1687
+ *
1688
+ * @template R returned task resolve type
1689
+ * @param producer function to be wrapped to a task
1690
+ * @param cancelRef reference object allowing to set cancelation function prior to invoking function
1691
+ * @returns task resolving to the specified function's result
1692
+ *
1693
+ * @example
1694
+ * ```typescript
1695
+ * const cancelRef = { cancel: () => {} };
1696
+ *
1697
+ * const task = Task.fromFunction(() => someFunction('someData'), cancelRef).map((result) => result.length);
1698
+ * ```
1699
+ */
1700
+ function fromFunction<R>(producer: () => R, cancelRef: {
1701
+ cancel: TaskCancel;
1702
+ }): Task<R>;
1703
+ /**
1704
+ * Generic callback task
1705
+ *
1706
+ * Usefull for creating custom tasks from success and error callbacks and cancelaton function. Synchronous callbacks are not supported, @see fromFunction
1707
+ *
1708
+ * @template H cancelation handler type
1709
+ * @template R returned task resolve type
1710
+ * @param create task body with success and error callbacks
1711
+ * @param onCancel cancelation function intended for stopping task execution
1712
+ * @returns task resolving to success value
1713
+ *
1714
+ * @example
1715
+ * ```typescript
1716
+ * export function timeout(delay: number) {
1717
+ * return fromCallback<NodeJS.Timeout, void>((resolve) => setTimeout(() => resolve(), delay), clearTimeout);
1718
+ * }
1719
+ * ```
1720
+ */
1721
+ function fromCallback<H, R>(create: (resolve: (result: R) => void, reject: (error: any) => void, cancel: () => void) => H, onCancel: (handler: H) => void): Task<R>;
1722
+ /**
1723
+ * Lift an async operation that accepts an AbortSignal into a Task.
1724
+ *
1725
+ * Creates an internal AbortController and passes its signal to `producer`.
1726
+ * If the resulting task is canceled or rejected externally, `controller.abort()`
1727
+ * is called automatically.
1728
+ *
1729
+ * @template R returned task resolve type
1730
+ * @param producer factory function receiving an AbortSignal
1731
+ * @returns task wrapping the operation
1732
+ *
1733
+ * @example
1734
+ * ```typescript
1735
+ * const fetchUser = (id: string) =>
1736
+ * Task.fromAbortSignal((signal) =>
1737
+ * fetch(`/api/users/${id}`, { signal }).then((res) => res.json())
1738
+ * );
1739
+ * ```
1740
+ */
1741
+ function fromAbortSignal<R>(producer: (signal: AbortSignal) => PromiseLike<R>): Task<R>;
1742
+ /**
1743
+ * Convinience shortcut for yielding async functions as tasks
1744
+ *
1745
+ * @template R returned generator resolve type
1746
+ * @param promise promise to be resolved
1747
+ * @returns generator to be be used with yield*
1748
+ */
1749
+ function promiseGenerator<R>(promise: PromiseLike<R>): TaskGenerator<unknown, Task<R>, R>;
1750
+ /**
1751
+ * Convinience shortcut for yielding timers
1752
+ *
1753
+ * @param delay duration in ms after that the generator resolves
1754
+ * @returns generator to be used with yield*
1755
+ */
1756
+ function timeoutGenerator(delay: number): TaskGenerator<unknown, Task<void>, void>;
1757
+ /**
1758
+ * Lift from function returning value/promise to function returning task resolving to that value
1759
+ *
1760
+ * Userfull for converting exising async functions to task functions for further use
1761
+ *
1762
+ * @template A returned function's argument types
1763
+ * @template R returned function's result task's resolve type
1764
+ * @param promiseFunction function returning promise or plain value to be resolved
1765
+ * @returns task function wrapping specified promise function
1766
+ * @see Task.fromPromise
1767
+ *
1768
+ * @example
1769
+ * ```typescript
1770
+ * const taskFunction = liftPromiseFunction(someAsyncOperation);
1771
+ *
1772
+ * const task = taskFunction('someData').map((result) => result.length);
1773
+ * ```
1774
+ */
1775
+ function lift<A extends unknown[], R>(promiseFunction: (...args: A) => PromiseLike<R>): TaskFunction<A, R>;
1776
+ /**
1777
+ * Create compound task from generator function
1778
+ *
1779
+ * Applying yield to task within the generator function awaits the task and returns underlying value in case of success
1780
+ * However the convinient option for typescript is to use ```yield* task.generator()``` as othervise one may have to deal with union types
1781
+ *
1782
+ * In case of failure an error is thrown and may be caught by try-catch
1783
+ *
1784
+ * If error is not caught the task is interrupted and returns 'just left error' immediately. In case of cancelation the task is interrupted and returns 'nothing' immediately
1785
+ *
1786
+ * @template T yielded underlying type
1787
+ * @template TT yielded task type
1788
+ * @template R returned task resolve type
1789
+ * @param taskGeneratorFunction task generator function
1790
+ * @returns task resolving to generator's return type
1791
+ *
1792
+ * @note compound task execution is interrupted only at yield statements, so despite returning immediately any promise-based chains would continue running until the first yield
1793
+ * @note due to type unpredictability you HAVE to use ```.generator()``` together with yield* to avoid type issues, despite the fact that task may be yielded directly
1794
+ *
1795
+ * @example
1796
+ * ```typescript
1797
+ * const task = Task.generate(function*() {
1798
+ * try {
1799
+ * const value1 = yield* delayedValueTask('data', 400).generator();
1800
+ *
1801
+ * const value2 = yield* delayedValueTask(value1.length, 300).generator();
1802
+ *
1803
+ * return value2;
1804
+ * } catch (e) {
1805
+ * return -1;
1806
+ * }
1807
+ * });
1808
+ * ```
1809
+ */
1810
+ function generate<T, TT extends Task<T>, R>(taskGeneratorFunction: TaskGeneratorFunction<[], T, TT, R>): Task<R>;
1811
+ /**
1812
+ * Task function generator
1813
+ *
1814
+ * Simplifies wrighting functions returning generated tasks
1815
+ *
1816
+ * @template A generator arguments
1817
+ * @template T yielded underlying type
1818
+ * @template TT yielded task type
1819
+ * @template R returned task resolve type
1820
+ * @param taskGeneratorFunction function* (...args) task generator function
1821
+ * @returns task function resolving to generator's return type
1822
+ */
1823
+ function generateFunction<A extends any[], T, TT extends Task<T>, R>(taskGeneratorFunction: TaskGeneratorFunction<A, T, TT, R>): TaskFunction<A, R>;
1824
+ /**
1825
+ * Generic timeout task
1826
+ *
1827
+ * Usefull for creating delays in task chains or implementing limiting tasks
1828
+ *
1829
+ * @param delay duration in ms after that the task resolves to void
1830
+ * @returns task resolving to void (undefined) after specified delay
1831
+ *
1832
+ * @example
1833
+ * ```typescript
1834
+ * const delayedValueTask = <T>(value: T, delay: number) => Task.timeout(delay).map(() => value);
1835
+ * // ... //
1836
+ * const value = yield* delayedValueTask(42, 1000).generator();
1837
+ *
1838
+ * console.log("It's past 1 second and here's a value:", value)
1839
+ * ```
1840
+ */
1841
+ function timeout(delay: number): Task<void>;
1842
+ /**
1843
+ * Start multiple tasks one after another
1844
+ *
1845
+ * @template T underlying task type
1846
+ * @param taskFunctions list of task functions (without arguments)
1847
+ * @returns composite task invoring every task in order and resolving to the list of results
1848
+ */
1849
+ function sequence<T>(taskFunctions: Iterable<TaskFunction<[], T>>): Task<T[]>;
1850
+ /**
1851
+ * Execute all tasks in parallel and return list of results
1852
+ *
1853
+ * Resulting task resolves when all tasks are resolved, returning list of tasks results maintaining result order
1854
+ * In case of failure (external or internal) resulting task is immediately immediately rejected with that error and all pending tasks are canceled
1855
+ * In case of cancelation resulting task is immediately immediately canceled together with all pending tasks
1856
+ *
1857
+ * @note direct equivalent of Promise.all
1858
+ *
1859
+ * @template T underlying task type
1860
+ * @param tasks iterable of tasks to execute
1861
+ * @returns task resolving to the list of results
1862
+ */
1863
+ function all<T>(tasks: Iterable<Task<T>>): Task<T[]>;
1864
+ /**
1865
+ * Execute all tasks in parallel and return the result of the first successful one
1866
+ *
1867
+ * Resulting task resolves when the first tasks is successfuly resolved, returning it's result
1868
+ * In case of external task rejecttion all tasks are rejected with that error and resulting task is rejected with an array of errors
1869
+ * In case of internal task rejection the execution is continued untill fthe first task resolves succesfully or all the tasks are rejected this way resulting task is rejected with an array of errors
1870
+ * In case of cancelation resulting task is immediately immediately canceled together with all pending tasks
1871
+ *
1872
+ * @note direct equivalent of Promise.any
1873
+ *
1874
+ * @template T underlying task type
1875
+ * @param tasks iterable of tasks to execute
1876
+ * @returns task resolving to the result of first successfull task
1877
+ */
1878
+ function any<T>(tasks: Iterable<Task<T>>): Task<unknown>;
1879
+ /**
1880
+ * Limit task execution based on another task
1881
+ *
1882
+ * If the second task resolves sooner than the first, resulting task is canceled
1883
+ * Otherwise the resulting task resolves together with the first one
1884
+ *
1885
+ * @template T underlying task type
1886
+ * @param task task to limit
1887
+ * @param limitTask limiting task
1888
+ * @returns task limites according to limitTask
1889
+ */
1890
+ function limit<T>(task: Task<T>, limitTask: Task<void>): Task<unknown>;
1891
+ /**
1892
+ * Repeat task untill successful
1893
+ *
1894
+ * Given task is re-generated in case of failure (both external and internal)
1895
+ * Resulting task is still cancelable by standard means
1896
+ *
1897
+ * @template T underlying task type
1898
+ * @param taskFunction task preoducing function
1899
+ * @returns task that retries until the produced task resolves
1900
+ */
1901
+ function repeat<T>(taskFunction: TaskFunction<[], T>): Task<T>;
1902
+ }
1903
+ type TaskInvoke<R> = Promise<Cancelable<R>>;
1904
+ type TaskCancel = (error: Maybe<any>) => boolean;
1905
+ /**
1906
+ * @hidden
1907
+ */
1908
+ interface TaskBase<R> {
1909
+ _invoke(op: (result: Cancelable<R>) => void): void;
1910
+ _cancel(error: Maybe<any>): boolean;
1911
+ }
1912
+ //#endregion
1913
+ //# sourceMappingURL=t-tasks.d.ts.map