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