retuple 1.0.0-next.0 → 1.0.0-next.10

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/index.js CHANGED
@@ -10,58 +10,131 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _ResultAsync_inner;
13
+ /**
14
+ * ## Retuple Unwrap Failed
15
+ *
16
+ * An error which occurs when calling `$unwrap` on `Err`.
17
+ */
13
18
  export class RetupleUnwrapFailed extends Error {
14
19
  constructor(value, msg = "Unwrap failed") {
15
20
  super(msg, value instanceof Error ? { cause: value } : undefined);
16
21
  this.value = value;
17
22
  }
18
23
  }
24
+ /**
25
+ * ## Retuple Unwrap Err Failed
26
+ *
27
+ * An error which occurs when calling `$unwrapErr` on `Ok`.
28
+ */
19
29
  export class RetupleUnwrapErrFailed extends Error {
20
30
  constructor(value, msg = "Unwrap error failed") {
21
31
  super(msg);
22
32
  this.value = value;
23
33
  }
24
34
  }
35
+ /**
36
+ * ## Retuple Expect Failed
37
+ *
38
+ * An error which occurs when calling `$expect` on `Err`, when the value
39
+ * contained in the `Err` is not an instance of `Error`.
40
+ */
25
41
  export class RetupleExpectFailed extends Error {
26
42
  constructor(value) {
27
43
  super("Expect failed");
28
44
  this.value = value;
29
45
  }
30
46
  }
47
+ /**
48
+ * ## Retuple Expect Failed
49
+ *
50
+ * An error which occurs when calling `$flatten` on `Ok`, when the value
51
+ * contained in the `Ok` is not an `Ok` or `Err`.
52
+ */
53
+ export class RetupleFlattenFailed extends Error {
54
+ constructor(value) {
55
+ super("Flatten Result failed, the contained value was not a Result");
56
+ this.value = value;
57
+ }
58
+ }
59
+ /**
60
+ * ## Retuple Thrown Value Error
61
+ *
62
+ * An error constructed when a safe function call throws or rejects, when the
63
+ * thrown error or rejected value is not an instance of `Error`, and when no
64
+ * map error function is provided.
65
+ */
31
66
  export class RetupleThrownValueError extends Error {
32
67
  constructor(value) {
33
68
  super("Caught value was not an instance of Error");
34
69
  this.value = value;
35
70
  }
36
71
  }
72
+ /**
73
+ * ## Retuple Invalid Result Error
74
+ *
75
+ * This error is thrown when attempting to construct a `Result` from a tuple,
76
+ * when neither index 0 or 1 are null or undefined. In this case, it is
77
+ * impossible to determine whether the result should be `Ok` or `Err`.
78
+ */
79
+ export class RetupleInvalidResultError extends Error {
80
+ constructor(value) {
81
+ super("Constructing a Result from native tuple failed, at least one of the " +
82
+ "values at index 0 or 1 should be null or undefined");
83
+ this.value = value;
84
+ }
85
+ }
86
+ /**
87
+ * ## Retuple Array Method Unavailable Error
88
+ *
89
+ * This error is thrown when calling a built-in array method from a `Result`.
90
+ */
91
+ export class RetupleArrayMethodUnavailableError extends Error {
92
+ constructor(value, method) {
93
+ super(`Built in array method '${method}' should not be called directly from ` +
94
+ "a Result, convert the Result to a tuple first");
95
+ this.value = value;
96
+ }
97
+ }
37
98
  /**
38
99
  * ## Result
39
100
  *
40
101
  * @TODO
41
102
  */
42
- export const Result = {
43
- Ok,
44
- Err,
45
- from,
46
- safe,
47
- safeAsync,
48
- safePromise,
49
- };
50
- export default Result;
103
+ export function Result(resultLike) {
104
+ const [err, ok] = resultLike;
105
+ if (err === null || err === undefined) {
106
+ return new ResultOk(ok);
107
+ }
108
+ if (ok === null || ok === undefined) {
109
+ return new ResultErr(err);
110
+ }
111
+ throw new RetupleInvalidResultError(resultLike);
112
+ }
113
+ Result.Ok = Ok;
114
+ Result.Err = Err;
115
+ Result.$nonNullable = nonNullable;
116
+ Result.$truthy = truthy;
117
+ Result.$safe = safe;
118
+ Result.$safeAsync = safeAsync;
119
+ Result.$safePromise = safePromise;
120
+ Object.freeze(Result);
51
121
  export function Ok(val) {
52
122
  return new ResultOk(val);
53
123
  }
54
124
  export function Err(err) {
55
125
  return new ResultErr(err);
56
126
  }
57
- export function from(value, error) {
58
- if (value) {
59
- return new ResultOk(value);
127
+ export function nonNullable(value, error = mapTrue) {
128
+ if (value !== null && value !== undefined) {
129
+ return Ok(value);
60
130
  }
61
- if (error) {
62
- return new ResultErr(error());
131
+ return Err(error());
132
+ }
133
+ export function truthy(value, error = mapTrue) {
134
+ if (value) {
135
+ return Ok(value);
63
136
  }
64
- return new ResultErr(true);
137
+ return Err(error());
65
138
  }
66
139
  export function safe(f, mapError = ensureError) {
67
140
  try {
@@ -85,24 +158,450 @@ export function safePromise(promise, mapError = ensureError) {
85
158
  return new ResultAsync(promise.then((Ok), async (err) => Err(await mapError(err))));
86
159
  }
87
160
  /**
88
- * ## Ok
161
+ * ## RetupleArray
89
162
  *
90
- * @TODO
163
+ * Using built-in array methods on a `Result` is probably a mistake. This class
164
+ * makes the built-in methods throw `RetupleArrayMethodUnavailableError`.
165
+ */
166
+ class RetupleArray extends Array {
167
+ /**
168
+ * ## Method not available
169
+ *
170
+ * Built-in array methods not available on `Result` types, convert the result
171
+ * to a tuple using `$tuple()` first.
172
+ *
173
+ * @deprecated
174
+ */
175
+ at() {
176
+ throw new RetupleArrayMethodUnavailableError(this, "at");
177
+ }
178
+ /**
179
+ * ## Method not available
180
+ *
181
+ * Built-in array methods not available on `Result` types, convert the result
182
+ * to a tuple using `$tuple()` first.
183
+ *
184
+ * @deprecated
185
+ */
186
+ concat() {
187
+ throw new RetupleArrayMethodUnavailableError(this, "concat");
188
+ }
189
+ /**
190
+ * ## Method not available
191
+ *
192
+ * Built-in array methods not available on `Result` types, convert the result
193
+ * to a tuple using `$tuple()` first.
194
+ *
195
+ * @deprecated
196
+ */
197
+ copyWithin() {
198
+ throw new RetupleArrayMethodUnavailableError(this, "copyWithin");
199
+ }
200
+ /**
201
+ * ## Method not available
202
+ *
203
+ * Built-in array methods not available on `Result` types, convert the result
204
+ * to a tuple using `$tuple()` first.
205
+ *
206
+ * @deprecated
207
+ */
208
+ entries() {
209
+ throw new RetupleArrayMethodUnavailableError(this, "entries");
210
+ }
211
+ /**
212
+ * ## Method not available
213
+ *
214
+ * Built-in array methods not available on `Result` types, convert the result
215
+ * to a tuple using `$tuple()` first.
216
+ *
217
+ * @deprecated
218
+ */
219
+ every() {
220
+ throw new RetupleArrayMethodUnavailableError(this, "every");
221
+ }
222
+ /**
223
+ * ## Method not available
224
+ *
225
+ * Built-in array methods not available on `Result` types, convert the result
226
+ * to a tuple using `$tuple()` first.
227
+ *
228
+ * @deprecated
229
+ */
230
+ fill() {
231
+ throw new RetupleArrayMethodUnavailableError(this, "fill");
232
+ }
233
+ /**
234
+ * ## Method not available
235
+ *
236
+ * Built-in array methods not available on `Result` types, convert the result
237
+ * to a tuple using `$tuple()` first.
238
+ *
239
+ * @deprecated
240
+ */
241
+ filter() {
242
+ throw new RetupleArrayMethodUnavailableError(this, "filter");
243
+ }
244
+ /**
245
+ * ## Method not available
246
+ *
247
+ * Built-in array methods not available on `Result` types, convert the result
248
+ * to a tuple using `$tuple()` first.
249
+ *
250
+ * @deprecated
251
+ */
252
+ find() {
253
+ throw new RetupleArrayMethodUnavailableError(this, "find");
254
+ }
255
+ /**
256
+ * ## Method not available
257
+ *
258
+ * Built-in array methods not available on `Result` types, convert the result
259
+ * to a tuple using `$tuple()` first.
260
+ *
261
+ * @deprecated
262
+ */
263
+ findIndex() {
264
+ throw new RetupleArrayMethodUnavailableError(this, "findIndex");
265
+ }
266
+ /**
267
+ * ## Method not available
268
+ *
269
+ * Built-in array methods not available on `Result` types, convert the result
270
+ * to a tuple using `$tuple()` first.
271
+ *
272
+ * @deprecated
273
+ */
274
+ findLast() {
275
+ throw new RetupleArrayMethodUnavailableError(this, "findLast");
276
+ }
277
+ /**
278
+ * ## Method not available
279
+ *
280
+ * Built-in array methods not available on `Result` types, convert the result
281
+ * to a tuple using `$tuple()` first.
282
+ *
283
+ * @deprecated
284
+ */
285
+ findLastIndex() {
286
+ throw new RetupleArrayMethodUnavailableError(this, "findLastIndex");
287
+ }
288
+ /**
289
+ * ## Method not available
290
+ *
291
+ * Built-in array methods not available on `Result` types, convert the result
292
+ * to a tuple using `$tuple()` first.
293
+ *
294
+ * @deprecated
295
+ */
296
+ flat() {
297
+ throw new RetupleArrayMethodUnavailableError(this, "flat");
298
+ }
299
+ /**
300
+ * ## Method not available
301
+ *
302
+ * Built-in array methods not available on `Result` types, convert the result
303
+ * to a tuple using `$tuple()` first.
304
+ *
305
+ * @deprecated
306
+ */
307
+ flatMap() {
308
+ throw new RetupleArrayMethodUnavailableError(this, "flatMap");
309
+ }
310
+ /**
311
+ * ## Method not available
312
+ *
313
+ * Built-in array methods not available on `Result` types, convert the result
314
+ * to a tuple using `$tuple()` first.
315
+ *
316
+ * @deprecated
317
+ */
318
+ forEach() {
319
+ throw new RetupleArrayMethodUnavailableError(this, "forEach");
320
+ }
321
+ /**
322
+ * ## Method not available
323
+ *
324
+ * Built-in array methods not available on `Result` types, convert the result
325
+ * to a tuple using `$tuple()` first.
326
+ *
327
+ * @deprecated
328
+ */
329
+ includes() {
330
+ throw new RetupleArrayMethodUnavailableError(this, "includes");
331
+ }
332
+ /**
333
+ * ## Method not available
334
+ *
335
+ * Built-in array methods not available on `Result` types, convert the result
336
+ * to a tuple using `$tuple()` first.
337
+ *
338
+ * @deprecated
339
+ */
340
+ indexOf() {
341
+ throw new RetupleArrayMethodUnavailableError(this, "indexOf");
342
+ }
343
+ /**
344
+ * ## Method not available
345
+ *
346
+ * Built-in array methods not available on `Result` types, convert the result
347
+ * to a tuple using `$tuple()` first.
348
+ *
349
+ * @deprecated
350
+ */
351
+ join() {
352
+ throw new RetupleArrayMethodUnavailableError(this, "join");
353
+ }
354
+ /**
355
+ * ## Method not available
356
+ *
357
+ * Built-in array methods not available on `Result` types, convert the result
358
+ * to a tuple using `$tuple()` first.
359
+ *
360
+ * @deprecated
361
+ */
362
+ keys() {
363
+ throw new RetupleArrayMethodUnavailableError(this, "keys");
364
+ }
365
+ /**
366
+ * ## Method not available
367
+ *
368
+ * Built-in array methods not available on `Result` types, convert the result
369
+ * to a tuple using `$tuple()` first.
370
+ *
371
+ * @deprecated
372
+ */
373
+ lastIndexOf() {
374
+ throw new RetupleArrayMethodUnavailableError(this, "lastIndexOf");
375
+ }
376
+ /**
377
+ * ## Method not available
378
+ *
379
+ * Built-in array methods not available on `Result` types, convert the result
380
+ * to a tuple using `$tuple()` first.
381
+ *
382
+ * @deprecated
383
+ */
384
+ map() {
385
+ throw new RetupleArrayMethodUnavailableError(this, "map");
386
+ }
387
+ /**
388
+ * ## Method not available
389
+ *
390
+ * Built-in array methods not available on `Result` types, convert the result
391
+ * to a tuple using `$tuple()` first.
392
+ *
393
+ * @deprecated
394
+ */
395
+ pop() {
396
+ throw new RetupleArrayMethodUnavailableError(this, "pop");
397
+ }
398
+ /**
399
+ * ## Method not available
400
+ *
401
+ * Built-in array methods not available on `Result` types, convert the result
402
+ * to a tuple using `$tuple()` first.
403
+ *
404
+ * @deprecated
405
+ */
406
+ push() {
407
+ throw new RetupleArrayMethodUnavailableError(this, "push");
408
+ }
409
+ /**
410
+ * ## Method not available
411
+ *
412
+ * Built-in array methods not available on `Result` types, convert the result
413
+ * to a tuple using `$tuple()` first.
414
+ *
415
+ * @deprecated
416
+ */
417
+ reduce() {
418
+ throw new RetupleArrayMethodUnavailableError(this, "reduce");
419
+ }
420
+ /**
421
+ * ## Method not available
422
+ *
423
+ * Built-in array methods not available on `Result` types, convert the result
424
+ * to a tuple using `$tuple()` first.
425
+ *
426
+ * @deprecated
427
+ */
428
+ reduceRight() {
429
+ throw new RetupleArrayMethodUnavailableError(this, "reduceRight");
430
+ }
431
+ /**
432
+ * ## Method not available
433
+ *
434
+ * Built-in array methods not available on `Result` types, convert the result
435
+ * to a tuple using `$tuple()` first.
436
+ *
437
+ * @deprecated
438
+ */
439
+ reverse() {
440
+ throw new RetupleArrayMethodUnavailableError(this, "reverse");
441
+ }
442
+ /**
443
+ * ## Method not available
444
+ *
445
+ * Built-in array methods not available on `Result` types, convert the result
446
+ * to a tuple using `$tuple()` first.
447
+ *
448
+ * @deprecated
449
+ */
450
+ shift() {
451
+ throw new RetupleArrayMethodUnavailableError(this, "shift");
452
+ }
453
+ /**
454
+ * ## Method not available
455
+ *
456
+ * Built-in array methods not available on `Result` types, convert the result
457
+ * to a tuple using `$tuple()` first.
458
+ *
459
+ * @deprecated
460
+ */
461
+ slice() {
462
+ throw new RetupleArrayMethodUnavailableError(this, "slice");
463
+ }
464
+ /**
465
+ * ## Method not available
466
+ *
467
+ * Built-in array methods not available on `Result` types, convert the result
468
+ * to a tuple using `$tuple()` first.
469
+ *
470
+ * @deprecated
471
+ */
472
+ some() {
473
+ throw new RetupleArrayMethodUnavailableError(this, "some");
474
+ }
475
+ /**
476
+ * ## Method not available
477
+ *
478
+ * Built-in array methods not available on `Result` types, convert the result
479
+ * to a tuple using `$tuple()` first.
480
+ *
481
+ * @deprecated
482
+ */
483
+ sort() {
484
+ throw new RetupleArrayMethodUnavailableError(this, "sort");
485
+ }
486
+ /**
487
+ * ## Method not available
488
+ *
489
+ * Built-in array methods not available on `Result` types, convert the result
490
+ * to a tuple using `$tuple()` first.
491
+ *
492
+ * @deprecated
493
+ */
494
+ splice() {
495
+ throw new RetupleArrayMethodUnavailableError(this, "splice");
496
+ }
497
+ /**
498
+ * ## Method not available
499
+ *
500
+ * Built-in array methods not available on `Result` types, convert the result
501
+ * to a tuple using `$tuple()` first.
502
+ *
503
+ * @deprecated
504
+ */
505
+ toString() {
506
+ throw new RetupleArrayMethodUnavailableError(this, "toString");
507
+ }
508
+ /**
509
+ * ## Method not available
510
+ *
511
+ * Built-in array methods not available on `Result` types, convert the result
512
+ * to a tuple using `$tuple()` first.
513
+ *
514
+ * @deprecated
515
+ */
516
+ toLocaleString() {
517
+ throw new RetupleArrayMethodUnavailableError(this, "toLocaleString");
518
+ }
519
+ /**
520
+ * ## Method not available
521
+ *
522
+ * Built-in array methods not available on `Result` types, convert the result
523
+ * to a tuple using `$tuple()` first.
524
+ *
525
+ * @deprecated
526
+ */
527
+ toReversed() {
528
+ throw new RetupleArrayMethodUnavailableError(this, "toReversed");
529
+ }
530
+ /**
531
+ * ## Method not available
532
+ *
533
+ * Built-in array methods not available on `Result` types, convert the result
534
+ * to a tuple using `$tuple()` first.
535
+ *
536
+ * @deprecated
537
+ */
538
+ toSorted() {
539
+ throw new RetupleArrayMethodUnavailableError(this, "toSorted");
540
+ }
541
+ /**
542
+ * ## Method not available
543
+ *
544
+ * Built-in array methods not available on `Result` types, convert the result
545
+ * to a tuple using `$tuple()` first.
546
+ *
547
+ * @deprecated
548
+ */
549
+ toSpliced() {
550
+ throw new RetupleArrayMethodUnavailableError(this, "toSpliced");
551
+ }
552
+ /**
553
+ * ## Method not available
554
+ *
555
+ * Built-in array methods not available on `Result` types, convert the result
556
+ * to a tuple using `$tuple()` first.
557
+ *
558
+ * @deprecated
559
+ */
560
+ unshift() {
561
+ throw new RetupleArrayMethodUnavailableError(this, "unshift");
562
+ }
563
+ /**
564
+ * ## Method not available
565
+ *
566
+ * Built-in array methods not available on `Result` types, convert the result
567
+ * to a tuple using `$tuple()` first.
568
+ *
569
+ * @deprecated
570
+ */
571
+ values() {
572
+ throw new RetupleArrayMethodUnavailableError(this, "values");
573
+ }
574
+ /**
575
+ * ## Method not available
576
+ *
577
+ * Built-in array methods not available on `Result` types, convert the result
578
+ * to a tuple using `$tuple()` first.
579
+ *
580
+ * @deprecated
581
+ */
582
+ with() {
583
+ throw new RetupleArrayMethodUnavailableError(this, "with");
584
+ }
585
+ }
586
+ /**
587
+ * ## ResultOk
588
+ *
589
+ * This is the `Ok` variant of a `Result`, used internally and not exported.
91
590
  */
92
- class ResultOk extends Array {
591
+ class ResultOk extends RetupleArray {
93
592
  constructor(value) {
94
593
  super(2);
95
594
  this[0] = undefined;
96
595
  this[1] = value;
97
596
  }
98
- $value() {
597
+ toJSON() {
99
598
  return this[1];
100
599
  }
101
600
  $isOk() {
102
601
  return true;
103
602
  }
104
603
  $isOkAnd(f) {
105
- return f(this[1]);
604
+ return !!f(this[1]);
106
605
  }
107
606
  $isErr() {
108
607
  return false;
@@ -126,16 +625,22 @@ class ResultOk extends Array {
126
625
  return this[1];
127
626
  }
128
627
  $map(f) {
129
- return new ResultOk(f(this[1]));
628
+ return Ok(f(this[1]));
130
629
  }
131
630
  $mapErr() {
132
631
  return this;
133
632
  }
134
633
  $mapOr(_def, f) {
135
- return new ResultOk(f(this[1]));
634
+ return Ok(f(this[1]));
136
635
  }
137
636
  $mapOrElse(_def, f) {
138
- return new ResultOk(f(this[1]));
637
+ return Ok(f(this[1]));
638
+ }
639
+ $andAssertOr(def, condition = isTruthy) {
640
+ return condition(this[1]) ? this : def;
641
+ }
642
+ $andAssertOrElse(def, condition = isTruthy) {
643
+ return condition(this[1]) ? this : def(this[1]);
139
644
  }
140
645
  $or() {
141
646
  return this;
@@ -158,10 +663,10 @@ class ResultOk extends Array {
158
663
  }
159
664
  $andSafe(f, mapError = ensureError) {
160
665
  try {
161
- return new ResultOk(f(this[1]));
666
+ return Ok(f(this[1]));
162
667
  }
163
668
  catch (err) {
164
- return new ResultErr(mapError(err));
669
+ return Err(mapError(err));
165
670
  }
166
671
  }
167
672
  $peek(f) {
@@ -176,7 +681,11 @@ class ResultOk extends Array {
176
681
  return this;
177
682
  }
178
683
  $flatten() {
179
- return this[1];
684
+ const inner = this[1];
685
+ if (inner instanceof ResultOk || inner instanceof ResultErr) {
686
+ return inner;
687
+ }
688
+ throw new RetupleFlattenFailed(this[1]);
180
689
  }
181
690
  $async() {
182
691
  return new ResultAsync(Promise.resolve(this));
@@ -184,20 +693,26 @@ class ResultOk extends Array {
184
693
  $promise() {
185
694
  return Promise.resolve(this);
186
695
  }
696
+ $tuple() {
697
+ return [undefined, this[1]];
698
+ }
699
+ *$iter() {
700
+ yield* this[1];
701
+ }
187
702
  }
188
703
  /**
189
- * ## Err
704
+ * ## ResultErr
190
705
  *
191
- * @TODO
706
+ * This is the `Err` variant of a `Result`, used internally and not exported.
192
707
  */
193
- class ResultErr extends Array {
708
+ class ResultErr extends RetupleArray {
194
709
  constructor(err) {
195
710
  super(2);
196
711
  this[0] = err;
197
712
  this[1] = undefined;
198
713
  }
199
- $value() {
200
- return this[0];
714
+ toJSON() {
715
+ return null;
201
716
  }
202
717
  $isOk() {
203
718
  return false;
@@ -209,7 +724,7 @@ class ResultErr extends Array {
209
724
  return true;
210
725
  }
211
726
  $isErrAnd(f) {
212
- return f(this[0]);
727
+ return !!f(this[0]);
213
728
  }
214
729
  $expect() {
215
730
  if (this[0] instanceof Error) {
@@ -233,13 +748,19 @@ class ResultErr extends Array {
233
748
  return this;
234
749
  }
235
750
  $mapErr(f) {
236
- return new ResultErr(f(this[0]));
751
+ return Err(f(this[0]));
237
752
  }
238
753
  $mapOr(def) {
239
- return new ResultOk(def);
754
+ return Ok(def);
240
755
  }
241
756
  $mapOrElse(def) {
242
- return new ResultOk(def(this[0]));
757
+ return Ok(def(this[0]));
758
+ }
759
+ $andAssertOr() {
760
+ return this;
761
+ }
762
+ $andAssertOrElse() {
763
+ return this;
243
764
  }
244
765
  $or(or) {
245
766
  return or;
@@ -249,10 +770,10 @@ class ResultErr extends Array {
249
770
  }
250
771
  $orSafe(f, mapError = ensureError) {
251
772
  try {
252
- return new ResultOk(f(this[0]));
773
+ return Ok(f(this[0]));
253
774
  }
254
775
  catch (err) {
255
- return new ResultErr(mapError(err));
776
+ return Err(mapError(err));
256
777
  }
257
778
  }
258
779
  $and() {
@@ -287,6 +808,12 @@ class ResultErr extends Array {
287
808
  $promise() {
288
809
  return Promise.resolve(this);
289
810
  }
811
+ $tuple() {
812
+ return [this[0], undefined];
813
+ }
814
+ *$iter() {
815
+ return;
816
+ }
290
817
  }
291
818
  /**
292
819
  * ## ResultAsync
@@ -302,133 +829,137 @@ class ResultAsync {
302
829
  return __classPrivateFieldGet(this, _ResultAsync_inner, "f").then(onfulfilled, onrejected);
303
830
  }
304
831
  /**
305
- * @TODO
306
- */
307
- async $value() {
308
- return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$value();
309
- }
310
- /**
311
- * @TODO
832
+ * The same as {@link Retuple.$expect|$expect}, except it returns a `Promise`.
312
833
  */
313
834
  async $expect() {
314
835
  return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$expect();
315
836
  }
316
837
  /**
317
- * @TODO
838
+ * The same as {@link Retuple.$unwrap|$unwrap}, except it returns a `Promise`.
318
839
  */
319
840
  async $unwrap(msg) {
320
841
  return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$unwrap(msg);
321
842
  }
322
843
  /**
323
- * @TODO
844
+ * The same as {@link Retuple.$unwrapErr|$unwrapErr}, except it returns
845
+ * a `Promise`.
324
846
  */
325
847
  async $unwrapErr(msg) {
326
848
  return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$unwrapErr(msg);
327
849
  }
328
850
  /**
329
- * @TODO
851
+ * The same as {@link Retuple.$unwrapOr|$unwrapOr}, except it returns
852
+ * a `Promise`.
330
853
  */
331
854
  async $unwrapOr(def) {
332
855
  return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$unwrapOr(def);
333
856
  }
334
857
  /**
335
- * @TODO
858
+ * The same as {@link Retuple.$unwrapOrElse|$unwrapOrElse}, except it returns
859
+ * a `Promise`.
336
860
  */
337
861
  async $unwrapOrElse(f) {
338
862
  const res = await __classPrivateFieldGet(this, _ResultAsync_inner, "f");
339
863
  return res instanceof ResultOk ? res[1] : f();
340
864
  }
341
865
  /**
342
- * @TODO
866
+ * The same as {@link Retuple.$map|$map}, except it returns `ResultAsync`.
343
867
  */
344
868
  $map(f) {
345
869
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
346
- return res instanceof ResultOk
347
- ? new ResultOk(f(res[1]))
348
- : res;
870
+ return res instanceof ResultOk ? Ok(f(res[1])) : res;
349
871
  }));
350
872
  }
351
873
  /**
352
- * @TODO
874
+ * The same as {@link Retuple.$mapErr|$mapErr}, except it returns
875
+ * `ResultAsync`.
353
876
  */
354
877
  $mapErr(f) {
355
878
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
356
- return res instanceof ResultErr
357
- ? new ResultErr(f(res[0]))
358
- : res;
879
+ return res instanceof ResultErr ? Err(f(res[0])) : res;
359
880
  }));
360
881
  }
361
882
  /**
362
- * @TODO
883
+ * The same as {@link Retuple.$mapOr|$mapOr}, except it returns `ResultAsync`.
363
884
  */
364
885
  $mapOr(def, f) {
365
886
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
366
- return res instanceof ResultOk
367
- ? new ResultOk(f(res[1]))
368
- : new ResultOk(def);
887
+ return res instanceof ResultOk ? Ok(f(res[1])) : Ok(def);
369
888
  }));
370
889
  }
371
890
  /**
372
- * @TODO
891
+ * The same as {@link Retuple.$mapOrElse|$mapOrElse}, except it returns
892
+ * `ResultAsync`.
373
893
  */
374
894
  $mapOrElse(def, f) {
375
895
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
376
- return res instanceof ResultOk
377
- ? new ResultOk(f(res[1]))
378
- : new ResultOk(def(res[0]));
896
+ return res instanceof ResultOk ? Ok(f(res[1])) : Ok(def(res[0]));
897
+ }));
898
+ }
899
+ $andAssertOr(def, condition = isTruthy) {
900
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
901
+ if (res instanceof ResultErr || condition(res[1])) {
902
+ return res;
903
+ }
904
+ return await def;
905
+ }));
906
+ }
907
+ $andAssertOrElse(def, condition = isTruthy) {
908
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
909
+ if (res instanceof ResultErr || condition(res[1])) {
910
+ return res;
911
+ }
912
+ return await def(res[1]);
379
913
  }));
380
914
  }
381
- /**
382
- * @TODO
383
- */
384
915
  $or(or) {
385
916
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
386
917
  return res instanceof ResultErr ? await or : res;
387
918
  }));
388
919
  }
389
- /**
390
- * @TODO
391
- */
392
920
  $orElse(f) {
393
921
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
394
- return res instanceof ResultErr ? await f(res[0]) : res;
922
+ return res instanceof ResultErr
923
+ ? await f(res[0])
924
+ : res;
395
925
  }));
396
926
  }
397
- /**
398
- * @TODO
399
- */
400
927
  $orSafe(f, mapError = ensureError) {
401
928
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
402
929
  if (res instanceof ResultOk) {
403
930
  return res;
404
931
  }
405
932
  try {
406
- return new ResultOk(await f(res[0]));
933
+ return Ok(await f(res[0]));
407
934
  }
408
935
  catch (err) {
409
- return new ResultErr(mapError(err));
936
+ return Err(mapError(err));
937
+ }
938
+ }));
939
+ }
940
+ $orSafePromise(promise, mapError = ensureError) {
941
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
942
+ if (res instanceof ResultOk) {
943
+ return res;
944
+ }
945
+ try {
946
+ return Ok(await promise);
947
+ }
948
+ catch (err) {
949
+ return Err(mapError(err));
410
950
  }
411
951
  }));
412
952
  }
413
- /**
414
- * @TODO
415
- */
416
953
  $and(and) {
417
954
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
418
955
  return res instanceof ResultOk ? await and : res;
419
956
  }));
420
957
  }
421
- /**
422
- * @TODO
423
- */
424
958
  $andThen(f) {
425
959
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
426
960
  return res instanceof ResultOk ? await f(res[1]) : res;
427
961
  }));
428
962
  }
429
- /**
430
- * @TODO
431
- */
432
963
  $andThrough(f) {
433
964
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
434
965
  if (res instanceof ResultOk) {
@@ -440,24 +971,37 @@ class ResultAsync {
440
971
  return res;
441
972
  }));
442
973
  }
443
- /**
444
- * @TODO
445
- */
446
974
  $andSafe(f, mapError = ensureError) {
447
975
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
448
976
  if (res instanceof ResultErr) {
449
977
  return res;
450
978
  }
451
979
  try {
452
- return new ResultOk(await f(res[1]));
980
+ return Ok(await f(res[1]));
453
981
  }
454
982
  catch (err) {
455
- return new ResultErr(mapError(err));
983
+ return Err(mapError(err));
984
+ }
985
+ }));
986
+ }
987
+ $andSafePromise(promise, mapError = ensureError) {
988
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
989
+ if (res instanceof ResultErr) {
990
+ return res;
991
+ }
992
+ try {
993
+ return Ok(await promise);
994
+ }
995
+ catch (err) {
996
+ return Err(mapError(err));
456
997
  }
457
998
  }));
458
999
  }
459
1000
  /**
460
- * @TODO
1001
+ * The same as {@link Retuple.$peek|$peek}, except it:
1002
+ *
1003
+ * - awaits the peek function;
1004
+ * - returns `ResultAsync`.
461
1005
  */
462
1006
  $peek(f) {
463
1007
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
@@ -466,7 +1010,10 @@ class ResultAsync {
466
1010
  }));
467
1011
  }
468
1012
  /**
469
- * @TODO
1013
+ * The same as {@link Retuple.$tap|$tap}, except it:
1014
+ *
1015
+ * - awaits the tap function;
1016
+ * - returns `ResultAsync`.
470
1017
  */
471
1018
  $tap(f) {
472
1019
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
@@ -477,7 +1024,10 @@ class ResultAsync {
477
1024
  }));
478
1025
  }
479
1026
  /**
480
- * @TODO
1027
+ * The same as {@link Retuple.$tapErr|$tapErr}, except it:
1028
+ *
1029
+ * - awaits the tap error function;
1030
+ * - returns `ResultAsync`.
481
1031
  */
482
1032
  $tapErr(f) {
483
1033
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
@@ -488,11 +1038,23 @@ class ResultAsync {
488
1038
  }));
489
1039
  }
490
1040
  /**
491
- * @TODO
1041
+ * The same as {@link Retuple.$promise|$promise}.
492
1042
  */
493
1043
  $promise() {
494
1044
  return Promise.resolve(this);
495
1045
  }
1046
+ /**
1047
+ * The same as {@link Retuple.$tuple|$tuple}, except it returns a `Promise`.
1048
+ */
1049
+ async $tuple() {
1050
+ return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$tuple();
1051
+ }
1052
+ /**
1053
+ * The same as {@link Retuple.$tuple|$iter}, except it returns a `Promise`.
1054
+ */
1055
+ async $iter() {
1056
+ return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$iter();
1057
+ }
496
1058
  }
497
1059
  _ResultAsync_inner = new WeakMap();
498
1060
  function ensureError(err) {
@@ -501,18 +1063,9 @@ function ensureError(err) {
501
1063
  }
502
1064
  return new RetupleThrownValueError(err);
503
1065
  }
504
- Object.freeze(Result);
505
- Object.freeze(ResultOk);
506
- Object.freeze(ResultErr);
507
- Object.freeze(ResultAsync);
508
- Object.freeze(RetupleUnwrapFailed);
509
- Object.freeze(RetupleUnwrapErrFailed);
510
- Object.freeze(RetupleExpectFailed);
511
- Object.freeze(RetupleThrownValueError);
512
- Object.freeze(ResultOk.prototype);
513
- Object.freeze(ResultErr.prototype);
514
- Object.freeze(ResultAsync.prototype);
515
- Object.freeze(RetupleUnwrapFailed.prototype);
516
- Object.freeze(RetupleUnwrapErrFailed.prototype);
517
- Object.freeze(RetupleExpectFailed.prototype);
518
- Object.freeze(RetupleThrownValueError.prototype);
1066
+ function mapTrue() {
1067
+ return true;
1068
+ }
1069
+ function isTruthy(val) {
1070
+ return !!val;
1071
+ }