@shirudo/result 0.0.3 → 0.0.5
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 +2 -0
- package/dist/index.cjs +240 -142
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +160 -114
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +160 -114
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +225 -143
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -12,14 +12,96 @@ var Pipeable = class {
|
|
|
12
12
|
}
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/errors.ts
|
|
17
|
+
const ERR_INVALID_STATE = "ERR_INVALID_STATE";
|
|
18
|
+
const ERR_TASK_YIELD_NOT_RESULT = "ERR_TASK_YIELD_NOT_RESULT";
|
|
19
|
+
const ERR_MATCH_ON_OK = "ERR_MATCH_ON_OK";
|
|
20
|
+
const ERR_UNWRAP_ON_ERR = "ERR_UNWRAP_ON_ERR";
|
|
21
|
+
const ERR_UNWRAP_ERR_ON_OK = "ERR_UNWRAP_ERR_ON_OK";
|
|
22
|
+
const ERR_EXPECT_OK = "ERR_EXPECT_OK";
|
|
23
|
+
const ERR_EXPECT_ERR = "ERR_EXPECT_ERR";
|
|
24
|
+
const formatResultErrorMessage = (code, message, context) => {
|
|
25
|
+
if (context) return `${code}: ${message} (context: ${context})`;
|
|
26
|
+
return `${code}: ${message}`;
|
|
27
|
+
};
|
|
28
|
+
var ResultError = class extends Error {
|
|
29
|
+
code;
|
|
30
|
+
context;
|
|
31
|
+
constructor(message, code, context) {
|
|
32
|
+
super(formatResultErrorMessage(code, message, context));
|
|
33
|
+
this.code = code;
|
|
34
|
+
this.context = context;
|
|
35
|
+
this.name = new.target.name;
|
|
36
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var ResultTypeError = class extends TypeError {
|
|
40
|
+
code;
|
|
41
|
+
context;
|
|
42
|
+
constructor(message, code, context) {
|
|
43
|
+
super(formatResultErrorMessage(code, message, context));
|
|
44
|
+
this.code = code;
|
|
45
|
+
this.context = context;
|
|
46
|
+
this.name = new.target.name;
|
|
47
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const INVALID_RESULT_STATE_MESSAGE = "Unreachable: Result is neither Ok nor Err";
|
|
51
|
+
var InvalidResultStateError = class extends ResultError {
|
|
52
|
+
constructor(context) {
|
|
53
|
+
super(INVALID_RESULT_STATE_MESSAGE, ERR_INVALID_STATE, context);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var TaskYieldNotResultError = class extends ResultTypeError {
|
|
57
|
+
yieldedValue;
|
|
58
|
+
constructor(yieldedValue) {
|
|
59
|
+
super("task() expected yielded values to be Result. Use `yield*` on a Result.", ERR_TASK_YIELD_NOT_RESULT);
|
|
60
|
+
this.yieldedValue = yieldedValue;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
var MatchOnOkError = class extends ResultTypeError {
|
|
64
|
+
constructor() {
|
|
65
|
+
super("match() can only be called on Err results. Use `if (result.isErr()) { ... }` first.", ERR_MATCH_ON_OK);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
var UnwrapOnErrError = class extends ResultTypeError {
|
|
69
|
+
errorValue;
|
|
70
|
+
constructor(errorValue) {
|
|
71
|
+
super(`Called unwrap() on Err: ${String(errorValue)}`, ERR_UNWRAP_ON_ERR);
|
|
72
|
+
this.errorValue = errorValue;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
var UnwrapErrOnOkError = class extends ResultTypeError {
|
|
76
|
+
okValue;
|
|
77
|
+
constructor(okValue) {
|
|
78
|
+
super(`Called unwrapErr() on Ok: ${String(okValue)}`, ERR_UNWRAP_ERR_ON_OK);
|
|
79
|
+
this.okValue = okValue;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
var ExpectOkError = class extends ResultError {
|
|
83
|
+
expectedMessage;
|
|
84
|
+
constructor(expectedMessage) {
|
|
85
|
+
super(expectedMessage, ERR_EXPECT_OK);
|
|
86
|
+
this.expectedMessage = expectedMessage;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
var ExpectErrError = class extends ResultError {
|
|
90
|
+
expectedMessage;
|
|
91
|
+
constructor(expectedMessage) {
|
|
92
|
+
super(expectedMessage, ERR_EXPECT_ERR);
|
|
93
|
+
this.expectedMessage = expectedMessage;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
15
97
|
//#endregion
|
|
16
98
|
//#region src/core/matcher.ts
|
|
17
99
|
/**
|
|
18
|
-
* Matcher
|
|
100
|
+
* Matcher for Err values (returns an arbitrary return type, e.g. string messages).
|
|
19
101
|
*
|
|
20
|
-
* - `.when(Ctor, handler)`
|
|
21
|
-
* - `.whenGuard(guard, handler)`
|
|
22
|
-
* - `.run()`
|
|
102
|
+
* - `.when(Ctor, handler)` matches via `instanceof`
|
|
103
|
+
* - `.whenGuard(guard, handler)` matches via Type-Guard
|
|
104
|
+
* - `.run()` is only allowed if all error cases have been handled (`E` has been reduced to `never`)
|
|
23
105
|
*/
|
|
24
106
|
var ErrorMatchBuilder = class ErrorMatchBuilder {
|
|
25
107
|
#error;
|
|
@@ -55,11 +137,11 @@ function isResult$1(value) {
|
|
|
55
137
|
return typeof value === "object" && value !== null && "isOk" in value && typeof value.isOk === "function" && "isErr" in value && typeof value.isErr === "function";
|
|
56
138
|
}
|
|
57
139
|
/**
|
|
58
|
-
* Matcher
|
|
140
|
+
* Matcher for `Result` Errors, which returns a `Result` again.
|
|
59
141
|
*
|
|
60
|
-
*
|
|
61
|
-
* -
|
|
62
|
-
* -
|
|
142
|
+
* Handlers may:
|
|
143
|
+
* - return a `Result` (is returned directly)
|
|
144
|
+
* - return an Error value (is automatically wrapped into `Err(error)`)
|
|
63
145
|
*/
|
|
64
146
|
var ErrMatchBuilder = class ErrMatchBuilder {
|
|
65
147
|
#makeErr;
|
|
@@ -74,7 +156,7 @@ var ErrMatchBuilder = class ErrMatchBuilder {
|
|
|
74
156
|
static fromResult(result, makeErr) {
|
|
75
157
|
if (result.isOk()) return new ErrMatchBuilder(makeErr, void 0, result);
|
|
76
158
|
if (result.isErr()) return new ErrMatchBuilder(makeErr, result.error);
|
|
77
|
-
throw new
|
|
159
|
+
throw new InvalidResultStateError("ErrMatchBuilder.fromResult");
|
|
78
160
|
}
|
|
79
161
|
when(ctor, handler) {
|
|
80
162
|
if (this.#resolved) return this;
|
|
@@ -109,8 +191,8 @@ var ErrMatchBuilder = class ErrMatchBuilder {
|
|
|
109
191
|
//#endregion
|
|
110
192
|
//#region src/core/map.ts
|
|
111
193
|
/**
|
|
112
|
-
*
|
|
113
|
-
*
|
|
194
|
+
* Transforms the value (Ok case).
|
|
195
|
+
* Corresponds to Rust `map`.
|
|
114
196
|
*/
|
|
115
197
|
function map(project) {
|
|
116
198
|
return (source) => {
|
|
@@ -122,8 +204,8 @@ function map(project) {
|
|
|
122
204
|
//#endregion
|
|
123
205
|
//#region src/core/mapErr.ts
|
|
124
206
|
/**
|
|
125
|
-
*
|
|
126
|
-
*
|
|
207
|
+
* Transforms the error (Err case).
|
|
208
|
+
* Corresponds to Rust `map_err`.
|
|
127
209
|
*/
|
|
128
210
|
function mapErr(project) {
|
|
129
211
|
return (source) => {
|
|
@@ -135,26 +217,26 @@ function mapErr(project) {
|
|
|
135
217
|
//#endregion
|
|
136
218
|
//#region src/core/mapBoth.ts
|
|
137
219
|
/**
|
|
138
|
-
*
|
|
139
|
-
*
|
|
220
|
+
* Transforms both the Ok value and the Err error.
|
|
221
|
+
* Corresponds to FP `bimap` / `mapBoth`.
|
|
140
222
|
*/
|
|
141
223
|
function mapBoth(mapOk, mapErr$1) {
|
|
142
224
|
return (source) => {
|
|
143
225
|
if (source.isOk()) return ok(mapOk(source.value));
|
|
144
226
|
if (source.isErr()) return err(mapErr$1(source.error));
|
|
145
|
-
throw new
|
|
227
|
+
throw new InvalidResultStateError("mapBoth");
|
|
146
228
|
};
|
|
147
229
|
}
|
|
148
230
|
/**
|
|
149
|
-
* Alias
|
|
231
|
+
* Alias for `mapBoth`.
|
|
150
232
|
*/
|
|
151
233
|
const bimap = mapBoth;
|
|
152
234
|
|
|
153
235
|
//#endregion
|
|
154
236
|
//#region src/core/flatMap.ts
|
|
155
237
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
238
|
+
* Chains another operation that returns a Result.
|
|
239
|
+
* Corresponds to Rust `and_then` or JS `flatMap`.
|
|
158
240
|
*/
|
|
159
241
|
function flatMap(project) {
|
|
160
242
|
return (source) => {
|
|
@@ -169,7 +251,7 @@ function zipImpl(left, right) {
|
|
|
169
251
|
if (left.isErr()) return left;
|
|
170
252
|
if (right.isErr()) return right;
|
|
171
253
|
if (left.isOk() && right.isOk()) return ok([left.value, right.value]);
|
|
172
|
-
throw new
|
|
254
|
+
throw new InvalidResultStateError("zip");
|
|
173
255
|
}
|
|
174
256
|
function zip(...args) {
|
|
175
257
|
if (args.length === 1) {
|
|
@@ -198,8 +280,8 @@ function combine(...args) {
|
|
|
198
280
|
//#endregion
|
|
199
281
|
//#region src/core/tap.ts
|
|
200
282
|
/**
|
|
201
|
-
*
|
|
202
|
-
*
|
|
283
|
+
* Executes a side effect (logging, debugging) without changing the Result.
|
|
284
|
+
* Corresponds to Rust `inspect` / `inspect_err`.
|
|
203
285
|
*/
|
|
204
286
|
function tap(observer) {
|
|
205
287
|
return (source) => {
|
|
@@ -212,8 +294,8 @@ function tap(observer) {
|
|
|
212
294
|
//#endregion
|
|
213
295
|
//#region src/core/filter.ts
|
|
214
296
|
/**
|
|
215
|
-
*
|
|
216
|
-
*
|
|
297
|
+
* Checks a condition. If false, the Result becomes Err.
|
|
298
|
+
* Corresponds to Rust `filter` (partially).
|
|
217
299
|
*/
|
|
218
300
|
function filter(predicate, errorFn) {
|
|
219
301
|
return (source) => {
|
|
@@ -228,59 +310,59 @@ function filter(predicate, errorFn) {
|
|
|
228
310
|
//#endregion
|
|
229
311
|
//#region src/core/match.ts
|
|
230
312
|
/**
|
|
231
|
-
*
|
|
232
|
-
*
|
|
313
|
+
* Resolves the Result. The end of the pipe.
|
|
314
|
+
* Corresponds to Rust `match`.
|
|
233
315
|
*/
|
|
234
316
|
function match(handlers) {
|
|
235
317
|
return (source) => {
|
|
236
318
|
if (source.isOk()) return handlers.ok(source.value);
|
|
237
319
|
if (source.isErr()) return handlers.err(source.error);
|
|
238
|
-
throw new
|
|
320
|
+
throw new InvalidResultStateError("match");
|
|
239
321
|
};
|
|
240
322
|
}
|
|
241
323
|
|
|
242
324
|
//#endregion
|
|
243
325
|
//#region src/core/recover.ts
|
|
244
326
|
/**
|
|
245
|
-
* Recover:
|
|
246
|
-
*
|
|
327
|
+
* Recover: converts Err to Ok(defaultValue).
|
|
328
|
+
* Result is guaranteed to be Ok → Error type becomes `never`.
|
|
247
329
|
*/
|
|
248
330
|
function recover(defaultValue) {
|
|
249
331
|
return (source) => {
|
|
250
332
|
if (source.isOk()) return source;
|
|
251
333
|
if (source.isErr()) return ok(defaultValue);
|
|
252
|
-
throw new
|
|
334
|
+
throw new InvalidResultStateError("recover");
|
|
253
335
|
};
|
|
254
336
|
}
|
|
255
337
|
/**
|
|
256
|
-
*
|
|
338
|
+
* Like `recover`, but calculates the default value based on the error.
|
|
257
339
|
*/
|
|
258
340
|
function recoverWith(fn) {
|
|
259
341
|
return (source) => {
|
|
260
342
|
if (source.isOk()) return source;
|
|
261
343
|
if (source.isErr()) return ok(fn(source.error));
|
|
262
|
-
throw new
|
|
344
|
+
throw new InvalidResultStateError("recoverWith");
|
|
263
345
|
};
|
|
264
346
|
}
|
|
265
347
|
|
|
266
348
|
//#endregion
|
|
267
349
|
//#region src/core/swap.ts
|
|
268
350
|
/**
|
|
269
|
-
*
|
|
351
|
+
* Swaps Ok and Err.
|
|
270
352
|
* Result<T, E> → Result<E, T>
|
|
271
353
|
*/
|
|
272
354
|
function swap(result) {
|
|
273
355
|
if (result.isOk()) return err(result.value);
|
|
274
356
|
if (result.isErr()) return ok(result.error);
|
|
275
|
-
throw new
|
|
357
|
+
throw new InvalidResultStateError("swap");
|
|
276
358
|
}
|
|
277
359
|
|
|
278
360
|
//#endregion
|
|
279
361
|
//#region src/core/tryCatch.ts
|
|
280
362
|
/**
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
363
|
+
* Executes a function and catches exceptions.
|
|
364
|
+
* Converts exceptions into Result<E>.
|
|
365
|
+
* Corresponds to Rust `Result::from` for fallible operations.
|
|
284
366
|
*/
|
|
285
367
|
function tryCatch(fn, errorMapper) {
|
|
286
368
|
return (source) => {
|
|
@@ -296,14 +378,14 @@ function tryCatch(fn, errorMapper) {
|
|
|
296
378
|
//#endregion
|
|
297
379
|
//#region src/core/tryMap.ts
|
|
298
380
|
/**
|
|
299
|
-
*
|
|
381
|
+
* Like `map`, but catches exceptions and converts them to Err.
|
|
300
382
|
*/
|
|
301
383
|
function tryMap(project, errorMapper) {
|
|
302
384
|
return (source) => {
|
|
303
385
|
if (source.isErr()) return source;
|
|
304
386
|
try {
|
|
305
387
|
if (source.isOk()) return ok(project(source.value));
|
|
306
|
-
throw new
|
|
388
|
+
throw new InvalidResultStateError("tryMap");
|
|
307
389
|
} catch (error) {
|
|
308
390
|
return err(errorMapper ? errorMapper(error) : error);
|
|
309
391
|
}
|
|
@@ -325,7 +407,7 @@ function collectFirstOk(results) {
|
|
|
325
407
|
errors.push(result.error);
|
|
326
408
|
continue;
|
|
327
409
|
}
|
|
328
|
-
throw new
|
|
410
|
+
throw new InvalidResultStateError("collectFirstOk");
|
|
329
411
|
}
|
|
330
412
|
return err(errors);
|
|
331
413
|
}
|
|
@@ -333,7 +415,7 @@ function collectFirstOk(results) {
|
|
|
333
415
|
//#endregion
|
|
334
416
|
//#region src/core/mapAsync.ts
|
|
335
417
|
/**
|
|
336
|
-
* Async
|
|
418
|
+
* Async version of map.
|
|
337
419
|
*/
|
|
338
420
|
function mapAsync(project) {
|
|
339
421
|
return async (source) => {
|
|
@@ -345,7 +427,7 @@ function mapAsync(project) {
|
|
|
345
427
|
//#endregion
|
|
346
428
|
//#region src/core/mapErrAsync.ts
|
|
347
429
|
/**
|
|
348
|
-
* Async
|
|
430
|
+
* Async version of mapErr.
|
|
349
431
|
*/
|
|
350
432
|
function mapErrAsync(project) {
|
|
351
433
|
return async (source) => {
|
|
@@ -357,7 +439,7 @@ function mapErrAsync(project) {
|
|
|
357
439
|
//#endregion
|
|
358
440
|
//#region src/core/flatMapAsync.ts
|
|
359
441
|
/**
|
|
360
|
-
* Async
|
|
442
|
+
* Async version of flatMap.
|
|
361
443
|
*/
|
|
362
444
|
function flatMapAsync(project) {
|
|
363
445
|
return async (source) => {
|
|
@@ -369,7 +451,7 @@ function flatMapAsync(project) {
|
|
|
369
451
|
//#endregion
|
|
370
452
|
//#region src/core/tapAsync.ts
|
|
371
453
|
/**
|
|
372
|
-
* Async
|
|
454
|
+
* Async version of tap.
|
|
373
455
|
*/
|
|
374
456
|
function tapAsync(observer) {
|
|
375
457
|
return async (source) => {
|
|
@@ -382,7 +464,7 @@ function tapAsync(observer) {
|
|
|
382
464
|
//#endregion
|
|
383
465
|
//#region src/core/filterAsync.ts
|
|
384
466
|
/**
|
|
385
|
-
* Async
|
|
467
|
+
* Async version of filter.
|
|
386
468
|
*/
|
|
387
469
|
function filterAsync(predicate, errorFn) {
|
|
388
470
|
return async (source) => {
|
|
@@ -397,20 +479,20 @@ function filterAsync(predicate, errorFn) {
|
|
|
397
479
|
//#endregion
|
|
398
480
|
//#region src/core/matchAsync.ts
|
|
399
481
|
/**
|
|
400
|
-
* Async
|
|
482
|
+
* Async version of match.
|
|
401
483
|
*/
|
|
402
484
|
function matchAsync(handlers) {
|
|
403
485
|
return async (source) => {
|
|
404
486
|
if (source.isOk()) return await handlers.ok(source.value);
|
|
405
487
|
if (source.isErr()) return await handlers.err(source.error);
|
|
406
|
-
throw new
|
|
488
|
+
throw new InvalidResultStateError("matchAsync");
|
|
407
489
|
};
|
|
408
490
|
}
|
|
409
491
|
|
|
410
492
|
//#endregion
|
|
411
493
|
//#region src/core/tryCatchAsync.ts
|
|
412
494
|
/**
|
|
413
|
-
* Async
|
|
495
|
+
* Async version of tryCatch.
|
|
414
496
|
*/
|
|
415
497
|
function tryCatchAsync(fn, errorMapper) {
|
|
416
498
|
return async (source) => {
|
|
@@ -426,14 +508,14 @@ function tryCatchAsync(fn, errorMapper) {
|
|
|
426
508
|
//#endregion
|
|
427
509
|
//#region src/core/tryMapAsync.ts
|
|
428
510
|
/**
|
|
429
|
-
* Async
|
|
511
|
+
* Async version of tryMap.
|
|
430
512
|
*/
|
|
431
513
|
function tryMapAsync(project, errorMapper) {
|
|
432
514
|
return async (source) => {
|
|
433
515
|
if (source.isErr()) return source;
|
|
434
516
|
try {
|
|
435
517
|
if (source.isOk()) return ok(await project(source.value));
|
|
436
|
-
throw new
|
|
518
|
+
throw new InvalidResultStateError("tryMapAsync");
|
|
437
519
|
} catch (error) {
|
|
438
520
|
return err(errorMapper ? errorMapper(error) : error);
|
|
439
521
|
}
|
|
@@ -458,7 +540,7 @@ var ResultBase = class extends Pipeable {
|
|
|
458
540
|
fold(onOk, onErr) {
|
|
459
541
|
if (this._tag === "Ok") return onOk(this.value);
|
|
460
542
|
if (this._tag === "Err") return onErr(this.error);
|
|
461
|
-
throw new
|
|
543
|
+
throw new InvalidResultStateError("Result.fold");
|
|
462
544
|
}
|
|
463
545
|
/**
|
|
464
546
|
* Enables `yield* result` in generators (Do-notation).
|
|
@@ -471,27 +553,27 @@ var ResultBase = class extends Pipeable {
|
|
|
471
553
|
return yield this;
|
|
472
554
|
}
|
|
473
555
|
/**
|
|
474
|
-
*
|
|
556
|
+
* Matches on the Err value via `.when(...)` chain.
|
|
475
557
|
*
|
|
476
|
-
*
|
|
477
|
-
*
|
|
558
|
+
* Note: for type safety reasons, `.match()` can only be called on a Result already narrowed to `Err`,
|
|
559
|
+
* e.g. inside `if (result.isErr()) { ... }`.
|
|
478
560
|
*/
|
|
479
561
|
match() {
|
|
480
562
|
if (this._tag === "Err") return new ErrorMatchBuilder(this.error);
|
|
481
|
-
throw new
|
|
563
|
+
throw new MatchOnOkError();
|
|
482
564
|
}
|
|
483
565
|
/**
|
|
484
|
-
*
|
|
485
|
-
* -
|
|
486
|
-
* -
|
|
566
|
+
* Matches on the Err value, but normalizes every branch to a `Result`:
|
|
567
|
+
* - Handlers may return a `Result` (is returned directly)
|
|
568
|
+
* - or an Error value (is wrapped into `Err(error)`)
|
|
487
569
|
*/
|
|
488
570
|
matchErr() {
|
|
489
571
|
const makeErr = (error) => err(error);
|
|
490
572
|
return ErrMatchBuilder.fromResult(this, makeErr);
|
|
491
573
|
}
|
|
492
574
|
/**
|
|
493
|
-
*
|
|
494
|
-
*
|
|
575
|
+
* Serializes the Result into a simple object format.
|
|
576
|
+
* Preserves the original types.
|
|
495
577
|
*/
|
|
496
578
|
serialize() {
|
|
497
579
|
if (this._tag === "Ok") return {
|
|
@@ -504,8 +586,8 @@ var ResultBase = class extends Pipeable {
|
|
|
504
586
|
};
|
|
505
587
|
}
|
|
506
588
|
/**
|
|
507
|
-
*
|
|
508
|
-
*
|
|
589
|
+
* Serializes the Result into a user-friendly format.
|
|
590
|
+
* Converts Errors to readable strings.
|
|
509
591
|
*/
|
|
510
592
|
toUserFriendly() {
|
|
511
593
|
if (this._tag === "Ok") return {
|
|
@@ -613,7 +695,7 @@ async function task(makeGenerator, onThrow) {
|
|
|
613
695
|
return err(onThrow(caught));
|
|
614
696
|
}
|
|
615
697
|
const yielded = step.value;
|
|
616
|
-
if (!isResult(yielded)) throw new
|
|
698
|
+
if (!isResult(yielded)) throw new TaskYieldNotResultError(yielded);
|
|
617
699
|
if (yielded.isOk()) {
|
|
618
700
|
input = yielded.value;
|
|
619
701
|
continue;
|
|
@@ -627,7 +709,7 @@ async function task(makeGenerator, onThrow) {
|
|
|
627
709
|
}
|
|
628
710
|
return yielded;
|
|
629
711
|
}
|
|
630
|
-
throw new
|
|
712
|
+
throw new InvalidResultStateError("task");
|
|
631
713
|
}
|
|
632
714
|
}
|
|
633
715
|
const gen = task;
|
|
@@ -644,7 +726,7 @@ function fold(handlers) {
|
|
|
644
726
|
return (source) => {
|
|
645
727
|
if (source.isOk()) return handlers.ok(source.value);
|
|
646
728
|
if (source.isErr()) return handlers.err(source.error);
|
|
647
|
-
throw new
|
|
729
|
+
throw new InvalidResultStateError("fold");
|
|
648
730
|
};
|
|
649
731
|
}
|
|
650
732
|
|
|
@@ -660,27 +742,27 @@ function foldAsync(handlers) {
|
|
|
660
742
|
return async (source) => {
|
|
661
743
|
if (source.isOk()) return await handlers.ok(source.value);
|
|
662
744
|
if (source.isErr()) return await handlers.err(source.error);
|
|
663
|
-
throw new
|
|
745
|
+
throw new InvalidResultStateError("foldAsync");
|
|
664
746
|
};
|
|
665
747
|
}
|
|
666
748
|
|
|
667
749
|
//#endregion
|
|
668
750
|
//#region src/core/unwrap.ts
|
|
669
751
|
/**
|
|
670
|
-
*
|
|
671
|
-
*
|
|
752
|
+
* Returns the value or throws an Error.
|
|
753
|
+
* Corresponds to Rust `unwrap`.
|
|
672
754
|
*/
|
|
673
755
|
function unwrap(result) {
|
|
674
756
|
if (result.isOk()) return result.value;
|
|
675
|
-
if (result.isErr()) throw new
|
|
676
|
-
throw new
|
|
757
|
+
if (result.isErr()) throw new UnwrapOnErrError(result.error);
|
|
758
|
+
throw new InvalidResultStateError("unwrap");
|
|
677
759
|
}
|
|
678
760
|
|
|
679
761
|
//#endregion
|
|
680
762
|
//#region src/core/unwrapOr.ts
|
|
681
763
|
/**
|
|
682
|
-
*
|
|
683
|
-
* Pure function
|
|
764
|
+
* Returns the value or a default value.
|
|
765
|
+
* Pure function alternative to the instance method.
|
|
684
766
|
*/
|
|
685
767
|
function unwrapOr(result, defaultValue) {
|
|
686
768
|
if (result.isOk()) return result.value;
|
|
@@ -690,20 +772,20 @@ function unwrapOr(result, defaultValue) {
|
|
|
690
772
|
//#endregion
|
|
691
773
|
//#region src/core/unwrapOrElse.ts
|
|
692
774
|
/**
|
|
693
|
-
*
|
|
694
|
-
*
|
|
775
|
+
* Returns the value or calculates a default value using a function.
|
|
776
|
+
* Corresponds to Rust `unwrap_or_else`.
|
|
695
777
|
*/
|
|
696
778
|
function unwrapOrElse(result, fn) {
|
|
697
779
|
if (result.isOk()) return result.value;
|
|
698
780
|
if (result.isErr()) return fn(result.error);
|
|
699
|
-
throw new
|
|
781
|
+
throw new InvalidResultStateError("unwrapOrElse");
|
|
700
782
|
}
|
|
701
783
|
|
|
702
784
|
//#endregion
|
|
703
785
|
//#region src/core/unwrapOrDefault.ts
|
|
704
786
|
/**
|
|
705
|
-
* Alias
|
|
706
|
-
*
|
|
787
|
+
* Alias for `unwrapOr`.
|
|
788
|
+
* Corresponds to Rust `unwrap_or_default` (with explicit default value).
|
|
707
789
|
*/
|
|
708
790
|
function unwrapOrDefault(result, defaultValue) {
|
|
709
791
|
return unwrapOr(result, defaultValue);
|
|
@@ -712,54 +794,54 @@ function unwrapOrDefault(result, defaultValue) {
|
|
|
712
794
|
//#endregion
|
|
713
795
|
//#region src/core/unwrapOrThrow.ts
|
|
714
796
|
/**
|
|
715
|
-
*
|
|
716
|
-
*
|
|
797
|
+
* Returns the value or throws the original Err value (not wrapped).
|
|
798
|
+
* Useful to preserve `Error` instances including stack traces.
|
|
717
799
|
*/
|
|
718
800
|
function unwrapOrThrow(result) {
|
|
719
801
|
if (result.isOk()) return result.value;
|
|
720
802
|
if (result.isErr()) throw result.error;
|
|
721
|
-
throw new
|
|
803
|
+
throw new InvalidResultStateError("unwrapOrThrow");
|
|
722
804
|
}
|
|
723
805
|
|
|
724
806
|
//#endregion
|
|
725
807
|
//#region src/core/unwrapErr.ts
|
|
726
808
|
/**
|
|
727
|
-
*
|
|
728
|
-
*
|
|
809
|
+
* Returns the error or throws an Error.
|
|
810
|
+
* Corresponds to Rust `unwrap_err`.
|
|
729
811
|
*/
|
|
730
812
|
function unwrapErr(result) {
|
|
731
813
|
if (result.isErr()) return result.error;
|
|
732
|
-
if (result.isOk()) throw new
|
|
733
|
-
throw new
|
|
814
|
+
if (result.isOk()) throw new UnwrapErrOnOkError(result.value);
|
|
815
|
+
throw new InvalidResultStateError("unwrapErr");
|
|
734
816
|
}
|
|
735
817
|
|
|
736
818
|
//#endregion
|
|
737
819
|
//#region src/core/expectResult.ts
|
|
738
820
|
/**
|
|
739
|
-
*
|
|
740
|
-
*
|
|
821
|
+
* Returns the value or throws an Error with a custom message.
|
|
822
|
+
* Corresponds to Rust `expect`.
|
|
741
823
|
*/
|
|
742
824
|
function expectResult(result, message) {
|
|
743
825
|
if (result.isOk()) return result.value;
|
|
744
|
-
throw new
|
|
826
|
+
throw new ExpectOkError(message);
|
|
745
827
|
}
|
|
746
828
|
|
|
747
829
|
//#endregion
|
|
748
830
|
//#region src/core/expectErr.ts
|
|
749
831
|
/**
|
|
750
|
-
*
|
|
751
|
-
*
|
|
832
|
+
* Returns the error or throws an Error with a custom message.
|
|
833
|
+
* Corresponds to Rust `expect_err`.
|
|
752
834
|
*/
|
|
753
835
|
function expectErr(result, message) {
|
|
754
836
|
if (result.isErr()) return result.error;
|
|
755
|
-
throw new
|
|
837
|
+
throw new ExpectErrError(message);
|
|
756
838
|
}
|
|
757
839
|
|
|
758
840
|
//#endregion
|
|
759
841
|
//#region src/core/and.ts
|
|
760
842
|
/**
|
|
761
|
-
*
|
|
762
|
-
*
|
|
843
|
+
* Combines two Results. Returns the second one only if the first is Ok.
|
|
844
|
+
* Corresponds to Rust `and`.
|
|
763
845
|
*/
|
|
764
846
|
function and(result, other) {
|
|
765
847
|
return result.isOk() ? other : result;
|
|
@@ -768,8 +850,8 @@ function and(result, other) {
|
|
|
768
850
|
//#endregion
|
|
769
851
|
//#region src/core/or.ts
|
|
770
852
|
/**
|
|
771
|
-
* Fallback
|
|
772
|
-
*
|
|
853
|
+
* Fallback to another Result if the first is Err.
|
|
854
|
+
* Corresponds to Rust `or`.
|
|
773
855
|
*/
|
|
774
856
|
function or(result, other) {
|
|
775
857
|
return result.isOk() ? result : other;
|
|
@@ -778,20 +860,20 @@ function or(result, other) {
|
|
|
778
860
|
//#endregion
|
|
779
861
|
//#region src/core/orElse.ts
|
|
780
862
|
/**
|
|
781
|
-
* Fallback
|
|
782
|
-
*
|
|
863
|
+
* Fallback with a function that returns a Result.
|
|
864
|
+
* Corresponds to Rust `or_else`.
|
|
783
865
|
*/
|
|
784
866
|
function orElse(result, fn) {
|
|
785
867
|
if (result.isOk()) return result;
|
|
786
868
|
if (result.isErr()) return fn(result.error);
|
|
787
|
-
throw new
|
|
869
|
+
throw new InvalidResultStateError("orElse");
|
|
788
870
|
}
|
|
789
871
|
|
|
790
872
|
//#endregion
|
|
791
873
|
//#region src/core/mapOr.ts
|
|
792
874
|
/**
|
|
793
|
-
*
|
|
794
|
-
*
|
|
875
|
+
* Transforms the value or returns a default value.
|
|
876
|
+
* Corresponds to Rust `map_or`.
|
|
795
877
|
*/
|
|
796
878
|
function mapOr(result, defaultValue, fn) {
|
|
797
879
|
if (result.isOk()) return fn(result.value);
|
|
@@ -801,21 +883,21 @@ function mapOr(result, defaultValue, fn) {
|
|
|
801
883
|
//#endregion
|
|
802
884
|
//#region src/core/mapOrElse.ts
|
|
803
885
|
/**
|
|
804
|
-
*
|
|
805
|
-
*
|
|
886
|
+
* Transforms the value or calculates a default value using a function.
|
|
887
|
+
* Corresponds to Rust `map_or_else`.
|
|
806
888
|
*/
|
|
807
889
|
function mapOrElse(result, defaultFn, fn) {
|
|
808
890
|
if (result.isOk()) return fn(result.value);
|
|
809
891
|
if (result.isErr()) return defaultFn(result.error);
|
|
810
|
-
throw new
|
|
892
|
+
throw new InvalidResultStateError("mapOrElse");
|
|
811
893
|
}
|
|
812
894
|
|
|
813
895
|
//#endregion
|
|
814
896
|
//#region src/core/sequence.ts
|
|
815
897
|
/**
|
|
816
|
-
*
|
|
817
|
-
* Short-circuits
|
|
818
|
-
*
|
|
898
|
+
* Combines a list of Results into a single Result of a list.
|
|
899
|
+
* Short-circuits on the first Err.
|
|
900
|
+
* Analogous to Rust `collect::<Result<Vec<_>, _>>()`.
|
|
819
901
|
*/
|
|
820
902
|
function sequence(results) {
|
|
821
903
|
const values = [];
|
|
@@ -825,12 +907,12 @@ function sequence(results) {
|
|
|
825
907
|
continue;
|
|
826
908
|
}
|
|
827
909
|
if (result.isErr()) return result;
|
|
828
|
-
throw new
|
|
910
|
+
throw new InvalidResultStateError("sequence");
|
|
829
911
|
}
|
|
830
912
|
return ok(values);
|
|
831
913
|
}
|
|
832
914
|
/**
|
|
833
|
-
* Alias
|
|
915
|
+
* Alias for `sequence`.
|
|
834
916
|
*/
|
|
835
917
|
function all(results) {
|
|
836
918
|
return sequence(results);
|
|
@@ -839,8 +921,8 @@ function all(results) {
|
|
|
839
921
|
//#endregion
|
|
840
922
|
//#region src/core/sequenceRecord.ts
|
|
841
923
|
/**
|
|
842
|
-
*
|
|
843
|
-
* Short-circuits
|
|
924
|
+
* Like `sequence`, but for Records/Objects.
|
|
925
|
+
* Short-circuits on the first Err.
|
|
844
926
|
*/
|
|
845
927
|
function sequenceRecord(record) {
|
|
846
928
|
const out = {};
|
|
@@ -852,7 +934,7 @@ function sequenceRecord(record) {
|
|
|
852
934
|
continue;
|
|
853
935
|
}
|
|
854
936
|
if (result.isErr()) return result;
|
|
855
|
-
throw new
|
|
937
|
+
throw new InvalidResultStateError("sequenceRecord");
|
|
856
938
|
}
|
|
857
939
|
return ok(out);
|
|
858
940
|
}
|
|
@@ -860,11 +942,11 @@ function sequenceRecord(record) {
|
|
|
860
942
|
//#endregion
|
|
861
943
|
//#region src/core/collectFirstOkAsync.ts
|
|
862
944
|
/**
|
|
863
|
-
* Async
|
|
945
|
+
* Async version of collectFirstOk.
|
|
864
946
|
*
|
|
865
|
-
* -
|
|
866
|
-
* -
|
|
867
|
-
* -
|
|
947
|
+
* - Takes either already started Promises or "Thunks" (`() => Awaitable<Result<...>>`).
|
|
948
|
+
* - Processes inputs strictly sequentially (like `for ... of` + `await`).
|
|
949
|
+
* - Returns the first `Ok` and collects all errors if no `Ok` is found.
|
|
868
950
|
*/
|
|
869
951
|
async function collectFirstOkAsync(inputs) {
|
|
870
952
|
const errors = [];
|
|
@@ -875,7 +957,7 @@ async function collectFirstOkAsync(inputs) {
|
|
|
875
957
|
errors.push(result.error);
|
|
876
958
|
continue;
|
|
877
959
|
}
|
|
878
|
-
throw new
|
|
960
|
+
throw new InvalidResultStateError("collectFirstOkAsync");
|
|
879
961
|
} catch (error) {
|
|
880
962
|
errors.push(error);
|
|
881
963
|
}
|
|
@@ -885,15 +967,15 @@ async function collectFirstOkAsync(inputs) {
|
|
|
885
967
|
//#endregion
|
|
886
968
|
//#region src/core/collectFirstOkParallelAsync.ts
|
|
887
969
|
/**
|
|
888
|
-
* Parallel
|
|
970
|
+
* Parallel version of `collectFirstOkAsync`.
|
|
889
971
|
*
|
|
890
|
-
* -
|
|
891
|
-
* -
|
|
892
|
-
* -
|
|
893
|
-
* - Rejections
|
|
894
|
-
* -
|
|
895
|
-
*
|
|
896
|
-
* -
|
|
972
|
+
* - Starts all inputs immediately (Promises or Thunks).
|
|
973
|
+
* - Returns the first `Ok` as soon as it is available.
|
|
974
|
+
* - If no `Ok` is found, returns an `Err` with all error values (in input order).
|
|
975
|
+
* - Rejections are treated as `ErrValue` (`caught as ErrValue`).
|
|
976
|
+
* - If multiple inputs provide an `Ok`, the one that completes first wins.
|
|
977
|
+
* In case of simultaneous completion, the first observed result wins.
|
|
978
|
+
* - If no `Ok` arrives and at least one input never settles, the Promise remains pending.
|
|
897
979
|
*/
|
|
898
980
|
async function collectFirstOkParallelAsync(inputs) {
|
|
899
981
|
if (inputs.length === 0) return err([]);
|
|
@@ -908,7 +990,7 @@ async function collectFirstOkParallelAsync(inputs) {
|
|
|
908
990
|
for (const entry of settled) if (entry.status === "fulfilled") {
|
|
909
991
|
const result = entry.value;
|
|
910
992
|
if (result.isErr()) errors.push(result.error);
|
|
911
|
-
else if (!result.isOk()) errors.push(
|
|
993
|
+
else if (!result.isOk()) errors.push(new InvalidResultStateError("collectFirstOkParallelAsync"));
|
|
912
994
|
} else errors.push(entry.reason);
|
|
913
995
|
return err(errors);
|
|
914
996
|
});
|
|
@@ -918,8 +1000,8 @@ async function collectFirstOkParallelAsync(inputs) {
|
|
|
918
1000
|
//#endregion
|
|
919
1001
|
//#region src/core/collectAllErrors.ts
|
|
920
1002
|
/**
|
|
921
|
-
*
|
|
922
|
-
*
|
|
1003
|
+
* Combines a list of Results.
|
|
1004
|
+
* Return Ok(values) only if all are Ok, otherwise Err([errors]).
|
|
923
1005
|
*/
|
|
924
1006
|
function collectAllErrors(results) {
|
|
925
1007
|
const values = [];
|
|
@@ -933,7 +1015,7 @@ function collectAllErrors(results) {
|
|
|
933
1015
|
errors.push(result.error);
|
|
934
1016
|
continue;
|
|
935
1017
|
}
|
|
936
|
-
throw new
|
|
1018
|
+
throw new InvalidResultStateError("collectAllErrors");
|
|
937
1019
|
}
|
|
938
1020
|
return errors.length === 0 ? ok(values) : err(errors);
|
|
939
1021
|
}
|
|
@@ -941,7 +1023,7 @@ function collectAllErrors(results) {
|
|
|
941
1023
|
//#endregion
|
|
942
1024
|
//#region src/core/partition.ts
|
|
943
1025
|
/**
|
|
944
|
-
*
|
|
1026
|
+
* Partitions Results into Ok values and Err errors.
|
|
945
1027
|
*/
|
|
946
1028
|
function partition(results) {
|
|
947
1029
|
const oks = [];
|
|
@@ -955,7 +1037,7 @@ function partition(results) {
|
|
|
955
1037
|
errs.push(result.error);
|
|
956
1038
|
continue;
|
|
957
1039
|
}
|
|
958
|
-
throw new
|
|
1040
|
+
throw new InvalidResultStateError("partition");
|
|
959
1041
|
}
|
|
960
1042
|
return [oks, errs];
|
|
961
1043
|
}
|
|
@@ -963,9 +1045,9 @@ function partition(results) {
|
|
|
963
1045
|
//#endregion
|
|
964
1046
|
//#region src/core/flatten.ts
|
|
965
1047
|
/**
|
|
966
|
-
*
|
|
1048
|
+
* Flattens a nested Result.
|
|
967
1049
|
* Result<Result<T, E>, E> → Result<T, E>
|
|
968
|
-
*
|
|
1050
|
+
* Corresponds to Rust `flatten`.
|
|
969
1051
|
*/
|
|
970
1052
|
function flatten(result) {
|
|
971
1053
|
if (result.isOk()) return result.value;
|
|
@@ -975,19 +1057,19 @@ function flatten(result) {
|
|
|
975
1057
|
//#endregion
|
|
976
1058
|
//#region src/core/toPromise.ts
|
|
977
1059
|
/**
|
|
978
|
-
*
|
|
1060
|
+
* Converts a Result to a Promise.
|
|
979
1061
|
* Ok → resolve(value), Err → reject(error)
|
|
980
1062
|
*/
|
|
981
1063
|
function toPromise(result) {
|
|
982
1064
|
if (result.isOk()) return Promise.resolve(result.value);
|
|
983
1065
|
if (result.isErr()) return Promise.reject(result.error);
|
|
984
|
-
throw new
|
|
1066
|
+
throw new InvalidResultStateError("toPromise");
|
|
985
1067
|
}
|
|
986
1068
|
|
|
987
1069
|
//#endregion
|
|
988
1070
|
//#region src/core/toNullable.ts
|
|
989
1071
|
/**
|
|
990
|
-
*
|
|
1072
|
+
* Converts a Result to `T | null`.
|
|
991
1073
|
* Ok → value, Err → null
|
|
992
1074
|
*/
|
|
993
1075
|
function toNullable(result) {
|
|
@@ -998,8 +1080,8 @@ function toNullable(result) {
|
|
|
998
1080
|
//#endregion
|
|
999
1081
|
//#region src/core/isOk.ts
|
|
1000
1082
|
/**
|
|
1001
|
-
*
|
|
1002
|
-
* Pure function
|
|
1083
|
+
* Checks if a Result is Ok.
|
|
1084
|
+
* Pure function alternative to the instance method.
|
|
1003
1085
|
*/
|
|
1004
1086
|
function isOk(result) {
|
|
1005
1087
|
return result.isOk();
|
|
@@ -1008,8 +1090,8 @@ function isOk(result) {
|
|
|
1008
1090
|
//#endregion
|
|
1009
1091
|
//#region src/core/isErr.ts
|
|
1010
1092
|
/**
|
|
1011
|
-
*
|
|
1012
|
-
* Pure function
|
|
1093
|
+
* Checks if a Result is Err.
|
|
1094
|
+
* Pure function alternative to the instance method.
|
|
1013
1095
|
*/
|
|
1014
1096
|
function isErr(result) {
|
|
1015
1097
|
return result.isErr();
|
|
@@ -1018,8 +1100,8 @@ function isErr(result) {
|
|
|
1018
1100
|
//#endregion
|
|
1019
1101
|
//#region src/core/contains.ts
|
|
1020
1102
|
/**
|
|
1021
|
-
*
|
|
1022
|
-
*
|
|
1103
|
+
* Checks if the Result contains a specific value.
|
|
1104
|
+
* Corresponds to Rust `contains`.
|
|
1023
1105
|
*/
|
|
1024
1106
|
function contains(result, value) {
|
|
1025
1107
|
if (!result.isOk()) return false;
|
|
@@ -1029,8 +1111,8 @@ function contains(result, value) {
|
|
|
1029
1111
|
//#endregion
|
|
1030
1112
|
//#region src/core/containsErr.ts
|
|
1031
1113
|
/**
|
|
1032
|
-
*
|
|
1033
|
-
*
|
|
1114
|
+
* Checks if the Result contains a specific error.
|
|
1115
|
+
* Analogous to `contains` for the Err case.
|
|
1034
1116
|
*/
|
|
1035
1117
|
function containsErr(result, error) {
|
|
1036
1118
|
if (!result.isErr()) return false;
|
|
@@ -1038,5 +1120,5 @@ function containsErr(result, error) {
|
|
|
1038
1120
|
}
|
|
1039
1121
|
|
|
1040
1122
|
//#endregion
|
|
1041
|
-
export { Err, Ok, Result, all, and, bimap, collectAllErrors, collectFirstOk, collectFirstOkAsync, collectFirstOkParallelAsync, combine, contains, containsErr, err, expectErr, expectResult, filter, filterAsync, flatMap, flatMapAsync, flatten, fold, foldAsync, fromNullable, fromPromise, gen, isErr, isOk, isResult, map, mapAsync, mapBoth, mapErr, mapErrAsync, mapOr, mapOrElse, match, matchAsync, ok, okIf, okIfLazy, or, orElse, partition, recover, recoverWith, sequence, sequenceRecord, swap, tap, tapAsync, task, toNullable, toPromise, tryCatch, tryCatchAsync, tryFn, tryMap, tryMapAsync, unwrap, unwrapErr, unwrapOr, unwrapOrDefault, unwrapOrElse, unwrapOrThrow, zip };
|
|
1123
|
+
export { ERR_EXPECT_ERR, ERR_EXPECT_OK, ERR_INVALID_STATE, ERR_MATCH_ON_OK, ERR_TASK_YIELD_NOT_RESULT, ERR_UNWRAP_ERR_ON_OK, ERR_UNWRAP_ON_ERR, Err, ExpectErrError, ExpectOkError, InvalidResultStateError, MatchOnOkError, Ok, Result, ResultError, ResultTypeError, TaskYieldNotResultError, UnwrapErrOnOkError, UnwrapOnErrError, all, and, bimap, collectAllErrors, collectFirstOk, collectFirstOkAsync, collectFirstOkParallelAsync, combine, contains, containsErr, err, expectErr, expectResult, filter, filterAsync, flatMap, flatMapAsync, flatten, fold, foldAsync, fromNullable, fromPromise, gen, isErr, isOk, isResult, map, mapAsync, mapBoth, mapErr, mapErrAsync, mapOr, mapOrElse, match, matchAsync, ok, okIf, okIfLazy, or, orElse, partition, recover, recoverWith, sequence, sequenceRecord, swap, tap, tapAsync, task, toNullable, toPromise, tryCatch, tryCatchAsync, tryFn, tryMap, tryMapAsync, unwrap, unwrapErr, unwrapOr, unwrapOrDefault, unwrapOrElse, unwrapOrThrow, zip };
|
|
1042
1124
|
//# sourceMappingURL=index.mjs.map
|