retuple 1.0.0-next.1 → 1.0.0-next.11

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,60 +10,145 @@ 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.$resolve = resolve;
116
+ Result.$nonNullable = nonNullable;
117
+ Result.$truthy = truthy;
118
+ Result.$safe = safe;
119
+ Result.$safeAsync = safeAsync;
120
+ Result.$safePromise = safePromise;
121
+ Object.freeze(Result);
51
122
  export function Ok(val) {
52
123
  return new ResultOk(val);
53
124
  }
54
125
  export function Err(err) {
55
126
  return new ResultErr(err);
56
127
  }
57
- export function from(value, error) {
58
- if (value) {
59
- return new ResultOk(value);
128
+ function resolve(result) {
129
+ if (result instanceof ResultAsync) {
130
+ return result;
60
131
  }
61
- if (error) {
62
- return new ResultErr(error());
132
+ else if (result instanceof ResultOk || result instanceof ResultErr) {
133
+ return result.$async();
63
134
  }
64
- return new ResultErr(true);
135
+ else {
136
+ return new ResultAsync(result);
137
+ }
138
+ }
139
+ function nonNullable(value, error = mapTrue) {
140
+ if (value !== null && value !== undefined) {
141
+ return Ok(value);
142
+ }
143
+ return Err(error());
65
144
  }
66
- export function safe(f, mapError = ensureError) {
145
+ function truthy(value, error = mapTrue) {
146
+ if (value) {
147
+ return Ok(value);
148
+ }
149
+ return Err(error());
150
+ }
151
+ function safe(f, mapError = ensureError) {
67
152
  try {
68
153
  return Ok(f());
69
154
  }
@@ -71,7 +156,7 @@ export function safe(f, mapError = ensureError) {
71
156
  return Err(mapError(err));
72
157
  }
73
158
  }
74
- export function safeAsync(f, mapError = ensureError) {
159
+ function safeAsync(f, mapError = ensureError) {
75
160
  return new ResultAsync((async () => {
76
161
  try {
77
162
  return Ok(await f());
@@ -81,28 +166,454 @@ export function safeAsync(f, mapError = ensureError) {
81
166
  }
82
167
  })());
83
168
  }
84
- export function safePromise(promise, mapError = ensureError) {
169
+ function safePromise(promise, mapError = ensureError) {
85
170
  return new ResultAsync(promise.then((Ok), async (err) => Err(await mapError(err))));
86
171
  }
87
172
  /**
88
- * ## Ok
173
+ * ## RetupleArray
89
174
  *
90
- * @TODO
175
+ * Using built-in array methods on a `Result` is probably a mistake. This class
176
+ * makes the built-in methods throw `RetupleArrayMethodUnavailableError`.
177
+ */
178
+ class RetupleArray extends Array {
179
+ /**
180
+ * ## Method not available
181
+ *
182
+ * Built-in array methods not available on `Result` types, convert the result
183
+ * to a tuple using `$tuple()` first.
184
+ *
185
+ * @deprecated
186
+ */
187
+ at() {
188
+ throw new RetupleArrayMethodUnavailableError(this, "at");
189
+ }
190
+ /**
191
+ * ## Method not available
192
+ *
193
+ * Built-in array methods not available on `Result` types, convert the result
194
+ * to a tuple using `$tuple()` first.
195
+ *
196
+ * @deprecated
197
+ */
198
+ concat() {
199
+ throw new RetupleArrayMethodUnavailableError(this, "concat");
200
+ }
201
+ /**
202
+ * ## Method not available
203
+ *
204
+ * Built-in array methods not available on `Result` types, convert the result
205
+ * to a tuple using `$tuple()` first.
206
+ *
207
+ * @deprecated
208
+ */
209
+ copyWithin() {
210
+ throw new RetupleArrayMethodUnavailableError(this, "copyWithin");
211
+ }
212
+ /**
213
+ * ## Method not available
214
+ *
215
+ * Built-in array methods not available on `Result` types, convert the result
216
+ * to a tuple using `$tuple()` first.
217
+ *
218
+ * @deprecated
219
+ */
220
+ entries() {
221
+ throw new RetupleArrayMethodUnavailableError(this, "entries");
222
+ }
223
+ /**
224
+ * ## Method not available
225
+ *
226
+ * Built-in array methods not available on `Result` types, convert the result
227
+ * to a tuple using `$tuple()` first.
228
+ *
229
+ * @deprecated
230
+ */
231
+ every() {
232
+ throw new RetupleArrayMethodUnavailableError(this, "every");
233
+ }
234
+ /**
235
+ * ## Method not available
236
+ *
237
+ * Built-in array methods not available on `Result` types, convert the result
238
+ * to a tuple using `$tuple()` first.
239
+ *
240
+ * @deprecated
241
+ */
242
+ fill() {
243
+ throw new RetupleArrayMethodUnavailableError(this, "fill");
244
+ }
245
+ /**
246
+ * ## Method not available
247
+ *
248
+ * Built-in array methods not available on `Result` types, convert the result
249
+ * to a tuple using `$tuple()` first.
250
+ *
251
+ * @deprecated
252
+ */
253
+ filter() {
254
+ throw new RetupleArrayMethodUnavailableError(this, "filter");
255
+ }
256
+ /**
257
+ * ## Method not available
258
+ *
259
+ * Built-in array methods not available on `Result` types, convert the result
260
+ * to a tuple using `$tuple()` first.
261
+ *
262
+ * @deprecated
263
+ */
264
+ find() {
265
+ throw new RetupleArrayMethodUnavailableError(this, "find");
266
+ }
267
+ /**
268
+ * ## Method not available
269
+ *
270
+ * Built-in array methods not available on `Result` types, convert the result
271
+ * to a tuple using `$tuple()` first.
272
+ *
273
+ * @deprecated
274
+ */
275
+ findIndex() {
276
+ throw new RetupleArrayMethodUnavailableError(this, "findIndex");
277
+ }
278
+ /**
279
+ * ## Method not available
280
+ *
281
+ * Built-in array methods not available on `Result` types, convert the result
282
+ * to a tuple using `$tuple()` first.
283
+ *
284
+ * @deprecated
285
+ */
286
+ findLast() {
287
+ throw new RetupleArrayMethodUnavailableError(this, "findLast");
288
+ }
289
+ /**
290
+ * ## Method not available
291
+ *
292
+ * Built-in array methods not available on `Result` types, convert the result
293
+ * to a tuple using `$tuple()` first.
294
+ *
295
+ * @deprecated
296
+ */
297
+ findLastIndex() {
298
+ throw new RetupleArrayMethodUnavailableError(this, "findLastIndex");
299
+ }
300
+ /**
301
+ * ## Method not available
302
+ *
303
+ * Built-in array methods not available on `Result` types, convert the result
304
+ * to a tuple using `$tuple()` first.
305
+ *
306
+ * @deprecated
307
+ */
308
+ flat() {
309
+ throw new RetupleArrayMethodUnavailableError(this, "flat");
310
+ }
311
+ /**
312
+ * ## Method not available
313
+ *
314
+ * Built-in array methods not available on `Result` types, convert the result
315
+ * to a tuple using `$tuple()` first.
316
+ *
317
+ * @deprecated
318
+ */
319
+ flatMap() {
320
+ throw new RetupleArrayMethodUnavailableError(this, "flatMap");
321
+ }
322
+ /**
323
+ * ## Method not available
324
+ *
325
+ * Built-in array methods not available on `Result` types, convert the result
326
+ * to a tuple using `$tuple()` first.
327
+ *
328
+ * @deprecated
329
+ */
330
+ forEach() {
331
+ throw new RetupleArrayMethodUnavailableError(this, "forEach");
332
+ }
333
+ /**
334
+ * ## Method not available
335
+ *
336
+ * Built-in array methods not available on `Result` types, convert the result
337
+ * to a tuple using `$tuple()` first.
338
+ *
339
+ * @deprecated
340
+ */
341
+ includes() {
342
+ throw new RetupleArrayMethodUnavailableError(this, "includes");
343
+ }
344
+ /**
345
+ * ## Method not available
346
+ *
347
+ * Built-in array methods not available on `Result` types, convert the result
348
+ * to a tuple using `$tuple()` first.
349
+ *
350
+ * @deprecated
351
+ */
352
+ indexOf() {
353
+ throw new RetupleArrayMethodUnavailableError(this, "indexOf");
354
+ }
355
+ /**
356
+ * ## Method not available
357
+ *
358
+ * Built-in array methods not available on `Result` types, convert the result
359
+ * to a tuple using `$tuple()` first.
360
+ *
361
+ * @deprecated
362
+ */
363
+ join() {
364
+ throw new RetupleArrayMethodUnavailableError(this, "join");
365
+ }
366
+ /**
367
+ * ## Method not available
368
+ *
369
+ * Built-in array methods not available on `Result` types, convert the result
370
+ * to a tuple using `$tuple()` first.
371
+ *
372
+ * @deprecated
373
+ */
374
+ keys() {
375
+ throw new RetupleArrayMethodUnavailableError(this, "keys");
376
+ }
377
+ /**
378
+ * ## Method not available
379
+ *
380
+ * Built-in array methods not available on `Result` types, convert the result
381
+ * to a tuple using `$tuple()` first.
382
+ *
383
+ * @deprecated
384
+ */
385
+ lastIndexOf() {
386
+ throw new RetupleArrayMethodUnavailableError(this, "lastIndexOf");
387
+ }
388
+ /**
389
+ * ## Method not available
390
+ *
391
+ * Built-in array methods not available on `Result` types, convert the result
392
+ * to a tuple using `$tuple()` first.
393
+ *
394
+ * @deprecated
395
+ */
396
+ map() {
397
+ throw new RetupleArrayMethodUnavailableError(this, "map");
398
+ }
399
+ /**
400
+ * ## Method not available
401
+ *
402
+ * Built-in array methods not available on `Result` types, convert the result
403
+ * to a tuple using `$tuple()` first.
404
+ *
405
+ * @deprecated
406
+ */
407
+ pop() {
408
+ throw new RetupleArrayMethodUnavailableError(this, "pop");
409
+ }
410
+ /**
411
+ * ## Method not available
412
+ *
413
+ * Built-in array methods not available on `Result` types, convert the result
414
+ * to a tuple using `$tuple()` first.
415
+ *
416
+ * @deprecated
417
+ */
418
+ push() {
419
+ throw new RetupleArrayMethodUnavailableError(this, "push");
420
+ }
421
+ /**
422
+ * ## Method not available
423
+ *
424
+ * Built-in array methods not available on `Result` types, convert the result
425
+ * to a tuple using `$tuple()` first.
426
+ *
427
+ * @deprecated
428
+ */
429
+ reduce() {
430
+ throw new RetupleArrayMethodUnavailableError(this, "reduce");
431
+ }
432
+ /**
433
+ * ## Method not available
434
+ *
435
+ * Built-in array methods not available on `Result` types, convert the result
436
+ * to a tuple using `$tuple()` first.
437
+ *
438
+ * @deprecated
439
+ */
440
+ reduceRight() {
441
+ throw new RetupleArrayMethodUnavailableError(this, "reduceRight");
442
+ }
443
+ /**
444
+ * ## Method not available
445
+ *
446
+ * Built-in array methods not available on `Result` types, convert the result
447
+ * to a tuple using `$tuple()` first.
448
+ *
449
+ * @deprecated
450
+ */
451
+ reverse() {
452
+ throw new RetupleArrayMethodUnavailableError(this, "reverse");
453
+ }
454
+ /**
455
+ * ## Method not available
456
+ *
457
+ * Built-in array methods not available on `Result` types, convert the result
458
+ * to a tuple using `$tuple()` first.
459
+ *
460
+ * @deprecated
461
+ */
462
+ shift() {
463
+ throw new RetupleArrayMethodUnavailableError(this, "shift");
464
+ }
465
+ /**
466
+ * ## Method not available
467
+ *
468
+ * Built-in array methods not available on `Result` types, convert the result
469
+ * to a tuple using `$tuple()` first.
470
+ *
471
+ * @deprecated
472
+ */
473
+ slice() {
474
+ throw new RetupleArrayMethodUnavailableError(this, "slice");
475
+ }
476
+ /**
477
+ * ## Method not available
478
+ *
479
+ * Built-in array methods not available on `Result` types, convert the result
480
+ * to a tuple using `$tuple()` first.
481
+ *
482
+ * @deprecated
483
+ */
484
+ some() {
485
+ throw new RetupleArrayMethodUnavailableError(this, "some");
486
+ }
487
+ /**
488
+ * ## Method not available
489
+ *
490
+ * Built-in array methods not available on `Result` types, convert the result
491
+ * to a tuple using `$tuple()` first.
492
+ *
493
+ * @deprecated
494
+ */
495
+ sort() {
496
+ throw new RetupleArrayMethodUnavailableError(this, "sort");
497
+ }
498
+ /**
499
+ * ## Method not available
500
+ *
501
+ * Built-in array methods not available on `Result` types, convert the result
502
+ * to a tuple using `$tuple()` first.
503
+ *
504
+ * @deprecated
505
+ */
506
+ splice() {
507
+ throw new RetupleArrayMethodUnavailableError(this, "splice");
508
+ }
509
+ /**
510
+ * ## Method not available
511
+ *
512
+ * Built-in array methods not available on `Result` types, convert the result
513
+ * to a tuple using `$tuple()` first.
514
+ *
515
+ * @deprecated
516
+ */
517
+ toString() {
518
+ throw new RetupleArrayMethodUnavailableError(this, "toString");
519
+ }
520
+ /**
521
+ * ## Method not available
522
+ *
523
+ * Built-in array methods not available on `Result` types, convert the result
524
+ * to a tuple using `$tuple()` first.
525
+ *
526
+ * @deprecated
527
+ */
528
+ toLocaleString() {
529
+ throw new RetupleArrayMethodUnavailableError(this, "toLocaleString");
530
+ }
531
+ /**
532
+ * ## Method not available
533
+ *
534
+ * Built-in array methods not available on `Result` types, convert the result
535
+ * to a tuple using `$tuple()` first.
536
+ *
537
+ * @deprecated
538
+ */
539
+ toReversed() {
540
+ throw new RetupleArrayMethodUnavailableError(this, "toReversed");
541
+ }
542
+ /**
543
+ * ## Method not available
544
+ *
545
+ * Built-in array methods not available on `Result` types, convert the result
546
+ * to a tuple using `$tuple()` first.
547
+ *
548
+ * @deprecated
549
+ */
550
+ toSorted() {
551
+ throw new RetupleArrayMethodUnavailableError(this, "toSorted");
552
+ }
553
+ /**
554
+ * ## Method not available
555
+ *
556
+ * Built-in array methods not available on `Result` types, convert the result
557
+ * to a tuple using `$tuple()` first.
558
+ *
559
+ * @deprecated
560
+ */
561
+ toSpliced() {
562
+ throw new RetupleArrayMethodUnavailableError(this, "toSpliced");
563
+ }
564
+ /**
565
+ * ## Method not available
566
+ *
567
+ * Built-in array methods not available on `Result` types, convert the result
568
+ * to a tuple using `$tuple()` first.
569
+ *
570
+ * @deprecated
571
+ */
572
+ unshift() {
573
+ throw new RetupleArrayMethodUnavailableError(this, "unshift");
574
+ }
575
+ /**
576
+ * ## Method not available
577
+ *
578
+ * Built-in array methods not available on `Result` types, convert the result
579
+ * to a tuple using `$tuple()` first.
580
+ *
581
+ * @deprecated
582
+ */
583
+ values() {
584
+ throw new RetupleArrayMethodUnavailableError(this, "values");
585
+ }
586
+ /**
587
+ * ## Method not available
588
+ *
589
+ * Built-in array methods not available on `Result` types, convert the result
590
+ * to a tuple using `$tuple()` first.
591
+ *
592
+ * @deprecated
593
+ */
594
+ with() {
595
+ throw new RetupleArrayMethodUnavailableError(this, "with");
596
+ }
597
+ }
598
+ /**
599
+ * ## ResultOk
600
+ *
601
+ * This is the `Ok` variant of a `Result`, used internally and not exported.
91
602
  */
92
- class ResultOk extends Array {
603
+ class ResultOk extends RetupleArray {
93
604
  constructor(value) {
94
605
  super(2);
95
606
  this[0] = undefined;
96
607
  this[1] = value;
97
608
  }
98
- $value() {
609
+ toJSON() {
99
610
  return this[1];
100
611
  }
101
612
  $isOk() {
102
613
  return true;
103
614
  }
104
615
  $isOkAnd(f) {
105
- return f(this[1]);
616
+ return !!f(this[1]);
106
617
  }
107
618
  $isErr() {
108
619
  return false;
@@ -126,16 +637,22 @@ class ResultOk extends Array {
126
637
  return this[1];
127
638
  }
128
639
  $map(f) {
129
- return new ResultOk(f(this[1]));
640
+ return Ok(f(this[1]));
130
641
  }
131
642
  $mapErr() {
132
643
  return this;
133
644
  }
134
645
  $mapOr(_def, f) {
135
- return new ResultOk(f(this[1]));
646
+ return Ok(f(this[1]));
136
647
  }
137
648
  $mapOrElse(_def, f) {
138
- return new ResultOk(f(this[1]));
649
+ return Ok(f(this[1]));
650
+ }
651
+ $andAssertOr(def, condition = isTruthy) {
652
+ return condition(this[1]) ? this : def;
653
+ }
654
+ $andAssertOrElse(def, condition = isTruthy) {
655
+ return condition(this[1]) ? this : def(this[1]);
139
656
  }
140
657
  $or() {
141
658
  return this;
@@ -158,10 +675,10 @@ class ResultOk extends Array {
158
675
  }
159
676
  $andSafe(f, mapError = ensureError) {
160
677
  try {
161
- return new ResultOk(f(this[1]));
678
+ return Ok(f(this[1]));
162
679
  }
163
680
  catch (err) {
164
- return new ResultErr(mapError(err));
681
+ return Err(mapError(err));
165
682
  }
166
683
  }
167
684
  $peek(f) {
@@ -176,7 +693,11 @@ class ResultOk extends Array {
176
693
  return this;
177
694
  }
178
695
  $flatten() {
179
- return this[1];
696
+ const inner = this[1];
697
+ if (inner instanceof ResultOk || inner instanceof ResultErr) {
698
+ return inner;
699
+ }
700
+ throw new RetupleFlattenFailed(this[1]);
180
701
  }
181
702
  $async() {
182
703
  return new ResultAsync(Promise.resolve(this));
@@ -184,20 +705,26 @@ class ResultOk extends Array {
184
705
  $promise() {
185
706
  return Promise.resolve(this);
186
707
  }
708
+ $tuple() {
709
+ return [undefined, this[1]];
710
+ }
711
+ *$iter() {
712
+ yield* this[1];
713
+ }
187
714
  }
188
715
  /**
189
- * ## Err
716
+ * ## ResultErr
190
717
  *
191
- * @TODO
718
+ * This is the `Err` variant of a `Result`, used internally and not exported.
192
719
  */
193
- class ResultErr extends Array {
720
+ class ResultErr extends RetupleArray {
194
721
  constructor(err) {
195
722
  super(2);
196
723
  this[0] = err;
197
724
  this[1] = undefined;
198
725
  }
199
- $value() {
200
- return this[0];
726
+ toJSON() {
727
+ return null;
201
728
  }
202
729
  $isOk() {
203
730
  return false;
@@ -209,7 +736,7 @@ class ResultErr extends Array {
209
736
  return true;
210
737
  }
211
738
  $isErrAnd(f) {
212
- return f(this[0]);
739
+ return !!f(this[0]);
213
740
  }
214
741
  $expect() {
215
742
  if (this[0] instanceof Error) {
@@ -233,13 +760,19 @@ class ResultErr extends Array {
233
760
  return this;
234
761
  }
235
762
  $mapErr(f) {
236
- return new ResultErr(f(this[0]));
763
+ return Err(f(this[0]));
237
764
  }
238
765
  $mapOr(def) {
239
- return new ResultOk(def);
766
+ return Ok(def);
240
767
  }
241
768
  $mapOrElse(def) {
242
- return new ResultOk(def(this[0]));
769
+ return Ok(def(this[0]));
770
+ }
771
+ $andAssertOr() {
772
+ return this;
773
+ }
774
+ $andAssertOrElse() {
775
+ return this;
243
776
  }
244
777
  $or(or) {
245
778
  return or;
@@ -249,10 +782,10 @@ class ResultErr extends Array {
249
782
  }
250
783
  $orSafe(f, mapError = ensureError) {
251
784
  try {
252
- return new ResultOk(f(this[0]));
785
+ return Ok(f(this[0]));
253
786
  }
254
787
  catch (err) {
255
- return new ResultErr(mapError(err));
788
+ return Err(mapError(err));
256
789
  }
257
790
  }
258
791
  $and() {
@@ -287,6 +820,12 @@ class ResultErr extends Array {
287
820
  $promise() {
288
821
  return Promise.resolve(this);
289
822
  }
823
+ $tuple() {
824
+ return [this[0], undefined];
825
+ }
826
+ *$iter() {
827
+ return;
828
+ }
290
829
  }
291
830
  /**
292
831
  * ## ResultAsync
@@ -302,133 +841,137 @@ class ResultAsync {
302
841
  return __classPrivateFieldGet(this, _ResultAsync_inner, "f").then(onfulfilled, onrejected);
303
842
  }
304
843
  /**
305
- * @TODO
306
- */
307
- async $value() {
308
- return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$value();
309
- }
310
- /**
311
- * @TODO
844
+ * The same as {@link Retuple.$expect|$expect}, except it returns a `Promise`.
312
845
  */
313
846
  async $expect() {
314
847
  return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$expect();
315
848
  }
316
849
  /**
317
- * @TODO
850
+ * The same as {@link Retuple.$unwrap|$unwrap}, except it returns a `Promise`.
318
851
  */
319
852
  async $unwrap(msg) {
320
853
  return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$unwrap(msg);
321
854
  }
322
855
  /**
323
- * @TODO
856
+ * The same as {@link Retuple.$unwrapErr|$unwrapErr}, except it returns
857
+ * a `Promise`.
324
858
  */
325
859
  async $unwrapErr(msg) {
326
860
  return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$unwrapErr(msg);
327
861
  }
328
862
  /**
329
- * @TODO
863
+ * The same as {@link Retuple.$unwrapOr|$unwrapOr}, except it returns
864
+ * a `Promise`.
330
865
  */
331
866
  async $unwrapOr(def) {
332
867
  return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$unwrapOr(def);
333
868
  }
334
869
  /**
335
- * @TODO
870
+ * The same as {@link Retuple.$unwrapOrElse|$unwrapOrElse}, except it returns
871
+ * a `Promise`.
336
872
  */
337
873
  async $unwrapOrElse(f) {
338
874
  const res = await __classPrivateFieldGet(this, _ResultAsync_inner, "f");
339
875
  return res instanceof ResultOk ? res[1] : f();
340
876
  }
341
877
  /**
342
- * @TODO
878
+ * The same as {@link Retuple.$map|$map}, except it returns `ResultAsync`.
343
879
  */
344
880
  $map(f) {
345
881
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
346
- return res instanceof ResultOk
347
- ? new ResultOk(f(res[1]))
348
- : res;
882
+ return res instanceof ResultOk ? Ok(f(res[1])) : res;
349
883
  }));
350
884
  }
351
885
  /**
352
- * @TODO
886
+ * The same as {@link Retuple.$mapErr|$mapErr}, except it returns
887
+ * `ResultAsync`.
353
888
  */
354
889
  $mapErr(f) {
355
890
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
356
- return res instanceof ResultErr
357
- ? new ResultErr(f(res[0]))
358
- : res;
891
+ return res instanceof ResultErr ? Err(f(res[0])) : res;
359
892
  }));
360
893
  }
361
894
  /**
362
- * @TODO
895
+ * The same as {@link Retuple.$mapOr|$mapOr}, except it returns `ResultAsync`.
363
896
  */
364
897
  $mapOr(def, f) {
365
898
  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);
899
+ return res instanceof ResultOk ? Ok(f(res[1])) : Ok(def);
369
900
  }));
370
901
  }
371
902
  /**
372
- * @TODO
903
+ * The same as {@link Retuple.$mapOrElse|$mapOrElse}, except it returns
904
+ * `ResultAsync`.
373
905
  */
374
906
  $mapOrElse(def, f) {
375
907
  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]));
908
+ return res instanceof ResultOk ? Ok(f(res[1])) : Ok(def(res[0]));
909
+ }));
910
+ }
911
+ $andAssertOr(def, condition = isTruthy) {
912
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
913
+ if (res instanceof ResultErr || condition(res[1])) {
914
+ return res;
915
+ }
916
+ return await def;
917
+ }));
918
+ }
919
+ $andAssertOrElse(def, condition = isTruthy) {
920
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
921
+ if (res instanceof ResultErr || condition(res[1])) {
922
+ return res;
923
+ }
924
+ return await def(res[1]);
379
925
  }));
380
926
  }
381
- /**
382
- * @TODO
383
- */
384
927
  $or(or) {
385
928
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
386
929
  return res instanceof ResultErr ? await or : res;
387
930
  }));
388
931
  }
389
- /**
390
- * @TODO
391
- */
392
932
  $orElse(f) {
393
933
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
394
- return res instanceof ResultErr ? await f(res[0]) : res;
934
+ return res instanceof ResultErr
935
+ ? await f(res[0])
936
+ : res;
395
937
  }));
396
938
  }
397
- /**
398
- * @TODO
399
- */
400
939
  $orSafe(f, mapError = ensureError) {
401
940
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
402
941
  if (res instanceof ResultOk) {
403
942
  return res;
404
943
  }
405
944
  try {
406
- return new ResultOk(await f(res[0]));
945
+ return Ok(await f(res[0]));
407
946
  }
408
947
  catch (err) {
409
- return new ResultErr(mapError(err));
948
+ return Err(mapError(err));
949
+ }
950
+ }));
951
+ }
952
+ $orSafePromise(promise, mapError = ensureError) {
953
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
954
+ if (res instanceof ResultOk) {
955
+ return res;
956
+ }
957
+ try {
958
+ return Ok(await promise);
959
+ }
960
+ catch (err) {
961
+ return Err(mapError(err));
410
962
  }
411
963
  }));
412
964
  }
413
- /**
414
- * @TODO
415
- */
416
965
  $and(and) {
417
966
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
418
967
  return res instanceof ResultOk ? await and : res;
419
968
  }));
420
969
  }
421
- /**
422
- * @TODO
423
- */
424
970
  $andThen(f) {
425
971
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
426
972
  return res instanceof ResultOk ? await f(res[1]) : res;
427
973
  }));
428
974
  }
429
- /**
430
- * @TODO
431
- */
432
975
  $andThrough(f) {
433
976
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
434
977
  if (res instanceof ResultOk) {
@@ -440,24 +983,37 @@ class ResultAsync {
440
983
  return res;
441
984
  }));
442
985
  }
443
- /**
444
- * @TODO
445
- */
446
986
  $andSafe(f, mapError = ensureError) {
447
987
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
448
988
  if (res instanceof ResultErr) {
449
989
  return res;
450
990
  }
451
991
  try {
452
- return new ResultOk(await f(res[1]));
992
+ return Ok(await f(res[1]));
993
+ }
994
+ catch (err) {
995
+ return Err(mapError(err));
996
+ }
997
+ }));
998
+ }
999
+ $andSafePromise(promise, mapError = ensureError) {
1000
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
1001
+ if (res instanceof ResultErr) {
1002
+ return res;
1003
+ }
1004
+ try {
1005
+ return Ok(await promise);
453
1006
  }
454
1007
  catch (err) {
455
- return new ResultErr(mapError(err));
1008
+ return Err(mapError(err));
456
1009
  }
457
1010
  }));
458
1011
  }
459
1012
  /**
460
- * @TODO
1013
+ * The same as {@link Retuple.$peek|$peek}, except it:
1014
+ *
1015
+ * - awaits the peek function;
1016
+ * - returns `ResultAsync`.
461
1017
  */
462
1018
  $peek(f) {
463
1019
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
@@ -466,7 +1022,10 @@ class ResultAsync {
466
1022
  }));
467
1023
  }
468
1024
  /**
469
- * @TODO
1025
+ * The same as {@link Retuple.$tap|$tap}, except it:
1026
+ *
1027
+ * - awaits the tap function;
1028
+ * - returns `ResultAsync`.
470
1029
  */
471
1030
  $tap(f) {
472
1031
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
@@ -477,7 +1036,10 @@ class ResultAsync {
477
1036
  }));
478
1037
  }
479
1038
  /**
480
- * @TODO
1039
+ * The same as {@link Retuple.$tapErr|$tapErr}, except it:
1040
+ *
1041
+ * - awaits the tap error function;
1042
+ * - returns `ResultAsync`.
481
1043
  */
482
1044
  $tapErr(f) {
483
1045
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
@@ -488,11 +1050,23 @@ class ResultAsync {
488
1050
  }));
489
1051
  }
490
1052
  /**
491
- * @TODO
1053
+ * The same as {@link Retuple.$promise|$promise}.
492
1054
  */
493
1055
  $promise() {
494
1056
  return Promise.resolve(this);
495
1057
  }
1058
+ /**
1059
+ * The same as {@link Retuple.$tuple|$tuple}, except it returns a `Promise`.
1060
+ */
1061
+ async $tuple() {
1062
+ return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$tuple();
1063
+ }
1064
+ /**
1065
+ * The same as {@link Retuple.$tuple|$iter}, except it returns a `Promise`.
1066
+ */
1067
+ async $iter() {
1068
+ return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$iter();
1069
+ }
496
1070
  }
497
1071
  _ResultAsync_inner = new WeakMap();
498
1072
  function ensureError(err) {
@@ -501,18 +1075,9 @@ function ensureError(err) {
501
1075
  }
502
1076
  return new RetupleThrownValueError(err);
503
1077
  }
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);
1078
+ function mapTrue() {
1079
+ return true;
1080
+ }
1081
+ function isTruthy(val) {
1082
+ return !!val;
1083
+ }