@shirudo/result 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +108 -108
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +113 -113
- package/dist/index.d.mts +113 -113
- package/dist/index.mjs +108 -108
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -97,11 +97,11 @@ var ExpectErrError = class extends ResultError {
|
|
|
97
97
|
//#endregion
|
|
98
98
|
//#region src/core/matcher.ts
|
|
99
99
|
/**
|
|
100
|
-
* Matcher
|
|
100
|
+
* Matcher for Err values (returns an arbitrary return type, e.g. string messages).
|
|
101
101
|
*
|
|
102
|
-
* - `.when(Ctor, handler)`
|
|
103
|
-
* - `.whenGuard(guard, handler)`
|
|
104
|
-
* - `.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`)
|
|
105
105
|
*/
|
|
106
106
|
var ErrorMatchBuilder = class ErrorMatchBuilder {
|
|
107
107
|
#error;
|
|
@@ -137,11 +137,11 @@ function isResult$1(value) {
|
|
|
137
137
|
return typeof value === "object" && value !== null && "isOk" in value && typeof value.isOk === "function" && "isErr" in value && typeof value.isErr === "function";
|
|
138
138
|
}
|
|
139
139
|
/**
|
|
140
|
-
* Matcher
|
|
140
|
+
* Matcher for `Result` Errors, which returns a `Result` again.
|
|
141
141
|
*
|
|
142
|
-
*
|
|
143
|
-
* -
|
|
144
|
-
* -
|
|
142
|
+
* Handlers may:
|
|
143
|
+
* - return a `Result` (is returned directly)
|
|
144
|
+
* - return an Error value (is automatically wrapped into `Err(error)`)
|
|
145
145
|
*/
|
|
146
146
|
var ErrMatchBuilder = class ErrMatchBuilder {
|
|
147
147
|
#makeErr;
|
|
@@ -191,8 +191,8 @@ var ErrMatchBuilder = class ErrMatchBuilder {
|
|
|
191
191
|
//#endregion
|
|
192
192
|
//#region src/core/map.ts
|
|
193
193
|
/**
|
|
194
|
-
*
|
|
195
|
-
*
|
|
194
|
+
* Transforms the value (Ok case).
|
|
195
|
+
* Corresponds to Rust `map`.
|
|
196
196
|
*/
|
|
197
197
|
function map(project) {
|
|
198
198
|
return (source) => {
|
|
@@ -204,8 +204,8 @@ function map(project) {
|
|
|
204
204
|
//#endregion
|
|
205
205
|
//#region src/core/mapErr.ts
|
|
206
206
|
/**
|
|
207
|
-
*
|
|
208
|
-
*
|
|
207
|
+
* Transforms the error (Err case).
|
|
208
|
+
* Corresponds to Rust `map_err`.
|
|
209
209
|
*/
|
|
210
210
|
function mapErr(project) {
|
|
211
211
|
return (source) => {
|
|
@@ -217,8 +217,8 @@ function mapErr(project) {
|
|
|
217
217
|
//#endregion
|
|
218
218
|
//#region src/core/mapBoth.ts
|
|
219
219
|
/**
|
|
220
|
-
*
|
|
221
|
-
*
|
|
220
|
+
* Transforms both the Ok value and the Err error.
|
|
221
|
+
* Corresponds to FP `bimap` / `mapBoth`.
|
|
222
222
|
*/
|
|
223
223
|
function mapBoth(mapOk, mapErr$1) {
|
|
224
224
|
return (source) => {
|
|
@@ -228,15 +228,15 @@ function mapBoth(mapOk, mapErr$1) {
|
|
|
228
228
|
};
|
|
229
229
|
}
|
|
230
230
|
/**
|
|
231
|
-
* Alias
|
|
231
|
+
* Alias for `mapBoth`.
|
|
232
232
|
*/
|
|
233
233
|
const bimap = mapBoth;
|
|
234
234
|
|
|
235
235
|
//#endregion
|
|
236
236
|
//#region src/core/flatMap.ts
|
|
237
237
|
/**
|
|
238
|
-
*
|
|
239
|
-
*
|
|
238
|
+
* Chains another operation that returns a Result.
|
|
239
|
+
* Corresponds to Rust `and_then` or JS `flatMap`.
|
|
240
240
|
*/
|
|
241
241
|
function flatMap(project) {
|
|
242
242
|
return (source) => {
|
|
@@ -280,8 +280,8 @@ function combine(...args) {
|
|
|
280
280
|
//#endregion
|
|
281
281
|
//#region src/core/tap.ts
|
|
282
282
|
/**
|
|
283
|
-
*
|
|
284
|
-
*
|
|
283
|
+
* Executes a side effect (logging, debugging) without changing the Result.
|
|
284
|
+
* Corresponds to Rust `inspect` / `inspect_err`.
|
|
285
285
|
*/
|
|
286
286
|
function tap(observer) {
|
|
287
287
|
return (source) => {
|
|
@@ -294,8 +294,8 @@ function tap(observer) {
|
|
|
294
294
|
//#endregion
|
|
295
295
|
//#region src/core/filter.ts
|
|
296
296
|
/**
|
|
297
|
-
*
|
|
298
|
-
*
|
|
297
|
+
* Checks a condition. If false, the Result becomes Err.
|
|
298
|
+
* Corresponds to Rust `filter` (partially).
|
|
299
299
|
*/
|
|
300
300
|
function filter(predicate, errorFn) {
|
|
301
301
|
return (source) => {
|
|
@@ -310,8 +310,8 @@ function filter(predicate, errorFn) {
|
|
|
310
310
|
//#endregion
|
|
311
311
|
//#region src/core/match.ts
|
|
312
312
|
/**
|
|
313
|
-
*
|
|
314
|
-
*
|
|
313
|
+
* Resolves the Result. The end of the pipe.
|
|
314
|
+
* Corresponds to Rust `match`.
|
|
315
315
|
*/
|
|
316
316
|
function match(handlers) {
|
|
317
317
|
return (source) => {
|
|
@@ -324,8 +324,8 @@ function match(handlers) {
|
|
|
324
324
|
//#endregion
|
|
325
325
|
//#region src/core/recover.ts
|
|
326
326
|
/**
|
|
327
|
-
* Recover:
|
|
328
|
-
*
|
|
327
|
+
* Recover: converts Err to Ok(defaultValue).
|
|
328
|
+
* Result is guaranteed to be Ok → Error type becomes `never`.
|
|
329
329
|
*/
|
|
330
330
|
function recover(defaultValue) {
|
|
331
331
|
return (source) => {
|
|
@@ -335,7 +335,7 @@ function recover(defaultValue) {
|
|
|
335
335
|
};
|
|
336
336
|
}
|
|
337
337
|
/**
|
|
338
|
-
*
|
|
338
|
+
* Like `recover`, but calculates the default value based on the error.
|
|
339
339
|
*/
|
|
340
340
|
function recoverWith(fn) {
|
|
341
341
|
return (source) => {
|
|
@@ -348,7 +348,7 @@ function recoverWith(fn) {
|
|
|
348
348
|
//#endregion
|
|
349
349
|
//#region src/core/swap.ts
|
|
350
350
|
/**
|
|
351
|
-
*
|
|
351
|
+
* Swaps Ok and Err.
|
|
352
352
|
* Result<T, E> → Result<E, T>
|
|
353
353
|
*/
|
|
354
354
|
function swap(result) {
|
|
@@ -360,9 +360,9 @@ function swap(result) {
|
|
|
360
360
|
//#endregion
|
|
361
361
|
//#region src/core/tryCatch.ts
|
|
362
362
|
/**
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
*
|
|
363
|
+
* Executes a function and catches exceptions.
|
|
364
|
+
* Converts exceptions into Result<E>.
|
|
365
|
+
* Corresponds to Rust `Result::from` for fallible operations.
|
|
366
366
|
*/
|
|
367
367
|
function tryCatch(fn, errorMapper) {
|
|
368
368
|
return (source) => {
|
|
@@ -378,7 +378,7 @@ function tryCatch(fn, errorMapper) {
|
|
|
378
378
|
//#endregion
|
|
379
379
|
//#region src/core/tryMap.ts
|
|
380
380
|
/**
|
|
381
|
-
*
|
|
381
|
+
* Like `map`, but catches exceptions and converts them to Err.
|
|
382
382
|
*/
|
|
383
383
|
function tryMap(project, errorMapper) {
|
|
384
384
|
return (source) => {
|
|
@@ -415,7 +415,7 @@ function collectFirstOk(results) {
|
|
|
415
415
|
//#endregion
|
|
416
416
|
//#region src/core/mapAsync.ts
|
|
417
417
|
/**
|
|
418
|
-
* Async
|
|
418
|
+
* Async version of map.
|
|
419
419
|
*/
|
|
420
420
|
function mapAsync(project) {
|
|
421
421
|
return async (source) => {
|
|
@@ -427,7 +427,7 @@ function mapAsync(project) {
|
|
|
427
427
|
//#endregion
|
|
428
428
|
//#region src/core/mapErrAsync.ts
|
|
429
429
|
/**
|
|
430
|
-
* Async
|
|
430
|
+
* Async version of mapErr.
|
|
431
431
|
*/
|
|
432
432
|
function mapErrAsync(project) {
|
|
433
433
|
return async (source) => {
|
|
@@ -439,7 +439,7 @@ function mapErrAsync(project) {
|
|
|
439
439
|
//#endregion
|
|
440
440
|
//#region src/core/flatMapAsync.ts
|
|
441
441
|
/**
|
|
442
|
-
* Async
|
|
442
|
+
* Async version of flatMap.
|
|
443
443
|
*/
|
|
444
444
|
function flatMapAsync(project) {
|
|
445
445
|
return async (source) => {
|
|
@@ -451,7 +451,7 @@ function flatMapAsync(project) {
|
|
|
451
451
|
//#endregion
|
|
452
452
|
//#region src/core/tapAsync.ts
|
|
453
453
|
/**
|
|
454
|
-
* Async
|
|
454
|
+
* Async version of tap.
|
|
455
455
|
*/
|
|
456
456
|
function tapAsync(observer) {
|
|
457
457
|
return async (source) => {
|
|
@@ -464,7 +464,7 @@ function tapAsync(observer) {
|
|
|
464
464
|
//#endregion
|
|
465
465
|
//#region src/core/filterAsync.ts
|
|
466
466
|
/**
|
|
467
|
-
* Async
|
|
467
|
+
* Async version of filter.
|
|
468
468
|
*/
|
|
469
469
|
function filterAsync(predicate, errorFn) {
|
|
470
470
|
return async (source) => {
|
|
@@ -479,7 +479,7 @@ function filterAsync(predicate, errorFn) {
|
|
|
479
479
|
//#endregion
|
|
480
480
|
//#region src/core/matchAsync.ts
|
|
481
481
|
/**
|
|
482
|
-
* Async
|
|
482
|
+
* Async version of match.
|
|
483
483
|
*/
|
|
484
484
|
function matchAsync(handlers) {
|
|
485
485
|
return async (source) => {
|
|
@@ -492,7 +492,7 @@ function matchAsync(handlers) {
|
|
|
492
492
|
//#endregion
|
|
493
493
|
//#region src/core/tryCatchAsync.ts
|
|
494
494
|
/**
|
|
495
|
-
* Async
|
|
495
|
+
* Async version of tryCatch.
|
|
496
496
|
*/
|
|
497
497
|
function tryCatchAsync(fn, errorMapper) {
|
|
498
498
|
return async (source) => {
|
|
@@ -508,7 +508,7 @@ function tryCatchAsync(fn, errorMapper) {
|
|
|
508
508
|
//#endregion
|
|
509
509
|
//#region src/core/tryMapAsync.ts
|
|
510
510
|
/**
|
|
511
|
-
* Async
|
|
511
|
+
* Async version of tryMap.
|
|
512
512
|
*/
|
|
513
513
|
function tryMapAsync(project, errorMapper) {
|
|
514
514
|
return async (source) => {
|
|
@@ -553,27 +553,27 @@ var ResultBase = class extends Pipeable {
|
|
|
553
553
|
return yield this;
|
|
554
554
|
}
|
|
555
555
|
/**
|
|
556
|
-
*
|
|
556
|
+
* Matches on the Err value via `.when(...)` chain.
|
|
557
557
|
*
|
|
558
|
-
*
|
|
559
|
-
*
|
|
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()) { ... }`.
|
|
560
560
|
*/
|
|
561
561
|
match() {
|
|
562
562
|
if (this._tag === "Err") return new ErrorMatchBuilder(this.error);
|
|
563
563
|
throw new MatchOnOkError();
|
|
564
564
|
}
|
|
565
565
|
/**
|
|
566
|
-
*
|
|
567
|
-
* -
|
|
568
|
-
* -
|
|
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)`)
|
|
569
569
|
*/
|
|
570
570
|
matchErr() {
|
|
571
571
|
const makeErr = (error) => err(error);
|
|
572
572
|
return ErrMatchBuilder.fromResult(this, makeErr);
|
|
573
573
|
}
|
|
574
574
|
/**
|
|
575
|
-
*
|
|
576
|
-
*
|
|
575
|
+
* Serializes the Result into a simple object format.
|
|
576
|
+
* Preserves the original types.
|
|
577
577
|
*/
|
|
578
578
|
serialize() {
|
|
579
579
|
if (this._tag === "Ok") return {
|
|
@@ -586,8 +586,8 @@ var ResultBase = class extends Pipeable {
|
|
|
586
586
|
};
|
|
587
587
|
}
|
|
588
588
|
/**
|
|
589
|
-
*
|
|
590
|
-
*
|
|
589
|
+
* Serializes the Result into a user-friendly format.
|
|
590
|
+
* Converts Errors to readable strings.
|
|
591
591
|
*/
|
|
592
592
|
toUserFriendly() {
|
|
593
593
|
if (this._tag === "Ok") return {
|
|
@@ -749,8 +749,8 @@ function foldAsync(handlers) {
|
|
|
749
749
|
//#endregion
|
|
750
750
|
//#region src/core/unwrap.ts
|
|
751
751
|
/**
|
|
752
|
-
*
|
|
753
|
-
*
|
|
752
|
+
* Returns the value or throws an Error.
|
|
753
|
+
* Corresponds to Rust `unwrap`.
|
|
754
754
|
*/
|
|
755
755
|
function unwrap(result) {
|
|
756
756
|
if (result.isOk()) return result.value;
|
|
@@ -761,8 +761,8 @@ function unwrap(result) {
|
|
|
761
761
|
//#endregion
|
|
762
762
|
//#region src/core/unwrapOr.ts
|
|
763
763
|
/**
|
|
764
|
-
*
|
|
765
|
-
* Pure function
|
|
764
|
+
* Returns the value or a default value.
|
|
765
|
+
* Pure function alternative to the instance method.
|
|
766
766
|
*/
|
|
767
767
|
function unwrapOr(result, defaultValue) {
|
|
768
768
|
if (result.isOk()) return result.value;
|
|
@@ -772,8 +772,8 @@ function unwrapOr(result, defaultValue) {
|
|
|
772
772
|
//#endregion
|
|
773
773
|
//#region src/core/unwrapOrElse.ts
|
|
774
774
|
/**
|
|
775
|
-
*
|
|
776
|
-
*
|
|
775
|
+
* Returns the value or calculates a default value using a function.
|
|
776
|
+
* Corresponds to Rust `unwrap_or_else`.
|
|
777
777
|
*/
|
|
778
778
|
function unwrapOrElse(result, fn) {
|
|
779
779
|
if (result.isOk()) return result.value;
|
|
@@ -784,8 +784,8 @@ function unwrapOrElse(result, fn) {
|
|
|
784
784
|
//#endregion
|
|
785
785
|
//#region src/core/unwrapOrDefault.ts
|
|
786
786
|
/**
|
|
787
|
-
* Alias
|
|
788
|
-
*
|
|
787
|
+
* Alias for `unwrapOr`.
|
|
788
|
+
* Corresponds to Rust `unwrap_or_default` (with explicit default value).
|
|
789
789
|
*/
|
|
790
790
|
function unwrapOrDefault(result, defaultValue) {
|
|
791
791
|
return unwrapOr(result, defaultValue);
|
|
@@ -794,8 +794,8 @@ function unwrapOrDefault(result, defaultValue) {
|
|
|
794
794
|
//#endregion
|
|
795
795
|
//#region src/core/unwrapOrThrow.ts
|
|
796
796
|
/**
|
|
797
|
-
*
|
|
798
|
-
*
|
|
797
|
+
* Returns the value or throws the original Err value (not wrapped).
|
|
798
|
+
* Useful to preserve `Error` instances including stack traces.
|
|
799
799
|
*/
|
|
800
800
|
function unwrapOrThrow(result) {
|
|
801
801
|
if (result.isOk()) return result.value;
|
|
@@ -806,8 +806,8 @@ function unwrapOrThrow(result) {
|
|
|
806
806
|
//#endregion
|
|
807
807
|
//#region src/core/unwrapErr.ts
|
|
808
808
|
/**
|
|
809
|
-
*
|
|
810
|
-
*
|
|
809
|
+
* Returns the error or throws an Error.
|
|
810
|
+
* Corresponds to Rust `unwrap_err`.
|
|
811
811
|
*/
|
|
812
812
|
function unwrapErr(result) {
|
|
813
813
|
if (result.isErr()) return result.error;
|
|
@@ -818,8 +818,8 @@ function unwrapErr(result) {
|
|
|
818
818
|
//#endregion
|
|
819
819
|
//#region src/core/expectResult.ts
|
|
820
820
|
/**
|
|
821
|
-
*
|
|
822
|
-
*
|
|
821
|
+
* Returns the value or throws an Error with a custom message.
|
|
822
|
+
* Corresponds to Rust `expect`.
|
|
823
823
|
*/
|
|
824
824
|
function expectResult(result, message) {
|
|
825
825
|
if (result.isOk()) return result.value;
|
|
@@ -829,8 +829,8 @@ function expectResult(result, message) {
|
|
|
829
829
|
//#endregion
|
|
830
830
|
//#region src/core/expectErr.ts
|
|
831
831
|
/**
|
|
832
|
-
*
|
|
833
|
-
*
|
|
832
|
+
* Returns the error or throws an Error with a custom message.
|
|
833
|
+
* Corresponds to Rust `expect_err`.
|
|
834
834
|
*/
|
|
835
835
|
function expectErr(result, message) {
|
|
836
836
|
if (result.isErr()) return result.error;
|
|
@@ -840,8 +840,8 @@ function expectErr(result, message) {
|
|
|
840
840
|
//#endregion
|
|
841
841
|
//#region src/core/and.ts
|
|
842
842
|
/**
|
|
843
|
-
*
|
|
844
|
-
*
|
|
843
|
+
* Combines two Results. Returns the second one only if the first is Ok.
|
|
844
|
+
* Corresponds to Rust `and`.
|
|
845
845
|
*/
|
|
846
846
|
function and(result, other) {
|
|
847
847
|
return result.isOk() ? other : result;
|
|
@@ -850,8 +850,8 @@ function and(result, other) {
|
|
|
850
850
|
//#endregion
|
|
851
851
|
//#region src/core/or.ts
|
|
852
852
|
/**
|
|
853
|
-
* Fallback
|
|
854
|
-
*
|
|
853
|
+
* Fallback to another Result if the first is Err.
|
|
854
|
+
* Corresponds to Rust `or`.
|
|
855
855
|
*/
|
|
856
856
|
function or(result, other) {
|
|
857
857
|
return result.isOk() ? result : other;
|
|
@@ -860,8 +860,8 @@ function or(result, other) {
|
|
|
860
860
|
//#endregion
|
|
861
861
|
//#region src/core/orElse.ts
|
|
862
862
|
/**
|
|
863
|
-
* Fallback
|
|
864
|
-
*
|
|
863
|
+
* Fallback with a function that returns a Result.
|
|
864
|
+
* Corresponds to Rust `or_else`.
|
|
865
865
|
*/
|
|
866
866
|
function orElse(result, fn) {
|
|
867
867
|
if (result.isOk()) return result;
|
|
@@ -872,8 +872,8 @@ function orElse(result, fn) {
|
|
|
872
872
|
//#endregion
|
|
873
873
|
//#region src/core/mapOr.ts
|
|
874
874
|
/**
|
|
875
|
-
*
|
|
876
|
-
*
|
|
875
|
+
* Transforms the value or returns a default value.
|
|
876
|
+
* Corresponds to Rust `map_or`.
|
|
877
877
|
*/
|
|
878
878
|
function mapOr(result, defaultValue, fn) {
|
|
879
879
|
if (result.isOk()) return fn(result.value);
|
|
@@ -883,8 +883,8 @@ function mapOr(result, defaultValue, fn) {
|
|
|
883
883
|
//#endregion
|
|
884
884
|
//#region src/core/mapOrElse.ts
|
|
885
885
|
/**
|
|
886
|
-
*
|
|
887
|
-
*
|
|
886
|
+
* Transforms the value or calculates a default value using a function.
|
|
887
|
+
* Corresponds to Rust `map_or_else`.
|
|
888
888
|
*/
|
|
889
889
|
function mapOrElse(result, defaultFn, fn) {
|
|
890
890
|
if (result.isOk()) return fn(result.value);
|
|
@@ -895,9 +895,9 @@ function mapOrElse(result, defaultFn, fn) {
|
|
|
895
895
|
//#endregion
|
|
896
896
|
//#region src/core/sequence.ts
|
|
897
897
|
/**
|
|
898
|
-
*
|
|
899
|
-
* Short-circuits
|
|
900
|
-
*
|
|
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<_>, _>>()`.
|
|
901
901
|
*/
|
|
902
902
|
function sequence(results) {
|
|
903
903
|
const values = [];
|
|
@@ -912,7 +912,7 @@ function sequence(results) {
|
|
|
912
912
|
return ok(values);
|
|
913
913
|
}
|
|
914
914
|
/**
|
|
915
|
-
* Alias
|
|
915
|
+
* Alias for `sequence`.
|
|
916
916
|
*/
|
|
917
917
|
function all(results) {
|
|
918
918
|
return sequence(results);
|
|
@@ -921,8 +921,8 @@ function all(results) {
|
|
|
921
921
|
//#endregion
|
|
922
922
|
//#region src/core/sequenceRecord.ts
|
|
923
923
|
/**
|
|
924
|
-
*
|
|
925
|
-
* Short-circuits
|
|
924
|
+
* Like `sequence`, but for Records/Objects.
|
|
925
|
+
* Short-circuits on the first Err.
|
|
926
926
|
*/
|
|
927
927
|
function sequenceRecord(record) {
|
|
928
928
|
const out = {};
|
|
@@ -942,11 +942,11 @@ function sequenceRecord(record) {
|
|
|
942
942
|
//#endregion
|
|
943
943
|
//#region src/core/collectFirstOkAsync.ts
|
|
944
944
|
/**
|
|
945
|
-
* Async
|
|
945
|
+
* Async version of collectFirstOk.
|
|
946
946
|
*
|
|
947
|
-
* -
|
|
948
|
-
* -
|
|
949
|
-
* -
|
|
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.
|
|
950
950
|
*/
|
|
951
951
|
async function collectFirstOkAsync(inputs) {
|
|
952
952
|
const errors = [];
|
|
@@ -967,15 +967,15 @@ async function collectFirstOkAsync(inputs) {
|
|
|
967
967
|
//#endregion
|
|
968
968
|
//#region src/core/collectFirstOkParallelAsync.ts
|
|
969
969
|
/**
|
|
970
|
-
* Parallel
|
|
970
|
+
* Parallel version of `collectFirstOkAsync`.
|
|
971
971
|
*
|
|
972
|
-
* -
|
|
973
|
-
* -
|
|
974
|
-
* -
|
|
975
|
-
* - Rejections
|
|
976
|
-
* -
|
|
977
|
-
*
|
|
978
|
-
* -
|
|
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.
|
|
979
979
|
*/
|
|
980
980
|
async function collectFirstOkParallelAsync(inputs) {
|
|
981
981
|
if (inputs.length === 0) return err([]);
|
|
@@ -1000,8 +1000,8 @@ async function collectFirstOkParallelAsync(inputs) {
|
|
|
1000
1000
|
//#endregion
|
|
1001
1001
|
//#region src/core/collectAllErrors.ts
|
|
1002
1002
|
/**
|
|
1003
|
-
*
|
|
1004
|
-
*
|
|
1003
|
+
* Combines a list of Results.
|
|
1004
|
+
* Return Ok(values) only if all are Ok, otherwise Err([errors]).
|
|
1005
1005
|
*/
|
|
1006
1006
|
function collectAllErrors(results) {
|
|
1007
1007
|
const values = [];
|
|
@@ -1023,7 +1023,7 @@ function collectAllErrors(results) {
|
|
|
1023
1023
|
//#endregion
|
|
1024
1024
|
//#region src/core/partition.ts
|
|
1025
1025
|
/**
|
|
1026
|
-
*
|
|
1026
|
+
* Partitions Results into Ok values and Err errors.
|
|
1027
1027
|
*/
|
|
1028
1028
|
function partition(results) {
|
|
1029
1029
|
const oks = [];
|
|
@@ -1045,9 +1045,9 @@ function partition(results) {
|
|
|
1045
1045
|
//#endregion
|
|
1046
1046
|
//#region src/core/flatten.ts
|
|
1047
1047
|
/**
|
|
1048
|
-
*
|
|
1048
|
+
* Flattens a nested Result.
|
|
1049
1049
|
* Result<Result<T, E>, E> → Result<T, E>
|
|
1050
|
-
*
|
|
1050
|
+
* Corresponds to Rust `flatten`.
|
|
1051
1051
|
*/
|
|
1052
1052
|
function flatten(result) {
|
|
1053
1053
|
if (result.isOk()) return result.value;
|
|
@@ -1057,7 +1057,7 @@ function flatten(result) {
|
|
|
1057
1057
|
//#endregion
|
|
1058
1058
|
//#region src/core/toPromise.ts
|
|
1059
1059
|
/**
|
|
1060
|
-
*
|
|
1060
|
+
* Converts a Result to a Promise.
|
|
1061
1061
|
* Ok → resolve(value), Err → reject(error)
|
|
1062
1062
|
*/
|
|
1063
1063
|
function toPromise(result) {
|
|
@@ -1069,7 +1069,7 @@ function toPromise(result) {
|
|
|
1069
1069
|
//#endregion
|
|
1070
1070
|
//#region src/core/toNullable.ts
|
|
1071
1071
|
/**
|
|
1072
|
-
*
|
|
1072
|
+
* Converts a Result to `T | null`.
|
|
1073
1073
|
* Ok → value, Err → null
|
|
1074
1074
|
*/
|
|
1075
1075
|
function toNullable(result) {
|
|
@@ -1080,8 +1080,8 @@ function toNullable(result) {
|
|
|
1080
1080
|
//#endregion
|
|
1081
1081
|
//#region src/core/isOk.ts
|
|
1082
1082
|
/**
|
|
1083
|
-
*
|
|
1084
|
-
* Pure function
|
|
1083
|
+
* Checks if a Result is Ok.
|
|
1084
|
+
* Pure function alternative to the instance method.
|
|
1085
1085
|
*/
|
|
1086
1086
|
function isOk(result) {
|
|
1087
1087
|
return result.isOk();
|
|
@@ -1090,8 +1090,8 @@ function isOk(result) {
|
|
|
1090
1090
|
//#endregion
|
|
1091
1091
|
//#region src/core/isErr.ts
|
|
1092
1092
|
/**
|
|
1093
|
-
*
|
|
1094
|
-
* Pure function
|
|
1093
|
+
* Checks if a Result is Err.
|
|
1094
|
+
* Pure function alternative to the instance method.
|
|
1095
1095
|
*/
|
|
1096
1096
|
function isErr(result) {
|
|
1097
1097
|
return result.isErr();
|
|
@@ -1100,8 +1100,8 @@ function isErr(result) {
|
|
|
1100
1100
|
//#endregion
|
|
1101
1101
|
//#region src/core/contains.ts
|
|
1102
1102
|
/**
|
|
1103
|
-
*
|
|
1104
|
-
*
|
|
1103
|
+
* Checks if the Result contains a specific value.
|
|
1104
|
+
* Corresponds to Rust `contains`.
|
|
1105
1105
|
*/
|
|
1106
1106
|
function contains(result, value) {
|
|
1107
1107
|
if (!result.isOk()) return false;
|
|
@@ -1111,8 +1111,8 @@ function contains(result, value) {
|
|
|
1111
1111
|
//#endregion
|
|
1112
1112
|
//#region src/core/containsErr.ts
|
|
1113
1113
|
/**
|
|
1114
|
-
*
|
|
1115
|
-
*
|
|
1114
|
+
* Checks if the Result contains a specific error.
|
|
1115
|
+
* Analogous to `contains` for the Err case.
|
|
1116
1116
|
*/
|
|
1117
1117
|
function containsErr(result, error) {
|
|
1118
1118
|
if (!result.isErr()) return false;
|