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.
- package/dist/t-tasks.d.ts +1913 -0
- package/dist/t-tasks.esm.js +778 -2
- package/dist/t-tasks.esm.js.map +1 -1
- package/dist/t-tasks.js +814 -2
- package/dist/t-tasks.js.map +1 -1
- package/package.json +12 -7
- package/src/either.ts +135 -45
- package/src/maybe.ts +121 -37
- package/src/task.ts +472 -252
- package/dist/either.d.ts +0 -626
- package/dist/index.d.ts +0 -3
- package/dist/maybe.d.ts +0 -595
- package/dist/t-tasks.modern.mjs +0 -2
- package/dist/t-tasks.modern.mjs.map +0 -1
- package/dist/t-tasks.umd.js +0 -2
- package/dist/t-tasks.umd.js.map +0 -1
- package/dist/task.d.ts +0 -489
package/src/maybe.ts
CHANGED
|
@@ -31,7 +31,7 @@ export interface Just<R> {
|
|
|
31
31
|
orTap(op: () => void): this;
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
|
-
* Maybe
|
|
34
|
+
* Maybe pattern matching peeker function
|
|
35
35
|
*
|
|
36
36
|
* Applied to 'just value' returns self invoking op.just(value) in process
|
|
37
37
|
* Applied to 'nothing' returns self invoking op.nothing() in process
|
|
@@ -63,6 +63,7 @@ export interface Just<R> {
|
|
|
63
63
|
* Applied to 'just value' returns self without invoking transformer
|
|
64
64
|
* Applied to 'nothing' returns 'just op()'
|
|
65
65
|
*
|
|
66
|
+
* @template R2 transformer function's return type
|
|
66
67
|
* @param op function to be invoked
|
|
67
68
|
* @returns 'just value' or 'just op()'
|
|
68
69
|
*/
|
|
@@ -72,7 +73,24 @@ export interface Just<R> {
|
|
|
72
73
|
orMap<R2>(this: Maybe<R>, op: () => R2): Just<R | R2>;
|
|
73
74
|
|
|
74
75
|
/**
|
|
75
|
-
* Maybe
|
|
76
|
+
* Maybe pattern matching extractor function
|
|
77
|
+
*
|
|
78
|
+
* Applied to 'just value' returns op.just(value)
|
|
79
|
+
* Applied to 'nothing' returns op.nothing()
|
|
80
|
+
*
|
|
81
|
+
* @template R2 just extractor function's return type
|
|
82
|
+
* @template R3 nothing extractor function's return type
|
|
83
|
+
* @param op.just extractor to be invoked with underlying value in case of just
|
|
84
|
+
* @param op.nothing extractor to be invoked in case of nothing
|
|
85
|
+
* @returns op.just(value) or op.nothing()
|
|
86
|
+
*/
|
|
87
|
+
// Just cases
|
|
88
|
+
match<R2, R3 = R2>(this: Just<R>, op: { just: (value: R) => R2; nothing: () => R3 }): R2;
|
|
89
|
+
// General case
|
|
90
|
+
match<R2, R3 = R2>(this: Maybe<R>, op: { just: (value: R) => R2; nothing: () => R3 }): R2 | R3;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Maybe pattern matching transformer function
|
|
76
94
|
*
|
|
77
95
|
* Applied to 'just value' returns 'just op.just(value)'
|
|
78
96
|
* Applied to 'nothing' returns 'just op.nothing()'
|
|
@@ -88,6 +106,7 @@ export interface Just<R> {
|
|
|
88
106
|
// General case
|
|
89
107
|
matchMap<R2, R3 = R2>(this: Maybe<R>, op: { just: (value: R) => R2; nothing: () => R3 }): Just<R2 | R3>;
|
|
90
108
|
|
|
109
|
+
|
|
91
110
|
/**
|
|
92
111
|
* Maybe composition function
|
|
93
112
|
*
|
|
@@ -110,7 +129,7 @@ export interface Just<R> {
|
|
|
110
129
|
/**
|
|
111
130
|
* Maybe fallback composition function
|
|
112
131
|
*
|
|
113
|
-
* Applied to 'just value' returns self
|
|
132
|
+
* Applied to 'just value' returns self without invoking composition function
|
|
114
133
|
* Applied to 'nothing' returns op()
|
|
115
134
|
*
|
|
116
135
|
* @template R2 transformer function result's underlying type
|
|
@@ -127,7 +146,7 @@ export interface Just<R> {
|
|
|
127
146
|
orChain<R2>(this: Maybe<R>, op: () => Maybe<R2>): Maybe<R | R2>;
|
|
128
147
|
|
|
129
148
|
/**
|
|
130
|
-
* Maybe
|
|
149
|
+
* Maybe pattern matching composition function
|
|
131
150
|
*
|
|
132
151
|
* Applied to 'just value' returns 'op.just(value)'
|
|
133
152
|
* Applied to 'nothing' returns op.nothing()
|
|
@@ -180,7 +199,7 @@ export interface Just<R> {
|
|
|
180
199
|
*
|
|
181
200
|
* @example
|
|
182
201
|
* ```typescript
|
|
183
|
-
* if (maybe.
|
|
202
|
+
* if (maybe.isNothing()) {
|
|
184
203
|
* console.log('nothing');
|
|
185
204
|
* }
|
|
186
205
|
* ```
|
|
@@ -190,7 +209,7 @@ export interface Just<R> {
|
|
|
190
209
|
/**
|
|
191
210
|
* Wrap Maybe to singleton generator
|
|
192
211
|
*
|
|
193
|
-
*
|
|
212
|
+
* Useful in order to avoid ambiguous yield types
|
|
194
213
|
*
|
|
195
214
|
* @returns generator of Maybe wrapping this
|
|
196
215
|
*/
|
|
@@ -200,7 +219,9 @@ export interface Just<R> {
|
|
|
200
219
|
/**
|
|
201
220
|
* Absolutely Nothing
|
|
202
221
|
*
|
|
203
|
-
* Maybe data type
|
|
222
|
+
* Maybe data type specialization representing an absence of any value
|
|
223
|
+
*
|
|
224
|
+
* @template R underlying value
|
|
204
225
|
*/
|
|
205
226
|
export interface Nothing<R> {
|
|
206
227
|
/**
|
|
@@ -226,7 +247,7 @@ export interface Nothing<R> {
|
|
|
226
247
|
orTap(op: () => void): this;
|
|
227
248
|
|
|
228
249
|
/**
|
|
229
|
-
* Maybe
|
|
250
|
+
* Maybe pattern matching peeker function
|
|
230
251
|
*
|
|
231
252
|
* Applied to 'just value' returns self invoking op.just(value) in process
|
|
232
253
|
* Applied to 'nothing' returns self invoking op.nothing() in process
|
|
@@ -258,6 +279,7 @@ export interface Nothing<R> {
|
|
|
258
279
|
* Applied to 'just value' returns self without invoking transformer
|
|
259
280
|
* Applied to 'nothing' returns 'just op()'
|
|
260
281
|
*
|
|
282
|
+
* @template R2 transformer function's return type
|
|
261
283
|
* @param op function to be invoked
|
|
262
284
|
* @returns 'just value' or 'just op()'
|
|
263
285
|
*/
|
|
@@ -267,7 +289,24 @@ export interface Nothing<R> {
|
|
|
267
289
|
orMap<R2>(this: Maybe<R>, op: () => R2): Just<R | R2>;
|
|
268
290
|
|
|
269
291
|
/**
|
|
270
|
-
* Maybe
|
|
292
|
+
* Maybe pattern matching extractor function
|
|
293
|
+
*
|
|
294
|
+
* Applied to 'just value' returns op.just(value)
|
|
295
|
+
* Applied to 'nothing' returns op.nothing()
|
|
296
|
+
*
|
|
297
|
+
* @template R2 just extractor function's return type
|
|
298
|
+
* @template R3 nothing extractor function's return type
|
|
299
|
+
* @param op.just extractor to be invoked with underlying value in case of just
|
|
300
|
+
* @param op.nothing extractor to be invoked in case of nothing
|
|
301
|
+
* @returns op.just(value) or op.nothing()
|
|
302
|
+
*/
|
|
303
|
+
// Nothing cases
|
|
304
|
+
match<R2, R3 = R2>(this: Nothing<R>, op: { just: (value: R) => R2; nothing: () => R3 }): R3;
|
|
305
|
+
// General case
|
|
306
|
+
match<R2, R3 = R2>(this: Maybe<R>, op: { just: (value: R) => R2; nothing: () => R3 }): R2 | R3;
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Maybe pattern matching transformer function
|
|
271
310
|
*
|
|
272
311
|
* Applied to 'just value' returns 'just op.just(value)'
|
|
273
312
|
* Applied to 'nothing' returns 'just op.nothing()'
|
|
@@ -280,7 +319,7 @@ export interface Nothing<R> {
|
|
|
280
319
|
*/
|
|
281
320
|
// Nothing cases
|
|
282
321
|
matchMap<R2, R3 = R2>(this: Nothing<R>, op: { just: (value: R) => R2; nothing: () => R3 }): Just<R3>;
|
|
283
|
-
// General
|
|
322
|
+
// General case
|
|
284
323
|
matchMap<R2, R3 = R2>(this: Maybe<R>, op: { just: (value: R) => R2; nothing: () => R3 }): Just<R2 | R3>;
|
|
285
324
|
|
|
286
325
|
/**
|
|
@@ -305,7 +344,7 @@ export interface Nothing<R> {
|
|
|
305
344
|
/**
|
|
306
345
|
* Maybe fallback composition function
|
|
307
346
|
*
|
|
308
|
-
* Applied to 'just value' returns self
|
|
347
|
+
* Applied to 'just value' returns self without invoking composition function
|
|
309
348
|
* Applied to 'nothing' returns op()
|
|
310
349
|
*
|
|
311
350
|
* @template R2 transformer function result's underlying type
|
|
@@ -322,7 +361,7 @@ export interface Nothing<R> {
|
|
|
322
361
|
orChain<R2>(this: Maybe<R>, op: () => Maybe<R2>): Maybe<R | R2>;
|
|
323
362
|
|
|
324
363
|
/**
|
|
325
|
-
* Maybe
|
|
364
|
+
* Maybe pattern matching composition function
|
|
326
365
|
*
|
|
327
366
|
* Applied to 'just value' returns 'op.just(value)'
|
|
328
367
|
* Applied to 'nothing' returns op.nothing()
|
|
@@ -375,7 +414,7 @@ export interface Nothing<R> {
|
|
|
375
414
|
*
|
|
376
415
|
* @example
|
|
377
416
|
* ```typescript
|
|
378
|
-
* if (maybe.
|
|
417
|
+
* if (maybe.isNothing()) {
|
|
379
418
|
* console.log('nothing');
|
|
380
419
|
* }
|
|
381
420
|
* ```
|
|
@@ -385,7 +424,7 @@ export interface Nothing<R> {
|
|
|
385
424
|
/**
|
|
386
425
|
* Wrap Maybe to singleton generator
|
|
387
426
|
*
|
|
388
|
-
*
|
|
427
|
+
* Useful in order to avoid ambiguous yield types
|
|
389
428
|
*
|
|
390
429
|
* @returns generator of Maybe wrapping this
|
|
391
430
|
*/
|
|
@@ -393,9 +432,9 @@ export interface Nothing<R> {
|
|
|
393
432
|
}
|
|
394
433
|
|
|
395
434
|
/**
|
|
396
|
-
*
|
|
435
|
+
* Generic Maybe monad interface
|
|
397
436
|
*
|
|
398
|
-
* As per classic Maybe monad implementation can
|
|
437
|
+
* As per classic Maybe monad implementation can either contain just a value or contain nothing
|
|
399
438
|
* Used throughout the library to represent optional return type, specifically return type of cancelled tasks
|
|
400
439
|
*
|
|
401
440
|
* @template R underlying value
|
|
@@ -406,7 +445,7 @@ export namespace Just {
|
|
|
406
445
|
/**
|
|
407
446
|
* Standalone Just value extractor
|
|
408
447
|
*
|
|
409
|
-
*
|
|
448
|
+
* Useful for passing as a function to collection transformers
|
|
410
449
|
*
|
|
411
450
|
* @template R underlying value type
|
|
412
451
|
* @param just Just instance
|
|
@@ -419,7 +458,23 @@ export namespace Just {
|
|
|
419
458
|
|
|
420
459
|
export namespace Nothing {}
|
|
421
460
|
|
|
461
|
+
/**
|
|
462
|
+
* Maybe generator
|
|
463
|
+
*
|
|
464
|
+
* @template T yielded underlying type
|
|
465
|
+
* @template TT yielded Maybe type
|
|
466
|
+
* @template R returned underlying type
|
|
467
|
+
*/
|
|
422
468
|
export type MaybeGenerator<T, TT extends Maybe<T>, R> = Generator<TT, R, T>;
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Function returning Maybe generator (generator function)
|
|
472
|
+
*
|
|
473
|
+
* @template A argument types
|
|
474
|
+
* @template T yielded underlying type
|
|
475
|
+
* @template TT yielded Maybe type
|
|
476
|
+
* @template R returned underlying type
|
|
477
|
+
*/
|
|
423
478
|
export type MaybeGeneratorFunction<A extends unknown[], T, TT extends Maybe<T>, R> = (...args: A) => MaybeGenerator<T, TT, R>;
|
|
424
479
|
|
|
425
480
|
export namespace Maybe {
|
|
@@ -439,8 +494,8 @@ export namespace Maybe {
|
|
|
439
494
|
*
|
|
440
495
|
* @returns 'nothing'
|
|
441
496
|
*/
|
|
442
|
-
export function nothing(): Nothing<
|
|
443
|
-
return staticNothing
|
|
497
|
+
export function nothing<R = never>(): Nothing<R> {
|
|
498
|
+
return staticNothing as Nothing<R>;
|
|
444
499
|
}
|
|
445
500
|
|
|
446
501
|
/**
|
|
@@ -448,6 +503,7 @@ export namespace Maybe {
|
|
|
448
503
|
*
|
|
449
504
|
* Resolves to 'nothing' in case of undefined value and 'just value' otherwise
|
|
450
505
|
*
|
|
506
|
+
* @template R underlying type
|
|
451
507
|
* @param value possibly undefined value
|
|
452
508
|
* @returns either 'just value' or 'nothing'
|
|
453
509
|
*/
|
|
@@ -463,7 +519,8 @@ export namespace Maybe {
|
|
|
463
519
|
*
|
|
464
520
|
* Resolves to 'nothing' in case of undefined or null value and 'just value' otherwise
|
|
465
521
|
*
|
|
466
|
-
* @
|
|
522
|
+
* @template R underlying type
|
|
523
|
+
* @param value possibly undefined or null value
|
|
467
524
|
* @returns either 'just value' or 'nothing'
|
|
468
525
|
*/
|
|
469
526
|
export function fromNullable(value: undefined): Nothing<never>;
|
|
@@ -474,10 +531,29 @@ export namespace Maybe {
|
|
|
474
531
|
return Maybe.fromOptional(value).chain((v) => (v !== null ? Maybe.just(v) : Maybe.nothing()));
|
|
475
532
|
}
|
|
476
533
|
|
|
534
|
+
/**
|
|
535
|
+
* Standalone Maybe value extractor
|
|
536
|
+
*
|
|
537
|
+
* Applied to 'just value' returns underlying value without using fallback
|
|
538
|
+
* Applied to 'nothing' returns fallback
|
|
539
|
+
*
|
|
540
|
+
* @template R underlying value type
|
|
541
|
+
* @template R2 fallback value type
|
|
542
|
+
* @param maybe Maybe instance
|
|
543
|
+
* @param fallback value to be returned in case of 'nothing'
|
|
544
|
+
* @returns underlying value or fallback
|
|
545
|
+
*/
|
|
546
|
+
export function fromMaybe<R>(maybe: Just<R>, fallback: unknown): R;
|
|
547
|
+
export function fromMaybe<R, R2>(maybe: Nothing<R>, fallback: R2): R2;
|
|
548
|
+
export function fromMaybe<R, R2>(maybe: Maybe<R>, fallback: R2): R | R2;
|
|
549
|
+
export function fromMaybe<R, R2>(maybe: Maybe<R>, fallback: R2) {
|
|
550
|
+
return maybe.match({ just: (value) => value, nothing: () => fallback });
|
|
551
|
+
}
|
|
552
|
+
|
|
477
553
|
/**
|
|
478
554
|
* Standalone type guard for 'just'
|
|
479
555
|
*
|
|
480
|
-
*
|
|
556
|
+
* Useful for passing as a predicate to collection transformers
|
|
481
557
|
*
|
|
482
558
|
* @template R underlying value type
|
|
483
559
|
* @param maybe Maybe instance
|
|
@@ -490,19 +566,20 @@ export namespace Maybe {
|
|
|
490
566
|
/**
|
|
491
567
|
* Standalone type guard for 'nothing'
|
|
492
568
|
*
|
|
493
|
-
*
|
|
569
|
+
* Useful for passing as a predicate to collection transformers
|
|
494
570
|
*
|
|
495
571
|
* @template R underlying value type
|
|
496
572
|
* @param maybe Maybe instance
|
|
497
573
|
* @returns 'true' in case wrapped value is 'nothing' (and resolves argument type to be 'nothing')
|
|
498
574
|
*/
|
|
499
|
-
export function isNothing<R>(maybe: Maybe<R>): maybe is Nothing<
|
|
575
|
+
export function isNothing<R>(maybe: Maybe<R>): maybe is Nothing<R> {
|
|
500
576
|
return maybe.isNothing();
|
|
501
577
|
}
|
|
502
578
|
|
|
503
579
|
/**
|
|
504
580
|
* Standalone list predicate
|
|
505
581
|
*
|
|
582
|
+
* @template R underlying value type
|
|
506
583
|
* @param maybes a list of Maybe
|
|
507
584
|
* @returns true in case every list element is Just
|
|
508
585
|
*/
|
|
@@ -513,8 +590,9 @@ export namespace Maybe {
|
|
|
513
590
|
/**
|
|
514
591
|
* Standalone list predicate
|
|
515
592
|
*
|
|
593
|
+
* @template R underlying value type
|
|
516
594
|
* @param maybes a list of Maybe
|
|
517
|
-
* @returns true in case
|
|
595
|
+
* @returns true in case at least one list element is Just
|
|
518
596
|
*/
|
|
519
597
|
export function someJust<R>(maybes: Maybe<R>[]): boolean {
|
|
520
598
|
return maybes.some(Maybe.isJust);
|
|
@@ -523,6 +601,7 @@ export namespace Maybe {
|
|
|
523
601
|
/**
|
|
524
602
|
* Standalone list predicate
|
|
525
603
|
*
|
|
604
|
+
* @template R underlying value type
|
|
526
605
|
* @param maybes a list of Maybe
|
|
527
606
|
* @returns true in case every list element is Nothing
|
|
528
607
|
*/
|
|
@@ -533,6 +612,7 @@ export namespace Maybe {
|
|
|
533
612
|
/**
|
|
534
613
|
* Standalone list predicate
|
|
535
614
|
*
|
|
615
|
+
* @template R underlying value type
|
|
536
616
|
* @param maybes a list of Maybe
|
|
537
617
|
* @returns true in case at least one list element is Nothing
|
|
538
618
|
*/
|
|
@@ -544,29 +624,27 @@ export namespace Maybe {
|
|
|
544
624
|
* Create compound Maybe from generator function
|
|
545
625
|
*
|
|
546
626
|
* Applying yield to a Maybe within the generator function unwraps the Maybe and returns underlying value in case of success
|
|
547
|
-
* However the
|
|
627
|
+
* However the convenient option for typescript is to use ```yield* maybe.generator()``` as otherwise one may have to deal with union types
|
|
548
628
|
*
|
|
629
|
+
* @template T yielded underlying type
|
|
549
630
|
* @template TT yielded Maybe type
|
|
550
631
|
* @template R returned underlying type
|
|
551
632
|
* @param maybeGeneratorFunction Maybe generator function
|
|
552
633
|
* @returns Just wrapping the result of generator function or Nothing
|
|
553
|
-
* ```
|
|
554
634
|
*/
|
|
555
635
|
export function generate<T, TT extends Maybe<T>, R>(maybeGeneratorFunction: MaybeGeneratorFunction<[], T, TT, R>): Maybe<R> {
|
|
556
636
|
const generator = maybeGeneratorFunction();
|
|
637
|
+
let next = generator.next();
|
|
557
638
|
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
Maybe.
|
|
561
|
-
|
|
562
|
-
next.value.matchChain<R>({
|
|
563
|
-
just: (value) => sequentor(generator.next(value)),
|
|
564
|
-
nothing: Maybe.nothing,
|
|
565
|
-
})
|
|
566
|
-
);
|
|
567
|
-
};
|
|
639
|
+
while (!next.done) {
|
|
640
|
+
if (next.value.isNothing()) {
|
|
641
|
+
return Maybe.nothing();
|
|
642
|
+
}
|
|
568
643
|
|
|
569
|
-
|
|
644
|
+
next = generator.next(next.value.just);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
return Maybe.just(next.value);
|
|
570
648
|
}
|
|
571
649
|
}
|
|
572
650
|
|
|
@@ -603,6 +681,9 @@ class JustClass<R> implements Just<R> {
|
|
|
603
681
|
isNothing(): this is Nothing<R> {
|
|
604
682
|
return false;
|
|
605
683
|
}
|
|
684
|
+
match<R2>(op: { just: (value: R) => R2 }) {
|
|
685
|
+
return op.just(this.just);
|
|
686
|
+
}
|
|
606
687
|
matchTap(op: { just: (value: R) => void }) {
|
|
607
688
|
return this.tap(op.just);
|
|
608
689
|
}
|
|
@@ -646,6 +727,9 @@ class NothingClass implements Nothing<never> {
|
|
|
646
727
|
isNothing(): this is Nothing<never> {
|
|
647
728
|
return true;
|
|
648
729
|
}
|
|
730
|
+
match<R2>(op: { nothing: () => R2 }) {
|
|
731
|
+
return op.nothing();
|
|
732
|
+
}
|
|
649
733
|
matchTap(op: { nothing: () => void }) {
|
|
650
734
|
return this.orTap(op.nothing);
|
|
651
735
|
}
|