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