retuple 1.0.0-next.13 → 1.0.0-next.14

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
@@ -10,12 +10,13 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _ResultAsync_inner, _a, _ResultRetry_f, _ResultRetry_promise, _ResultRetry_times, _ResultRetry_attempt, _ResultRetry_aborted, _ResultRetry_abort, _ResultRetry_getDelay, _ResultRetry_errorHandler;
13
+ var _ResultAsync_inner, _a, _ResultRetry_f, _ResultRetry_promise, _ResultRetry_times, _ResultRetry_attempt, _ResultRetry_aborted, _ResultRetry_abort, _ResultRetry_getDelay, _ResultRetry_handler;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.RetupleArrayMethodUnavailableError = exports.RetupleInvalidUnionError = exports.RetupleInvalidResultError = exports.RetupleThrownValueError = exports.RetupleFlattenFailed = exports.RetupleExpectFailed = exports.RetupleUnwrapErrFailed = exports.RetupleUnwrapFailed = void 0;
15
+ exports.RetupleArrayMethodUnavailableError = exports.RetupleInvalidUnionError = exports.RetupleCaughtValueError = exports.RetupleExpectFailed = exports.RetupleUnwrapErrFailed = exports.RetupleUnwrapFailed = void 0;
16
16
  exports.Result = Result;
17
17
  exports.Ok = Ok;
18
18
  exports.Err = Err;
19
+ const symbol_js_1 = require("./symbol.cjs");
19
20
  /**
20
21
  * ## Retuple Unwrap Failed
21
22
  *
@@ -53,19 +54,6 @@ class RetupleExpectFailed extends Error {
53
54
  }
54
55
  }
55
56
  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
57
  /**
70
58
  * ## Retuple Thrown Value Error
71
59
  *
@@ -73,28 +61,13 @@ exports.RetupleFlattenFailed = RetupleFlattenFailed;
73
61
  * thrown error or rejected value is not an instance of `Error`, and when no
74
62
  * map error function is provided.
75
63
  */
76
- class RetupleThrownValueError extends Error {
64
+ class RetupleCaughtValueError extends Error {
77
65
  constructor(value) {
78
66
  super("Caught value was not an instance of Error");
79
67
  this.value = value;
80
68
  }
81
69
  }
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;
70
+ exports.RetupleCaughtValueError = RetupleCaughtValueError;
98
71
  /**
99
72
  * ## Retuple Invalid Union Error
100
73
  *
@@ -130,26 +103,20 @@ exports.RetupleArrayMethodUnavailableError = RetupleArrayMethodUnavailableError;
130
103
  * @TODO
131
104
  */
132
105
  function Result(resultLike) {
133
- const [err, ok] = resultLike;
134
- if (err === null || err === undefined) {
135
- return new ResultOk(ok);
136
- }
137
- if (ok === null || ok === undefined) {
138
- return new ResultErr(err);
139
- }
140
- throw new RetupleInvalidResultError(resultLike);
106
+ return asResult(resultLike);
141
107
  }
142
108
  Result.Ok = Ok;
143
109
  Result.Err = Err;
144
- Result.$resolve = resolve;
145
- Result.$nonNullable = nonNullable;
146
- Result.$truthy = truthy;
147
- Result.$union = union;
148
- Result.$safe = safe;
149
- Result.$safeAsync = safeAsync;
150
- Result.$safePromise = safePromise;
151
- Result.$retry = retry;
152
- Result.$safeRetry = safeRetry;
110
+ Result.$from = $from;
111
+ Result.$resolve = $resolve;
112
+ Result.$nonNullable = $nonNullable;
113
+ Result.$truthy = $truthy;
114
+ Result.$fromUnion = $fromUnion;
115
+ Result.$safe = $safe;
116
+ Result.$safeAsync = $safeAsync;
117
+ Result.$safePromise = $safePromise;
118
+ Result.$retry = $retry;
119
+ Result.$safeRetry = $safeRetry;
153
120
  Object.freeze(Result);
154
121
  function Ok(val) {
155
122
  return new ResultOk(val);
@@ -158,35 +125,110 @@ function Err(err) {
158
125
  return new ResultErr(err);
159
126
  }
160
127
  /**
161
- * @TODO
128
+ * Construct a {@link Result} from a {@link ResultLike}.
129
+ *
130
+ * @example
131
+ *
132
+ * ```ts
133
+ * const value: Result<T, E> | ResultLike<T, E> = someResultFn();
134
+ * const result: Result<T, E> = Result.$from(result);
135
+ * ```
162
136
  */
163
- function resolve(result) {
164
- if (result instanceof ResultAsync) {
137
+ function $from(result) {
138
+ if (result instanceof ResultOk || result instanceof ResultErr) {
165
139
  return result;
166
140
  }
167
- else if (result instanceof ResultOk || result instanceof ResultErr) {
168
- return result.$async();
169
- }
170
- else {
171
- return new ResultAsync(result);
141
+ return asResult(result);
142
+ }
143
+ /**
144
+ * Construct a {@link ResultAsync} from a {@link ResultLikeAwaitable}.
145
+ *
146
+ * @example
147
+ *
148
+ * ```ts
149
+ * const value:
150
+ * | Result<T, E>
151
+ * | ResultLike<T, E>
152
+ * | ResultAsync<T, E>
153
+ * | Promise<Result<T, E>>
154
+ * | Promise<ResultLike<T, E>>
155
+ * | PromiseLike<Result<T, E>>
156
+ * | PromiseLike<ResultLike<T, E>> = someResultFn();
157
+ *
158
+ * const result: ResultAsync<T, E> = Result.$resolve(result);
159
+ * ```
160
+ */
161
+ function $resolve(result) {
162
+ switch (true) {
163
+ case result instanceof ResultAsync:
164
+ return result;
165
+ case result instanceof ResultRetry:
166
+ return new ResultAsync(result);
167
+ case result instanceof ResultOk:
168
+ case result instanceof ResultErr:
169
+ return new ResultAsync(Promise.resolve(result));
170
+ default:
171
+ return new ResultAsync(Promise.resolve(result).then((asResult)));
172
172
  }
173
173
  }
174
- function nonNullable(value, error = mapTrue) {
174
+ function $nonNullable(value, error = mapTrue) {
175
175
  if (value !== null && value !== undefined) {
176
176
  return Ok(value);
177
177
  }
178
178
  return Err(error());
179
179
  }
180
- function truthy(value, error = mapTrue) {
180
+ function $truthy(value, error = mapTrue) {
181
181
  if (value) {
182
182
  return Ok(value);
183
183
  }
184
184
  return Err(error());
185
185
  }
186
186
  /**
187
- * @TODO
187
+ * Construct a {@link Result} from a common discriminated union shape. If the
188
+ * union is 'success' then the result is `Ok`
189
+ *
190
+ * Otherwise, the result is `Err` containing:
191
+ *
192
+ * - the returned value from the error function when provided;
193
+ * - or `true` otherwise.
194
+ *
195
+ * @example
196
+ *
197
+ * ```ts
198
+ * const result: Result<string, Error> = Result.$truthy(
199
+ * username.trim(),
200
+ * () => new Error("Username is empty"),
201
+ * );
202
+ * ```
203
+ *
204
+ * @example
205
+ *
206
+ * ```ts
207
+ * const [err, value] = Result.$truthy("test");
208
+ *
209
+ * assert.equal(err, undefined);
210
+ * assert.equal(value, "test");
211
+ * ```
212
+ *
213
+ * @example
214
+ *
215
+ * ```ts
216
+ * const [err, value] = Result.$truthy("");
217
+ *
218
+ * assert.equal(err, true);
219
+ * assert.equal(value, undefined);
220
+ * ```
221
+ *
222
+ * @example
223
+ *
224
+ * ```ts
225
+ * const [err, value] = Result.$truthy(0, () => "error");
226
+ *
227
+ * assert.equal(err, "error");
228
+ * assert.equal(value, undefined);
229
+ * ```
188
230
  */
189
- function union(union) {
231
+ function $fromUnion(union) {
190
232
  if (union.success === true) {
191
233
  return Ok(union.data);
192
234
  }
@@ -195,7 +237,7 @@ function union(union) {
195
237
  }
196
238
  throw new RetupleInvalidUnionError(union);
197
239
  }
198
- function safe(f, mapError = ensureError) {
240
+ function $safe(f, mapError = ensureError) {
199
241
  try {
200
242
  return Ok(f());
201
243
  }
@@ -203,7 +245,7 @@ function safe(f, mapError = ensureError) {
203
245
  return Err(mapError(err));
204
246
  }
205
247
  }
206
- function safeAsync(f, mapError = ensureError) {
248
+ function $safeAsync(f, mapError = ensureError) {
207
249
  return new ResultAsync((async () => {
208
250
  try {
209
251
  return Ok(await f());
@@ -213,16 +255,29 @@ function safeAsync(f, mapError = ensureError) {
213
255
  }
214
256
  })());
215
257
  }
216
- function safePromise(promise, mapError = ensureError) {
258
+ function $safePromise(promise, mapError = ensureError) {
217
259
  return new ResultAsync(promise.then((Ok), async (err) => Err(await mapError(err))));
218
260
  }
219
261
  /**
220
- * @TODO
262
+ * Construct a {@link ResultRetry} from a function which returns a
263
+ * {@link Result}. The function will be retried based on provided retry
264
+ * settings and eventually return a `Result`. No attempt is made to catch
265
+ * thrown errors or promise rejections.
266
+ *
267
+ * To retry a potentially unsafe function, use {@link Result.$safeRetry}.
268
+ *
269
+ * @example
270
+ *
271
+ * ```ts
272
+ * // Retry someResultFn up to 3 times until Ok is returned,
273
+ * // with a 1 second delay between each invocation:
274
+ * const result = await Result.$retry(someResultFn).$times(3).$delay(1000);
275
+ * ```
221
276
  */
222
- function retry(f) {
277
+ function $retry(f) {
223
278
  return new ResultRetry(f);
224
279
  }
225
- function safeRetry(f, mapError = ensureError) {
280
+ function $safeRetry(f, mapError = ensureError) {
226
281
  return new ResultRetry(async () => {
227
282
  try {
228
283
  return Ok(await f());
@@ -242,8 +297,7 @@ class RetupleArray extends Array {
242
297
  /**
243
298
  * ## Method not available
244
299
  *
245
- * Built-in array methods not available on `Result` types, convert the result
246
- * to a tuple using `$tuple()` first.
300
+ * Built-in array methods not available on {@link Result} types.
247
301
  *
248
302
  * @deprecated
249
303
  */
@@ -253,8 +307,7 @@ class RetupleArray extends Array {
253
307
  /**
254
308
  * ## Method not available
255
309
  *
256
- * Built-in array methods not available on `Result` types, convert the result
257
- * to a tuple using `$tuple()` first.
310
+ * Built-in array methods not available on {@link Result} types.
258
311
  *
259
312
  * @deprecated
260
313
  */
@@ -264,8 +317,7 @@ class RetupleArray extends Array {
264
317
  /**
265
318
  * ## Method not available
266
319
  *
267
- * Built-in array methods not available on `Result` types, convert the result
268
- * to a tuple using `$tuple()` first.
320
+ * Built-in array methods not available on {@link Result} types.
269
321
  *
270
322
  * @deprecated
271
323
  */
@@ -275,8 +327,7 @@ class RetupleArray extends Array {
275
327
  /**
276
328
  * ## Method not available
277
329
  *
278
- * Built-in array methods not available on `Result` types, convert the result
279
- * to a tuple using `$tuple()` first.
330
+ * Built-in array methods not available on {@link Result} types.
280
331
  *
281
332
  * @deprecated
282
333
  */
@@ -286,8 +337,7 @@ class RetupleArray extends Array {
286
337
  /**
287
338
  * ## Method not available
288
339
  *
289
- * Built-in array methods not available on `Result` types, convert the result
290
- * to a tuple using `$tuple()` first.
340
+ * Built-in array methods not available on {@link Result} types.
291
341
  *
292
342
  * @deprecated
293
343
  */
@@ -297,8 +347,7 @@ class RetupleArray extends Array {
297
347
  /**
298
348
  * ## Method not available
299
349
  *
300
- * Built-in array methods not available on `Result` types, convert the result
301
- * to a tuple using `$tuple()` first.
350
+ * Built-in array methods not available on {@link Result} types.
302
351
  *
303
352
  * @deprecated
304
353
  */
@@ -308,8 +357,7 @@ class RetupleArray extends Array {
308
357
  /**
309
358
  * ## Method not available
310
359
  *
311
- * Built-in array methods not available on `Result` types, convert the result
312
- * to a tuple using `$tuple()` first.
360
+ * Built-in array methods not available on {@link Result} types.
313
361
  *
314
362
  * @deprecated
315
363
  */
@@ -319,8 +367,7 @@ class RetupleArray extends Array {
319
367
  /**
320
368
  * ## Method not available
321
369
  *
322
- * Built-in array methods not available on `Result` types, convert the result
323
- * to a tuple using `$tuple()` first.
370
+ * Built-in array methods not available on {@link Result} types.
324
371
  *
325
372
  * @deprecated
326
373
  */
@@ -330,8 +377,7 @@ class RetupleArray extends Array {
330
377
  /**
331
378
  * ## Method not available
332
379
  *
333
- * Built-in array methods not available on `Result` types, convert the result
334
- * to a tuple using `$tuple()` first.
380
+ * Built-in array methods not available on {@link Result} types.
335
381
  *
336
382
  * @deprecated
337
383
  */
@@ -341,8 +387,7 @@ class RetupleArray extends Array {
341
387
  /**
342
388
  * ## Method not available
343
389
  *
344
- * Built-in array methods not available on `Result` types, convert the result
345
- * to a tuple using `$tuple()` first.
390
+ * Built-in array methods not available on {@link Result} types.
346
391
  *
347
392
  * @deprecated
348
393
  */
@@ -352,8 +397,7 @@ class RetupleArray extends Array {
352
397
  /**
353
398
  * ## Method not available
354
399
  *
355
- * Built-in array methods not available on `Result` types, convert the result
356
- * to a tuple using `$tuple()` first.
400
+ * Built-in array methods not available on {@link Result} types.
357
401
  *
358
402
  * @deprecated
359
403
  */
@@ -363,8 +407,7 @@ class RetupleArray extends Array {
363
407
  /**
364
408
  * ## Method not available
365
409
  *
366
- * Built-in array methods not available on `Result` types, convert the result
367
- * to a tuple using `$tuple()` first.
410
+ * Built-in array methods not available on {@link Result} types.
368
411
  *
369
412
  * @deprecated
370
413
  */
@@ -374,8 +417,7 @@ class RetupleArray extends Array {
374
417
  /**
375
418
  * ## Method not available
376
419
  *
377
- * Built-in array methods not available on `Result` types, convert the result
378
- * to a tuple using `$tuple()` first.
420
+ * Built-in array methods not available on {@link Result} types.
379
421
  *
380
422
  * @deprecated
381
423
  */
@@ -385,8 +427,7 @@ class RetupleArray extends Array {
385
427
  /**
386
428
  * ## Method not available
387
429
  *
388
- * Built-in array methods not available on `Result` types, convert the result
389
- * to a tuple using `$tuple()` first.
430
+ * Built-in array methods not available on {@link Result} types.
390
431
  *
391
432
  * @deprecated
392
433
  */
@@ -396,8 +437,7 @@ class RetupleArray extends Array {
396
437
  /**
397
438
  * ## Method not available
398
439
  *
399
- * Built-in array methods not available on `Result` types, convert the result
400
- * to a tuple using `$tuple()` first.
440
+ * Built-in array methods not available on {@link Result} types.
401
441
  *
402
442
  * @deprecated
403
443
  */
@@ -407,8 +447,7 @@ class RetupleArray extends Array {
407
447
  /**
408
448
  * ## Method not available
409
449
  *
410
- * Built-in array methods not available on `Result` types, convert the result
411
- * to a tuple using `$tuple()` first.
450
+ * Built-in array methods not available on {@link Result} types.
412
451
  *
413
452
  * @deprecated
414
453
  */
@@ -418,8 +457,7 @@ class RetupleArray extends Array {
418
457
  /**
419
458
  * ## Method not available
420
459
  *
421
- * Built-in array methods not available on `Result` types, convert the result
422
- * to a tuple using `$tuple()` first.
460
+ * Built-in array methods not available on {@link Result} types.
423
461
  *
424
462
  * @deprecated
425
463
  */
@@ -429,8 +467,7 @@ class RetupleArray extends Array {
429
467
  /**
430
468
  * ## Method not available
431
469
  *
432
- * Built-in array methods not available on `Result` types, convert the result
433
- * to a tuple using `$tuple()` first.
470
+ * Built-in array methods not available on {@link Result} types.
434
471
  *
435
472
  * @deprecated
436
473
  */
@@ -440,8 +477,7 @@ class RetupleArray extends Array {
440
477
  /**
441
478
  * ## Method not available
442
479
  *
443
- * Built-in array methods not available on `Result` types, convert the result
444
- * to a tuple using `$tuple()` first.
480
+ * Built-in array methods not available on {@link Result} types.
445
481
  *
446
482
  * @deprecated
447
483
  */
@@ -451,8 +487,7 @@ class RetupleArray extends Array {
451
487
  /**
452
488
  * ## Method not available
453
489
  *
454
- * Built-in array methods not available on `Result` types, convert the result
455
- * to a tuple using `$tuple()` first.
490
+ * Built-in array methods not available on {@link Result} types.
456
491
  *
457
492
  * @deprecated
458
493
  */
@@ -462,8 +497,7 @@ class RetupleArray extends Array {
462
497
  /**
463
498
  * ## Method not available
464
499
  *
465
- * Built-in array methods not available on `Result` types, convert the result
466
- * to a tuple using `$tuple()` first.
500
+ * Built-in array methods not available on {@link Result} types.
467
501
  *
468
502
  * @deprecated
469
503
  */
@@ -473,8 +507,7 @@ class RetupleArray extends Array {
473
507
  /**
474
508
  * ## Method not available
475
509
  *
476
- * Built-in array methods not available on `Result` types, convert the result
477
- * to a tuple using `$tuple()` first.
510
+ * Built-in array methods not available on {@link Result} types.
478
511
  *
479
512
  * @deprecated
480
513
  */
@@ -484,8 +517,7 @@ class RetupleArray extends Array {
484
517
  /**
485
518
  * ## Method not available
486
519
  *
487
- * Built-in array methods not available on `Result` types, convert the result
488
- * to a tuple using `$tuple()` first.
520
+ * Built-in array methods not available on {@link Result} types.
489
521
  *
490
522
  * @deprecated
491
523
  */
@@ -495,8 +527,7 @@ class RetupleArray extends Array {
495
527
  /**
496
528
  * ## Method not available
497
529
  *
498
- * Built-in array methods not available on `Result` types, convert the result
499
- * to a tuple using `$tuple()` first.
530
+ * Built-in array methods not available on {@link Result} types.
500
531
  *
501
532
  * @deprecated
502
533
  */
@@ -506,8 +537,7 @@ class RetupleArray extends Array {
506
537
  /**
507
538
  * ## Method not available
508
539
  *
509
- * Built-in array methods not available on `Result` types, convert the result
510
- * to a tuple using `$tuple()` first.
540
+ * Built-in array methods not available on {@link Result} types.
511
541
  *
512
542
  * @deprecated
513
543
  */
@@ -517,8 +547,7 @@ class RetupleArray extends Array {
517
547
  /**
518
548
  * ## Method not available
519
549
  *
520
- * Built-in array methods not available on `Result` types, convert the result
521
- * to a tuple using `$tuple()` first.
550
+ * Built-in array methods not available on {@link Result} types.
522
551
  *
523
552
  * @deprecated
524
553
  */
@@ -528,8 +557,7 @@ class RetupleArray extends Array {
528
557
  /**
529
558
  * ## Method not available
530
559
  *
531
- * Built-in array methods not available on `Result` types, convert the result
532
- * to a tuple using `$tuple()` first.
560
+ * Built-in array methods not available on {@link Result} types.
533
561
  *
534
562
  * @deprecated
535
563
  */
@@ -539,8 +567,7 @@ class RetupleArray extends Array {
539
567
  /**
540
568
  * ## Method not available
541
569
  *
542
- * Built-in array methods not available on `Result` types, convert the result
543
- * to a tuple using `$tuple()` first.
570
+ * Built-in array methods not available on {@link Result} types.
544
571
  *
545
572
  * @deprecated
546
573
  */
@@ -550,8 +577,7 @@ class RetupleArray extends Array {
550
577
  /**
551
578
  * ## Method not available
552
579
  *
553
- * Built-in array methods not available on `Result` types, convert the result
554
- * to a tuple using `$tuple()` first.
580
+ * Built-in array methods not available on {@link Result} types.
555
581
  *
556
582
  * @deprecated
557
583
  */
@@ -561,8 +587,7 @@ class RetupleArray extends Array {
561
587
  /**
562
588
  * ## Method not available
563
589
  *
564
- * Built-in array methods not available on `Result` types, convert the result
565
- * to a tuple using `$tuple()` first.
590
+ * Built-in array methods not available on {@link Result} types.
566
591
  *
567
592
  * @deprecated
568
593
  */
@@ -572,8 +597,7 @@ class RetupleArray extends Array {
572
597
  /**
573
598
  * ## Method not available
574
599
  *
575
- * Built-in array methods not available on `Result` types, convert the result
576
- * to a tuple using `$tuple()` first.
600
+ * Built-in array methods not available on {@link Result} types.
577
601
  *
578
602
  * @deprecated
579
603
  */
@@ -583,8 +607,7 @@ class RetupleArray extends Array {
583
607
  /**
584
608
  * ## Method not available
585
609
  *
586
- * Built-in array methods not available on `Result` types, convert the result
587
- * to a tuple using `$tuple()` first.
610
+ * Built-in array methods not available on {@link Result} types.
588
611
  *
589
612
  * @deprecated
590
613
  */
@@ -594,8 +617,7 @@ class RetupleArray extends Array {
594
617
  /**
595
618
  * ## Method not available
596
619
  *
597
- * Built-in array methods not available on `Result` types, convert the result
598
- * to a tuple using `$tuple()` first.
620
+ * Built-in array methods not available on {@link Result} types.
599
621
  *
600
622
  * @deprecated
601
623
  */
@@ -605,8 +627,7 @@ class RetupleArray extends Array {
605
627
  /**
606
628
  * ## Method not available
607
629
  *
608
- * Built-in array methods not available on `Result` types, convert the result
609
- * to a tuple using `$tuple()` first.
630
+ * Built-in array methods not available on {@link Result} types.
610
631
  *
611
632
  * @deprecated
612
633
  */
@@ -616,8 +637,7 @@ class RetupleArray extends Array {
616
637
  /**
617
638
  * ## Method not available
618
639
  *
619
- * Built-in array methods not available on `Result` types, convert the result
620
- * to a tuple using `$tuple()` first.
640
+ * Built-in array methods not available on {@link Result} types.
621
641
  *
622
642
  * @deprecated
623
643
  */
@@ -627,8 +647,7 @@ class RetupleArray extends Array {
627
647
  /**
628
648
  * ## Method not available
629
649
  *
630
- * Built-in array methods not available on `Result` types, convert the result
631
- * to a tuple using `$tuple()` first.
650
+ * Built-in array methods not available on {@link Result} types.
632
651
  *
633
652
  * @deprecated
634
653
  */
@@ -638,8 +657,7 @@ class RetupleArray extends Array {
638
657
  /**
639
658
  * ## Method not available
640
659
  *
641
- * Built-in array methods not available on `Result` types, convert the result
642
- * to a tuple using `$tuple()` first.
660
+ * Built-in array methods not available on {@link Result} types.
643
661
  *
644
662
  * @deprecated
645
663
  */
@@ -649,8 +667,7 @@ class RetupleArray extends Array {
649
667
  /**
650
668
  * ## Method not available
651
669
  *
652
- * Built-in array methods not available on `Result` types, convert the result
653
- * to a tuple using `$tuple()` first.
670
+ * Built-in array methods not available on {@link Result} types.
654
671
  *
655
672
  * @deprecated
656
673
  */
@@ -669,6 +686,9 @@ class ResultOk extends RetupleArray {
669
686
  this[0] = undefined;
670
687
  this[1] = value;
671
688
  }
689
+ [symbol_js_1.ResultLikeSymbol]() {
690
+ return this;
691
+ }
672
692
  toJSON() {
673
693
  return this[1];
674
694
  }
@@ -712,10 +732,10 @@ class ResultOk extends RetupleArray {
712
732
  return Ok(f(this[1]));
713
733
  }
714
734
  $andAssertOr(def, condition = isTruthy) {
715
- return condition(this[1]) ? this : def;
735
+ return condition(this[1]) ? this : asResult(def);
716
736
  }
717
737
  $andAssertOrElse(def, condition = isTruthy) {
718
- return condition(this[1]) ? this : def(this[1]);
738
+ return condition(this[1]) ? this : asResult(def(this[1]));
719
739
  }
720
740
  $or() {
721
741
  return this;
@@ -727,13 +747,13 @@ class ResultOk extends RetupleArray {
727
747
  return this;
728
748
  }
729
749
  $and(and) {
730
- return and;
750
+ return asResult(and);
731
751
  }
732
752
  $andThen(f) {
733
- return f(this[1]);
753
+ return asResult(f(this[1]));
734
754
  }
735
755
  $andThrough(f) {
736
- const res = f(this[1]);
756
+ const res = asResult(f(this[1]));
737
757
  return res instanceof ResultErr ? res : this;
738
758
  }
739
759
  $andSafe(f, mapError = ensureError) {
@@ -756,11 +776,7 @@ class ResultOk extends RetupleArray {
756
776
  return this;
757
777
  }
758
778
  $flatten() {
759
- const inner = this[1];
760
- if (inner instanceof ResultOk || inner instanceof ResultErr) {
761
- return inner;
762
- }
763
- throw new RetupleFlattenFailed(this[1]);
779
+ return this[1];
764
780
  }
765
781
  $async() {
766
782
  return new ResultAsync(Promise.resolve(this));
@@ -768,9 +784,6 @@ class ResultOk extends RetupleArray {
768
784
  $promise() {
769
785
  return Promise.resolve(this);
770
786
  }
771
- $tuple() {
772
- return [undefined, this[1]];
773
- }
774
787
  *$iter() {
775
788
  yield* this[1];
776
789
  }
@@ -786,6 +799,9 @@ class ResultErr extends RetupleArray {
786
799
  this[0] = err;
787
800
  this[1] = undefined;
788
801
  }
802
+ [symbol_js_1.ResultLikeSymbol]() {
803
+ return this;
804
+ }
789
805
  toJSON() {
790
806
  return null;
791
807
  }
@@ -838,10 +854,10 @@ class ResultErr extends RetupleArray {
838
854
  return this;
839
855
  }
840
856
  $or(or) {
841
- return or;
857
+ return asResult(or);
842
858
  }
843
859
  $orElse(f) {
844
- return f(this[0]);
860
+ return asResult(f(this[0]));
845
861
  }
846
862
  $orSafe(f, mapError = ensureError) {
847
863
  try {
@@ -883,9 +899,6 @@ class ResultErr extends RetupleArray {
883
899
  $promise() {
884
900
  return Promise.resolve(this);
885
901
  }
886
- $tuple() {
887
- return [this[0], undefined];
888
- }
889
902
  *$iter() {
890
903
  return;
891
904
  }
@@ -938,37 +951,47 @@ class ResultAsync {
938
951
  return res instanceof ResultOk ? res[1] : f();
939
952
  }
940
953
  /**
941
- * The same as {@link Retuple.$map|$map}, except it returns `ResultAsync`.
954
+ * The same as {@link Retuple.$map|$map}, except it returns
955
+ * {@link ResultAsync}.
942
956
  */
943
957
  $map(f) {
944
958
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
945
- return res instanceof ResultOk ? Ok(f(res[1])) : res;
959
+ return res instanceof ResultOk
960
+ ? new ResultOk(f(res[1]))
961
+ : res;
946
962
  }));
947
963
  }
948
964
  /**
949
965
  * The same as {@link Retuple.$mapErr|$mapErr}, except it returns
950
- * `ResultAsync`.
966
+ * {@link ResultAsync}.
951
967
  */
952
968
  $mapErr(f) {
953
969
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
954
- return res instanceof ResultErr ? Err(f(res[0])) : res;
970
+ return res instanceof ResultErr
971
+ ? new ResultErr(f(res[0]))
972
+ : res;
955
973
  }));
956
974
  }
957
975
  /**
958
- * The same as {@link Retuple.$mapOr|$mapOr}, except it returns `ResultAsync`.
976
+ * The same as {@link Retuple.$mapOr|$mapOr}, except it returns
977
+ * {@link ResultAsync}.
959
978
  */
960
979
  $mapOr(def, f) {
961
980
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
962
- return res instanceof ResultOk ? Ok(f(res[1])) : Ok(def);
981
+ return res instanceof ResultOk
982
+ ? new ResultOk(f(res[1]))
983
+ : new ResultOk(def);
963
984
  }));
964
985
  }
965
986
  /**
966
987
  * The same as {@link Retuple.$mapOrElse|$mapOrElse}, except it returns
967
- * `ResultAsync`.
988
+ * {@link ResultAsync}.
968
989
  */
969
990
  $mapOrElse(def, f) {
970
991
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
971
- return res instanceof ResultOk ? Ok(f(res[1])) : Ok(def(res[0]));
992
+ return res instanceof ResultOk
993
+ ? new ResultOk(f(res[1]))
994
+ : new ResultOk(def(res[0]));
972
995
  }));
973
996
  }
974
997
  $andAssertOr(def, condition = isTruthy) {
@@ -976,7 +999,7 @@ class ResultAsync {
976
999
  if (res instanceof ResultErr || condition(res[1])) {
977
1000
  return res;
978
1001
  }
979
- return await def;
1002
+ return asResult(await def);
980
1003
  }));
981
1004
  }
982
1005
  $andAssertOrElse(def, condition = isTruthy) {
@@ -984,18 +1007,20 @@ class ResultAsync {
984
1007
  if (res instanceof ResultErr || condition(res[1])) {
985
1008
  return res;
986
1009
  }
987
- return await def(res[1]);
1010
+ return asResult(await def(res[1]));
988
1011
  }));
989
1012
  }
990
1013
  $or(or) {
991
1014
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
992
- return res instanceof ResultErr ? await or : res;
1015
+ return res instanceof ResultErr
1016
+ ? asResult(await or)
1017
+ : res;
993
1018
  }));
994
1019
  }
995
1020
  $orElse(f) {
996
1021
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
997
1022
  return res instanceof ResultErr
998
- ? await f(res[0])
1023
+ ? asResult(await f(res[0]))
999
1024
  : res;
1000
1025
  }));
1001
1026
  }
@@ -1005,10 +1030,10 @@ class ResultAsync {
1005
1030
  return res;
1006
1031
  }
1007
1032
  try {
1008
- return Ok(await f(res[0]));
1033
+ return new ResultOk(await f(res[0]));
1009
1034
  }
1010
1035
  catch (err) {
1011
- return Err(mapError(err));
1036
+ return new ResultErr(mapError(err));
1012
1037
  }
1013
1038
  }));
1014
1039
  }
@@ -1018,27 +1043,31 @@ class ResultAsync {
1018
1043
  return res;
1019
1044
  }
1020
1045
  try {
1021
- return Ok(await promise);
1046
+ return new ResultOk(await promise);
1022
1047
  }
1023
1048
  catch (err) {
1024
- return Err(mapError(err));
1049
+ return new ResultErr(mapError(err));
1025
1050
  }
1026
1051
  }));
1027
1052
  }
1028
1053
  $and(and) {
1029
1054
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
1030
- return res instanceof ResultOk ? await and : res;
1055
+ return res instanceof ResultOk
1056
+ ? asResult(await and)
1057
+ : res;
1031
1058
  }));
1032
1059
  }
1033
1060
  $andThen(f) {
1034
1061
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
1035
- return res instanceof ResultOk ? await f(res[1]) : res;
1062
+ return res instanceof ResultOk
1063
+ ? asResult(await f(res[1]))
1064
+ : res;
1036
1065
  }));
1037
1066
  }
1038
1067
  $andThrough(f) {
1039
1068
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
1040
1069
  if (res instanceof ResultOk) {
1041
- const through = await f(res[1]);
1070
+ const through = asResult(await f(res[1]));
1042
1071
  if (through instanceof ResultErr) {
1043
1072
  return through;
1044
1073
  }
@@ -1076,7 +1105,7 @@ class ResultAsync {
1076
1105
  * The same as {@link Retuple.$peek|$peek}, except it:
1077
1106
  *
1078
1107
  * - awaits the peek function;
1079
- * - returns `ResultAsync`.
1108
+ * - returns {@link ResultAsync}.
1080
1109
  */
1081
1110
  $peek(f) {
1082
1111
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
@@ -1088,7 +1117,7 @@ class ResultAsync {
1088
1117
  * The same as {@link Retuple.$tap|$tap}, except it:
1089
1118
  *
1090
1119
  * - awaits the tap function;
1091
- * - returns `ResultAsync`.
1120
+ * - returns {@link ResultAsync}.
1092
1121
  */
1093
1122
  $tap(f) {
1094
1123
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
@@ -1102,7 +1131,7 @@ class ResultAsync {
1102
1131
  * The same as {@link Retuple.$tapErr|$tapErr}, except it:
1103
1132
  *
1104
1133
  * - awaits the tap error function;
1105
- * - returns `ResultAsync`.
1134
+ * - returns {@link ResultAsync}.
1106
1135
  */
1107
1136
  $tapErr(f) {
1108
1137
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
@@ -1119,13 +1148,7 @@ class ResultAsync {
1119
1148
  return Promise.resolve(this);
1120
1149
  }
1121
1150
  /**
1122
- * The same as {@link Retuple.$tuple|$tuple}, except it returns a `Promise`.
1123
- */
1124
- async $tuple() {
1125
- return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$tuple();
1126
- }
1127
- /**
1128
- * The same as {@link Retuple.$tuple|$iter}, except it returns a `Promise`.
1151
+ * The same as {@link Retuple.$iter|$iter}, except it returns a `Promise`.
1129
1152
  */
1130
1153
  async $iter() {
1131
1154
  return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$iter();
@@ -1133,7 +1156,7 @@ class ResultAsync {
1133
1156
  }
1134
1157
  _ResultAsync_inner = new WeakMap();
1135
1158
  /**
1136
- * @TODO
1159
+ * ## ResultRetry
1137
1160
  */
1138
1161
  class ResultRetry {
1139
1162
  static zero() {
@@ -1156,7 +1179,7 @@ class ResultRetry {
1156
1179
  _ResultRetry_aborted.set(this, false);
1157
1180
  _ResultRetry_abort.set(this, () => (__classPrivateFieldSet(this, _ResultRetry_aborted, true, "f")));
1158
1181
  _ResultRetry_getDelay.set(this, _a.zero);
1159
- _ResultRetry_errorHandler.set(this, void 0);
1182
+ _ResultRetry_handler.set(this, void 0);
1160
1183
  __classPrivateFieldSet(this, _ResultRetry_f, f, "f");
1161
1184
  __classPrivateFieldSet(this, _ResultRetry_promise, this.drain(), "f");
1162
1185
  }
@@ -1164,7 +1187,27 @@ class ResultRetry {
1164
1187
  return __classPrivateFieldGet(this, _ResultRetry_promise, "f").then(onfulfilled, onrejected);
1165
1188
  }
1166
1189
  /**
1167
- * @TODO - Capped 100
1190
+ * Sets the maximum number of times the retry function can be executed,
1191
+ * mutating this `ResultRetry` instance.
1192
+ *
1193
+ * **The default value is 1 - meaning that unless set, no retries will be
1194
+ * attempted.**
1195
+ *
1196
+ * The retry function can be called up to the maximum number of times until
1197
+ * it returns `Ok`. If it never returns `Ok`, the most recent `Err` is
1198
+ * returned.
1199
+ *
1200
+ * This function accepts a positive integer between 1 and 100:
1201
+ *
1202
+ * - Integers outside of this range are clamped to the nearest valid value;
1203
+ * - Any other value (NaN, Infinity, fractions, strings) are treated as 1.
1204
+ *
1205
+ * @example
1206
+ *
1207
+ * ```ts
1208
+ * // Retry someResultFn up to 3 times until Ok is returned:
1209
+ * const result = await Result.$retry(someResultFn).$times(3);
1210
+ * ```
1168
1211
  */
1169
1212
  $times(times) {
1170
1213
  __classPrivateFieldSet(this, _ResultRetry_times, Math.min(Math.max(1, _a.integer(times)), _a.MAX_RETRY), "f");
@@ -1182,34 +1225,84 @@ class ResultRetry {
1182
1225
  return this;
1183
1226
  }
1184
1227
  /**
1185
- * @TODO
1228
+ * Sets a handler to be called when an attempt returns `Err`, mutating this
1229
+ * `ResultRetry` instance. The handler can be used to capture information
1230
+ * about each failure, and to abort early and prevent further retries.
1231
+ *
1232
+ * The handler function is called with `ResultRetryHandleState`, containing:
1233
+ *
1234
+ * - **error** - The error value from the last failed attempt;
1235
+ * - **attempt** - The attempt number;
1236
+ * - **abort** - A function which when called, prevents further retries.
1237
+ *
1238
+ * @example
1239
+ *
1240
+ * ```ts
1241
+ * // Retry someResultFn up to 3 times until Ok is returned, logging each
1242
+ * // attempt and aborting early if the error code is "UNAUTHORIZED".
1243
+ * const result = await Result.$retry(someResultFn)
1244
+ * .$times(3)
1245
+ * .$handle(({ error, attempt, abort }) => {
1246
+ * console.info(`Attempt ${attempt} failed: ${error}`);
1247
+ * if (error === "UNAUTHORIZED") {
1248
+ * abort();
1249
+ * }
1250
+ * });
1251
+ * ```
1186
1252
  */
1187
- $monitor(f) {
1188
- __classPrivateFieldSet(this, _ResultRetry_errorHandler, f, "f");
1253
+ $handle(f) {
1254
+ __classPrivateFieldSet(this, _ResultRetry_handler, f, "f");
1189
1255
  return this;
1190
1256
  }
1191
1257
  /**
1192
- * @TODO
1258
+ * Returns {@link ResultAsync} which resolves to this retried {@link Result}.
1259
+ *
1260
+ * @example
1261
+ *
1262
+ * ```ts
1263
+ * const result: Result<string, SomeError> = await Result
1264
+ * .$retry(someResultFn)
1265
+ * .$times(3)
1266
+ * .$delay(100)
1267
+ * .$async()
1268
+ * .$andThen((message) => `Success: ${message}`)
1269
+ * .$mapErr((code) => new SomeError({ code }));
1270
+ * ```
1193
1271
  */
1194
1272
  $async() {
1195
1273
  return new ResultAsync(this);
1196
1274
  }
1275
+ /**
1276
+ * Returns a `Promise` which resolves to this retried {@link Result}.
1277
+ *
1278
+ * @example
1279
+ *
1280
+ * ```ts
1281
+ * const promise: Promise<Result<string, Error>> = Result
1282
+ * .$retry(someResultFn)
1283
+ * .$times(3)
1284
+ * .$promise();
1285
+ * ```
1286
+ */
1287
+ $promise() {
1288
+ return Promise.resolve(this);
1289
+ }
1197
1290
  async drain() {
1198
1291
  var _b;
1199
1292
  while (__classPrivateFieldGet(this, _ResultRetry_attempt, "f") < __classPrivateFieldGet(this, _ResultRetry_times, "f")) {
1200
- const result = await __classPrivateFieldGet(this, _ResultRetry_f, "f").call(this);
1293
+ const result = asResult(await __classPrivateFieldGet(this, _ResultRetry_f, "f").call(this));
1201
1294
  __classPrivateFieldSet(this, _ResultRetry_attempt, (_b = __classPrivateFieldGet(this, _ResultRetry_attempt, "f"), _b++, _b), "f");
1202
1295
  if (result.$isOk()) {
1203
1296
  return result;
1204
1297
  }
1205
- if (__classPrivateFieldGet(this, _ResultRetry_errorHandler, "f")) {
1206
- await __classPrivateFieldGet(this, _ResultRetry_errorHandler, "f").call(this, {
1298
+ if (__classPrivateFieldGet(this, _ResultRetry_handler, "f")) {
1299
+ await __classPrivateFieldGet(this, _ResultRetry_handler, "f").call(this, {
1207
1300
  error: result[0],
1208
1301
  attempt: __classPrivateFieldGet(this, _ResultRetry_attempt, "f"),
1209
1302
  abort: __classPrivateFieldGet(this, _ResultRetry_abort, "f"),
1210
1303
  });
1211
1304
  }
1212
- if (__classPrivateFieldGet(this, _ResultRetry_aborted, "f") || __classPrivateFieldGet(this, _ResultRetry_attempt, "f") === __classPrivateFieldGet(this, _ResultRetry_times, "f")) {
1305
+ if (__classPrivateFieldGet(this, _ResultRetry_aborted, "f") || __classPrivateFieldGet(this, _ResultRetry_attempt, "f") >= __classPrivateFieldGet(this, _ResultRetry_times, "f")) {
1213
1306
  return result;
1214
1307
  }
1215
1308
  const delay = _a.integer(__classPrivateFieldGet(this, _ResultRetry_getDelay, "f").call(this, __classPrivateFieldGet(this, _ResultRetry_attempt, "f")));
@@ -1217,16 +1310,21 @@ class ResultRetry {
1217
1310
  await _a.delay(delay);
1218
1311
  }
1219
1312
  }
1313
+ /* v8 ignore next */
1314
+ throw new Error("Retuple: Unreachable code executed");
1220
1315
  }
1221
1316
  }
1222
- _a = ResultRetry, _ResultRetry_f = new WeakMap(), _ResultRetry_promise = new WeakMap(), _ResultRetry_times = new WeakMap(), _ResultRetry_attempt = new WeakMap(), _ResultRetry_aborted = new WeakMap(), _ResultRetry_abort = new WeakMap(), _ResultRetry_getDelay = new WeakMap(), _ResultRetry_errorHandler = new WeakMap();
1317
+ _a = ResultRetry, _ResultRetry_f = new WeakMap(), _ResultRetry_promise = new WeakMap(), _ResultRetry_times = new WeakMap(), _ResultRetry_attempt = new WeakMap(), _ResultRetry_aborted = new WeakMap(), _ResultRetry_abort = new WeakMap(), _ResultRetry_getDelay = new WeakMap(), _ResultRetry_handler = new WeakMap();
1223
1318
  ResultRetry.MAX_TIMEOUT = 3600000;
1224
1319
  ResultRetry.MAX_RETRY = 100;
1320
+ function asResult(resultLike) {
1321
+ return resultLike[symbol_js_1.ResultLikeSymbol]();
1322
+ }
1225
1323
  function ensureError(err) {
1226
1324
  if (err instanceof Error) {
1227
1325
  return err;
1228
1326
  }
1229
- return new RetupleThrownValueError(err);
1327
+ return new RetupleCaughtValueError(err);
1230
1328
  }
1231
1329
  function mapTrue() {
1232
1330
  return true;