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