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