@shirudo/result 0.0.4 → 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/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.cjs
CHANGED
|
@@ -98,11 +98,11 @@ var ExpectErrError = class extends ResultError {
|
|
|
98
98
|
//#endregion
|
|
99
99
|
//#region src/core/matcher.ts
|
|
100
100
|
/**
|
|
101
|
-
* Matcher
|
|
101
|
+
* Matcher for Err values (returns an arbitrary return type, e.g. string messages).
|
|
102
102
|
*
|
|
103
|
-
* - `.when(Ctor, handler)`
|
|
104
|
-
* - `.whenGuard(guard, handler)`
|
|
105
|
-
* - `.run()`
|
|
103
|
+
* - `.when(Ctor, handler)` matches via `instanceof`
|
|
104
|
+
* - `.whenGuard(guard, handler)` matches via Type-Guard
|
|
105
|
+
* - `.run()` is only allowed if all error cases have been handled (`E` has been reduced to `never`)
|
|
106
106
|
*/
|
|
107
107
|
var ErrorMatchBuilder = class ErrorMatchBuilder {
|
|
108
108
|
#error;
|
|
@@ -138,11 +138,11 @@ function isResult$1(value) {
|
|
|
138
138
|
return typeof value === "object" && value !== null && "isOk" in value && typeof value.isOk === "function" && "isErr" in value && typeof value.isErr === "function";
|
|
139
139
|
}
|
|
140
140
|
/**
|
|
141
|
-
* Matcher
|
|
141
|
+
* Matcher for `Result` Errors, which returns a `Result` again.
|
|
142
142
|
*
|
|
143
|
-
*
|
|
144
|
-
* -
|
|
145
|
-
* -
|
|
143
|
+
* Handlers may:
|
|
144
|
+
* - return a `Result` (is returned directly)
|
|
145
|
+
* - return an Error value (is automatically wrapped into `Err(error)`)
|
|
146
146
|
*/
|
|
147
147
|
var ErrMatchBuilder = class ErrMatchBuilder {
|
|
148
148
|
#makeErr;
|
|
@@ -192,8 +192,8 @@ var ErrMatchBuilder = class ErrMatchBuilder {
|
|
|
192
192
|
//#endregion
|
|
193
193
|
//#region src/core/map.ts
|
|
194
194
|
/**
|
|
195
|
-
*
|
|
196
|
-
*
|
|
195
|
+
* Transforms the value (Ok case).
|
|
196
|
+
* Corresponds to Rust `map`.
|
|
197
197
|
*/
|
|
198
198
|
function map(project) {
|
|
199
199
|
return (source) => {
|
|
@@ -205,8 +205,8 @@ function map(project) {
|
|
|
205
205
|
//#endregion
|
|
206
206
|
//#region src/core/mapErr.ts
|
|
207
207
|
/**
|
|
208
|
-
*
|
|
209
|
-
*
|
|
208
|
+
* Transforms the error (Err case).
|
|
209
|
+
* Corresponds to Rust `map_err`.
|
|
210
210
|
*/
|
|
211
211
|
function mapErr(project) {
|
|
212
212
|
return (source) => {
|
|
@@ -218,8 +218,8 @@ function mapErr(project) {
|
|
|
218
218
|
//#endregion
|
|
219
219
|
//#region src/core/mapBoth.ts
|
|
220
220
|
/**
|
|
221
|
-
*
|
|
222
|
-
*
|
|
221
|
+
* Transforms both the Ok value and the Err error.
|
|
222
|
+
* Corresponds to FP `bimap` / `mapBoth`.
|
|
223
223
|
*/
|
|
224
224
|
function mapBoth(mapOk, mapErr$1) {
|
|
225
225
|
return (source) => {
|
|
@@ -229,15 +229,15 @@ function mapBoth(mapOk, mapErr$1) {
|
|
|
229
229
|
};
|
|
230
230
|
}
|
|
231
231
|
/**
|
|
232
|
-
* Alias
|
|
232
|
+
* Alias for `mapBoth`.
|
|
233
233
|
*/
|
|
234
234
|
const bimap = mapBoth;
|
|
235
235
|
|
|
236
236
|
//#endregion
|
|
237
237
|
//#region src/core/flatMap.ts
|
|
238
238
|
/**
|
|
239
|
-
*
|
|
240
|
-
*
|
|
239
|
+
* Chains another operation that returns a Result.
|
|
240
|
+
* Corresponds to Rust `and_then` or JS `flatMap`.
|
|
241
241
|
*/
|
|
242
242
|
function flatMap(project) {
|
|
243
243
|
return (source) => {
|
|
@@ -281,8 +281,8 @@ function combine(...args) {
|
|
|
281
281
|
//#endregion
|
|
282
282
|
//#region src/core/tap.ts
|
|
283
283
|
/**
|
|
284
|
-
*
|
|
285
|
-
*
|
|
284
|
+
* Executes a side effect (logging, debugging) without changing the Result.
|
|
285
|
+
* Corresponds to Rust `inspect` / `inspect_err`.
|
|
286
286
|
*/
|
|
287
287
|
function tap(observer) {
|
|
288
288
|
return (source) => {
|
|
@@ -295,8 +295,8 @@ function tap(observer) {
|
|
|
295
295
|
//#endregion
|
|
296
296
|
//#region src/core/filter.ts
|
|
297
297
|
/**
|
|
298
|
-
*
|
|
299
|
-
*
|
|
298
|
+
* Checks a condition. If false, the Result becomes Err.
|
|
299
|
+
* Corresponds to Rust `filter` (partially).
|
|
300
300
|
*/
|
|
301
301
|
function filter(predicate, errorFn) {
|
|
302
302
|
return (source) => {
|
|
@@ -311,8 +311,8 @@ function filter(predicate, errorFn) {
|
|
|
311
311
|
//#endregion
|
|
312
312
|
//#region src/core/match.ts
|
|
313
313
|
/**
|
|
314
|
-
*
|
|
315
|
-
*
|
|
314
|
+
* Resolves the Result. The end of the pipe.
|
|
315
|
+
* Corresponds to Rust `match`.
|
|
316
316
|
*/
|
|
317
317
|
function match(handlers) {
|
|
318
318
|
return (source) => {
|
|
@@ -325,8 +325,8 @@ function match(handlers) {
|
|
|
325
325
|
//#endregion
|
|
326
326
|
//#region src/core/recover.ts
|
|
327
327
|
/**
|
|
328
|
-
* Recover:
|
|
329
|
-
*
|
|
328
|
+
* Recover: converts Err to Ok(defaultValue).
|
|
329
|
+
* Result is guaranteed to be Ok → Error type becomes `never`.
|
|
330
330
|
*/
|
|
331
331
|
function recover(defaultValue) {
|
|
332
332
|
return (source) => {
|
|
@@ -336,7 +336,7 @@ function recover(defaultValue) {
|
|
|
336
336
|
};
|
|
337
337
|
}
|
|
338
338
|
/**
|
|
339
|
-
*
|
|
339
|
+
* Like `recover`, but calculates the default value based on the error.
|
|
340
340
|
*/
|
|
341
341
|
function recoverWith(fn) {
|
|
342
342
|
return (source) => {
|
|
@@ -349,7 +349,7 @@ function recoverWith(fn) {
|
|
|
349
349
|
//#endregion
|
|
350
350
|
//#region src/core/swap.ts
|
|
351
351
|
/**
|
|
352
|
-
*
|
|
352
|
+
* Swaps Ok and Err.
|
|
353
353
|
* Result<T, E> → Result<E, T>
|
|
354
354
|
*/
|
|
355
355
|
function swap(result) {
|
|
@@ -361,9 +361,9 @@ function swap(result) {
|
|
|
361
361
|
//#endregion
|
|
362
362
|
//#region src/core/tryCatch.ts
|
|
363
363
|
/**
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
364
|
+
* Executes a function and catches exceptions.
|
|
365
|
+
* Converts exceptions into Result<E>.
|
|
366
|
+
* Corresponds to Rust `Result::from` for fallible operations.
|
|
367
367
|
*/
|
|
368
368
|
function tryCatch(fn, errorMapper) {
|
|
369
369
|
return (source) => {
|
|
@@ -379,7 +379,7 @@ function tryCatch(fn, errorMapper) {
|
|
|
379
379
|
//#endregion
|
|
380
380
|
//#region src/core/tryMap.ts
|
|
381
381
|
/**
|
|
382
|
-
*
|
|
382
|
+
* Like `map`, but catches exceptions and converts them to Err.
|
|
383
383
|
*/
|
|
384
384
|
function tryMap(project, errorMapper) {
|
|
385
385
|
return (source) => {
|
|
@@ -416,7 +416,7 @@ function collectFirstOk(results) {
|
|
|
416
416
|
//#endregion
|
|
417
417
|
//#region src/core/mapAsync.ts
|
|
418
418
|
/**
|
|
419
|
-
* Async
|
|
419
|
+
* Async version of map.
|
|
420
420
|
*/
|
|
421
421
|
function mapAsync(project) {
|
|
422
422
|
return async (source) => {
|
|
@@ -428,7 +428,7 @@ function mapAsync(project) {
|
|
|
428
428
|
//#endregion
|
|
429
429
|
//#region src/core/mapErrAsync.ts
|
|
430
430
|
/**
|
|
431
|
-
* Async
|
|
431
|
+
* Async version of mapErr.
|
|
432
432
|
*/
|
|
433
433
|
function mapErrAsync(project) {
|
|
434
434
|
return async (source) => {
|
|
@@ -440,7 +440,7 @@ function mapErrAsync(project) {
|
|
|
440
440
|
//#endregion
|
|
441
441
|
//#region src/core/flatMapAsync.ts
|
|
442
442
|
/**
|
|
443
|
-
* Async
|
|
443
|
+
* Async version of flatMap.
|
|
444
444
|
*/
|
|
445
445
|
function flatMapAsync(project) {
|
|
446
446
|
return async (source) => {
|
|
@@ -452,7 +452,7 @@ function flatMapAsync(project) {
|
|
|
452
452
|
//#endregion
|
|
453
453
|
//#region src/core/tapAsync.ts
|
|
454
454
|
/**
|
|
455
|
-
* Async
|
|
455
|
+
* Async version of tap.
|
|
456
456
|
*/
|
|
457
457
|
function tapAsync(observer) {
|
|
458
458
|
return async (source) => {
|
|
@@ -465,7 +465,7 @@ function tapAsync(observer) {
|
|
|
465
465
|
//#endregion
|
|
466
466
|
//#region src/core/filterAsync.ts
|
|
467
467
|
/**
|
|
468
|
-
* Async
|
|
468
|
+
* Async version of filter.
|
|
469
469
|
*/
|
|
470
470
|
function filterAsync(predicate, errorFn) {
|
|
471
471
|
return async (source) => {
|
|
@@ -480,7 +480,7 @@ function filterAsync(predicate, errorFn) {
|
|
|
480
480
|
//#endregion
|
|
481
481
|
//#region src/core/matchAsync.ts
|
|
482
482
|
/**
|
|
483
|
-
* Async
|
|
483
|
+
* Async version of match.
|
|
484
484
|
*/
|
|
485
485
|
function matchAsync(handlers) {
|
|
486
486
|
return async (source) => {
|
|
@@ -493,7 +493,7 @@ function matchAsync(handlers) {
|
|
|
493
493
|
//#endregion
|
|
494
494
|
//#region src/core/tryCatchAsync.ts
|
|
495
495
|
/**
|
|
496
|
-
* Async
|
|
496
|
+
* Async version of tryCatch.
|
|
497
497
|
*/
|
|
498
498
|
function tryCatchAsync(fn, errorMapper) {
|
|
499
499
|
return async (source) => {
|
|
@@ -509,7 +509,7 @@ function tryCatchAsync(fn, errorMapper) {
|
|
|
509
509
|
//#endregion
|
|
510
510
|
//#region src/core/tryMapAsync.ts
|
|
511
511
|
/**
|
|
512
|
-
* Async
|
|
512
|
+
* Async version of tryMap.
|
|
513
513
|
*/
|
|
514
514
|
function tryMapAsync(project, errorMapper) {
|
|
515
515
|
return async (source) => {
|
|
@@ -554,27 +554,27 @@ var ResultBase = class extends Pipeable {
|
|
|
554
554
|
return yield this;
|
|
555
555
|
}
|
|
556
556
|
/**
|
|
557
|
-
*
|
|
557
|
+
* Matches on the Err value via `.when(...)` chain.
|
|
558
558
|
*
|
|
559
|
-
*
|
|
560
|
-
*
|
|
559
|
+
* Note: for type safety reasons, `.match()` can only be called on a Result already narrowed to `Err`,
|
|
560
|
+
* e.g. inside `if (result.isErr()) { ... }`.
|
|
561
561
|
*/
|
|
562
562
|
match() {
|
|
563
563
|
if (this._tag === "Err") return new ErrorMatchBuilder(this.error);
|
|
564
564
|
throw new MatchOnOkError();
|
|
565
565
|
}
|
|
566
566
|
/**
|
|
567
|
-
*
|
|
568
|
-
* -
|
|
569
|
-
* -
|
|
567
|
+
* Matches on the Err value, but normalizes every branch to a `Result`:
|
|
568
|
+
* - Handlers may return a `Result` (is returned directly)
|
|
569
|
+
* - or an Error value (is wrapped into `Err(error)`)
|
|
570
570
|
*/
|
|
571
571
|
matchErr() {
|
|
572
572
|
const makeErr = (error) => err(error);
|
|
573
573
|
return ErrMatchBuilder.fromResult(this, makeErr);
|
|
574
574
|
}
|
|
575
575
|
/**
|
|
576
|
-
*
|
|
577
|
-
*
|
|
576
|
+
* Serializes the Result into a simple object format.
|
|
577
|
+
* Preserves the original types.
|
|
578
578
|
*/
|
|
579
579
|
serialize() {
|
|
580
580
|
if (this._tag === "Ok") return {
|
|
@@ -587,8 +587,8 @@ var ResultBase = class extends Pipeable {
|
|
|
587
587
|
};
|
|
588
588
|
}
|
|
589
589
|
/**
|
|
590
|
-
*
|
|
591
|
-
*
|
|
590
|
+
* Serializes the Result into a user-friendly format.
|
|
591
|
+
* Converts Errors to readable strings.
|
|
592
592
|
*/
|
|
593
593
|
toUserFriendly() {
|
|
594
594
|
if (this._tag === "Ok") return {
|
|
@@ -750,8 +750,8 @@ function foldAsync(handlers) {
|
|
|
750
750
|
//#endregion
|
|
751
751
|
//#region src/core/unwrap.ts
|
|
752
752
|
/**
|
|
753
|
-
*
|
|
754
|
-
*
|
|
753
|
+
* Returns the value or throws an Error.
|
|
754
|
+
* Corresponds to Rust `unwrap`.
|
|
755
755
|
*/
|
|
756
756
|
function unwrap(result) {
|
|
757
757
|
if (result.isOk()) return result.value;
|
|
@@ -762,8 +762,8 @@ function unwrap(result) {
|
|
|
762
762
|
//#endregion
|
|
763
763
|
//#region src/core/unwrapOr.ts
|
|
764
764
|
/**
|
|
765
|
-
*
|
|
766
|
-
* Pure function
|
|
765
|
+
* Returns the value or a default value.
|
|
766
|
+
* Pure function alternative to the instance method.
|
|
767
767
|
*/
|
|
768
768
|
function unwrapOr(result, defaultValue) {
|
|
769
769
|
if (result.isOk()) return result.value;
|
|
@@ -773,8 +773,8 @@ function unwrapOr(result, defaultValue) {
|
|
|
773
773
|
//#endregion
|
|
774
774
|
//#region src/core/unwrapOrElse.ts
|
|
775
775
|
/**
|
|
776
|
-
*
|
|
777
|
-
*
|
|
776
|
+
* Returns the value or calculates a default value using a function.
|
|
777
|
+
* Corresponds to Rust `unwrap_or_else`.
|
|
778
778
|
*/
|
|
779
779
|
function unwrapOrElse(result, fn) {
|
|
780
780
|
if (result.isOk()) return result.value;
|
|
@@ -785,8 +785,8 @@ function unwrapOrElse(result, fn) {
|
|
|
785
785
|
//#endregion
|
|
786
786
|
//#region src/core/unwrapOrDefault.ts
|
|
787
787
|
/**
|
|
788
|
-
* Alias
|
|
789
|
-
*
|
|
788
|
+
* Alias for `unwrapOr`.
|
|
789
|
+
* Corresponds to Rust `unwrap_or_default` (with explicit default value).
|
|
790
790
|
*/
|
|
791
791
|
function unwrapOrDefault(result, defaultValue) {
|
|
792
792
|
return unwrapOr(result, defaultValue);
|
|
@@ -795,8 +795,8 @@ function unwrapOrDefault(result, defaultValue) {
|
|
|
795
795
|
//#endregion
|
|
796
796
|
//#region src/core/unwrapOrThrow.ts
|
|
797
797
|
/**
|
|
798
|
-
*
|
|
799
|
-
*
|
|
798
|
+
* Returns the value or throws the original Err value (not wrapped).
|
|
799
|
+
* Useful to preserve `Error` instances including stack traces.
|
|
800
800
|
*/
|
|
801
801
|
function unwrapOrThrow(result) {
|
|
802
802
|
if (result.isOk()) return result.value;
|
|
@@ -807,8 +807,8 @@ function unwrapOrThrow(result) {
|
|
|
807
807
|
//#endregion
|
|
808
808
|
//#region src/core/unwrapErr.ts
|
|
809
809
|
/**
|
|
810
|
-
*
|
|
811
|
-
*
|
|
810
|
+
* Returns the error or throws an Error.
|
|
811
|
+
* Corresponds to Rust `unwrap_err`.
|
|
812
812
|
*/
|
|
813
813
|
function unwrapErr(result) {
|
|
814
814
|
if (result.isErr()) return result.error;
|
|
@@ -819,8 +819,8 @@ function unwrapErr(result) {
|
|
|
819
819
|
//#endregion
|
|
820
820
|
//#region src/core/expectResult.ts
|
|
821
821
|
/**
|
|
822
|
-
*
|
|
823
|
-
*
|
|
822
|
+
* Returns the value or throws an Error with a custom message.
|
|
823
|
+
* Corresponds to Rust `expect`.
|
|
824
824
|
*/
|
|
825
825
|
function expectResult(result, message) {
|
|
826
826
|
if (result.isOk()) return result.value;
|
|
@@ -830,8 +830,8 @@ function expectResult(result, message) {
|
|
|
830
830
|
//#endregion
|
|
831
831
|
//#region src/core/expectErr.ts
|
|
832
832
|
/**
|
|
833
|
-
*
|
|
834
|
-
*
|
|
833
|
+
* Returns the error or throws an Error with a custom message.
|
|
834
|
+
* Corresponds to Rust `expect_err`.
|
|
835
835
|
*/
|
|
836
836
|
function expectErr(result, message) {
|
|
837
837
|
if (result.isErr()) return result.error;
|
|
@@ -841,8 +841,8 @@ function expectErr(result, message) {
|
|
|
841
841
|
//#endregion
|
|
842
842
|
//#region src/core/and.ts
|
|
843
843
|
/**
|
|
844
|
-
*
|
|
845
|
-
*
|
|
844
|
+
* Combines two Results. Returns the second one only if the first is Ok.
|
|
845
|
+
* Corresponds to Rust `and`.
|
|
846
846
|
*/
|
|
847
847
|
function and(result, other) {
|
|
848
848
|
return result.isOk() ? other : result;
|
|
@@ -851,8 +851,8 @@ function and(result, other) {
|
|
|
851
851
|
//#endregion
|
|
852
852
|
//#region src/core/or.ts
|
|
853
853
|
/**
|
|
854
|
-
* Fallback
|
|
855
|
-
*
|
|
854
|
+
* Fallback to another Result if the first is Err.
|
|
855
|
+
* Corresponds to Rust `or`.
|
|
856
856
|
*/
|
|
857
857
|
function or(result, other) {
|
|
858
858
|
return result.isOk() ? result : other;
|
|
@@ -861,8 +861,8 @@ function or(result, other) {
|
|
|
861
861
|
//#endregion
|
|
862
862
|
//#region src/core/orElse.ts
|
|
863
863
|
/**
|
|
864
|
-
* Fallback
|
|
865
|
-
*
|
|
864
|
+
* Fallback with a function that returns a Result.
|
|
865
|
+
* Corresponds to Rust `or_else`.
|
|
866
866
|
*/
|
|
867
867
|
function orElse(result, fn) {
|
|
868
868
|
if (result.isOk()) return result;
|
|
@@ -873,8 +873,8 @@ function orElse(result, fn) {
|
|
|
873
873
|
//#endregion
|
|
874
874
|
//#region src/core/mapOr.ts
|
|
875
875
|
/**
|
|
876
|
-
*
|
|
877
|
-
*
|
|
876
|
+
* Transforms the value or returns a default value.
|
|
877
|
+
* Corresponds to Rust `map_or`.
|
|
878
878
|
*/
|
|
879
879
|
function mapOr(result, defaultValue, fn) {
|
|
880
880
|
if (result.isOk()) return fn(result.value);
|
|
@@ -884,8 +884,8 @@ function mapOr(result, defaultValue, fn) {
|
|
|
884
884
|
//#endregion
|
|
885
885
|
//#region src/core/mapOrElse.ts
|
|
886
886
|
/**
|
|
887
|
-
*
|
|
888
|
-
*
|
|
887
|
+
* Transforms the value or calculates a default value using a function.
|
|
888
|
+
* Corresponds to Rust `map_or_else`.
|
|
889
889
|
*/
|
|
890
890
|
function mapOrElse(result, defaultFn, fn) {
|
|
891
891
|
if (result.isOk()) return fn(result.value);
|
|
@@ -896,9 +896,9 @@ function mapOrElse(result, defaultFn, fn) {
|
|
|
896
896
|
//#endregion
|
|
897
897
|
//#region src/core/sequence.ts
|
|
898
898
|
/**
|
|
899
|
-
*
|
|
900
|
-
* Short-circuits
|
|
901
|
-
*
|
|
899
|
+
* Combines a list of Results into a single Result of a list.
|
|
900
|
+
* Short-circuits on the first Err.
|
|
901
|
+
* Analogous to Rust `collect::<Result<Vec<_>, _>>()`.
|
|
902
902
|
*/
|
|
903
903
|
function sequence(results) {
|
|
904
904
|
const values = [];
|
|
@@ -913,7 +913,7 @@ function sequence(results) {
|
|
|
913
913
|
return ok(values);
|
|
914
914
|
}
|
|
915
915
|
/**
|
|
916
|
-
* Alias
|
|
916
|
+
* Alias for `sequence`.
|
|
917
917
|
*/
|
|
918
918
|
function all(results) {
|
|
919
919
|
return sequence(results);
|
|
@@ -922,8 +922,8 @@ function all(results) {
|
|
|
922
922
|
//#endregion
|
|
923
923
|
//#region src/core/sequenceRecord.ts
|
|
924
924
|
/**
|
|
925
|
-
*
|
|
926
|
-
* Short-circuits
|
|
925
|
+
* Like `sequence`, but for Records/Objects.
|
|
926
|
+
* Short-circuits on the first Err.
|
|
927
927
|
*/
|
|
928
928
|
function sequenceRecord(record) {
|
|
929
929
|
const out = {};
|
|
@@ -943,11 +943,11 @@ function sequenceRecord(record) {
|
|
|
943
943
|
//#endregion
|
|
944
944
|
//#region src/core/collectFirstOkAsync.ts
|
|
945
945
|
/**
|
|
946
|
-
* Async
|
|
946
|
+
* Async version of collectFirstOk.
|
|
947
947
|
*
|
|
948
|
-
* -
|
|
949
|
-
* -
|
|
950
|
-
* -
|
|
948
|
+
* - Takes either already started Promises or "Thunks" (`() => Awaitable<Result<...>>`).
|
|
949
|
+
* - Processes inputs strictly sequentially (like `for ... of` + `await`).
|
|
950
|
+
* - Returns the first `Ok` and collects all errors if no `Ok` is found.
|
|
951
951
|
*/
|
|
952
952
|
async function collectFirstOkAsync(inputs) {
|
|
953
953
|
const errors = [];
|
|
@@ -968,15 +968,15 @@ async function collectFirstOkAsync(inputs) {
|
|
|
968
968
|
//#endregion
|
|
969
969
|
//#region src/core/collectFirstOkParallelAsync.ts
|
|
970
970
|
/**
|
|
971
|
-
* Parallel
|
|
971
|
+
* Parallel version of `collectFirstOkAsync`.
|
|
972
972
|
*
|
|
973
|
-
* -
|
|
974
|
-
* -
|
|
975
|
-
* -
|
|
976
|
-
* - Rejections
|
|
977
|
-
* -
|
|
978
|
-
*
|
|
979
|
-
* -
|
|
973
|
+
* - Starts all inputs immediately (Promises or Thunks).
|
|
974
|
+
* - Returns the first `Ok` as soon as it is available.
|
|
975
|
+
* - If no `Ok` is found, returns an `Err` with all error values (in input order).
|
|
976
|
+
* - Rejections are treated as `ErrValue` (`caught as ErrValue`).
|
|
977
|
+
* - If multiple inputs provide an `Ok`, the one that completes first wins.
|
|
978
|
+
* In case of simultaneous completion, the first observed result wins.
|
|
979
|
+
* - If no `Ok` arrives and at least one input never settles, the Promise remains pending.
|
|
980
980
|
*/
|
|
981
981
|
async function collectFirstOkParallelAsync(inputs) {
|
|
982
982
|
if (inputs.length === 0) return err([]);
|
|
@@ -1001,8 +1001,8 @@ async function collectFirstOkParallelAsync(inputs) {
|
|
|
1001
1001
|
//#endregion
|
|
1002
1002
|
//#region src/core/collectAllErrors.ts
|
|
1003
1003
|
/**
|
|
1004
|
-
*
|
|
1005
|
-
*
|
|
1004
|
+
* Combines a list of Results.
|
|
1005
|
+
* Return Ok(values) only if all are Ok, otherwise Err([errors]).
|
|
1006
1006
|
*/
|
|
1007
1007
|
function collectAllErrors(results) {
|
|
1008
1008
|
const values = [];
|
|
@@ -1024,7 +1024,7 @@ function collectAllErrors(results) {
|
|
|
1024
1024
|
//#endregion
|
|
1025
1025
|
//#region src/core/partition.ts
|
|
1026
1026
|
/**
|
|
1027
|
-
*
|
|
1027
|
+
* Partitions Results into Ok values and Err errors.
|
|
1028
1028
|
*/
|
|
1029
1029
|
function partition(results) {
|
|
1030
1030
|
const oks = [];
|
|
@@ -1046,9 +1046,9 @@ function partition(results) {
|
|
|
1046
1046
|
//#endregion
|
|
1047
1047
|
//#region src/core/flatten.ts
|
|
1048
1048
|
/**
|
|
1049
|
-
*
|
|
1049
|
+
* Flattens a nested Result.
|
|
1050
1050
|
* Result<Result<T, E>, E> → Result<T, E>
|
|
1051
|
-
*
|
|
1051
|
+
* Corresponds to Rust `flatten`.
|
|
1052
1052
|
*/
|
|
1053
1053
|
function flatten(result) {
|
|
1054
1054
|
if (result.isOk()) return result.value;
|
|
@@ -1058,7 +1058,7 @@ function flatten(result) {
|
|
|
1058
1058
|
//#endregion
|
|
1059
1059
|
//#region src/core/toPromise.ts
|
|
1060
1060
|
/**
|
|
1061
|
-
*
|
|
1061
|
+
* Converts a Result to a Promise.
|
|
1062
1062
|
* Ok → resolve(value), Err → reject(error)
|
|
1063
1063
|
*/
|
|
1064
1064
|
function toPromise(result) {
|
|
@@ -1070,7 +1070,7 @@ function toPromise(result) {
|
|
|
1070
1070
|
//#endregion
|
|
1071
1071
|
//#region src/core/toNullable.ts
|
|
1072
1072
|
/**
|
|
1073
|
-
*
|
|
1073
|
+
* Converts a Result to `T | null`.
|
|
1074
1074
|
* Ok → value, Err → null
|
|
1075
1075
|
*/
|
|
1076
1076
|
function toNullable(result) {
|
|
@@ -1081,8 +1081,8 @@ function toNullable(result) {
|
|
|
1081
1081
|
//#endregion
|
|
1082
1082
|
//#region src/core/isOk.ts
|
|
1083
1083
|
/**
|
|
1084
|
-
*
|
|
1085
|
-
* Pure function
|
|
1084
|
+
* Checks if a Result is Ok.
|
|
1085
|
+
* Pure function alternative to the instance method.
|
|
1086
1086
|
*/
|
|
1087
1087
|
function isOk(result) {
|
|
1088
1088
|
return result.isOk();
|
|
@@ -1091,8 +1091,8 @@ function isOk(result) {
|
|
|
1091
1091
|
//#endregion
|
|
1092
1092
|
//#region src/core/isErr.ts
|
|
1093
1093
|
/**
|
|
1094
|
-
*
|
|
1095
|
-
* Pure function
|
|
1094
|
+
* Checks if a Result is Err.
|
|
1095
|
+
* Pure function alternative to the instance method.
|
|
1096
1096
|
*/
|
|
1097
1097
|
function isErr(result) {
|
|
1098
1098
|
return result.isErr();
|
|
@@ -1101,8 +1101,8 @@ function isErr(result) {
|
|
|
1101
1101
|
//#endregion
|
|
1102
1102
|
//#region src/core/contains.ts
|
|
1103
1103
|
/**
|
|
1104
|
-
*
|
|
1105
|
-
*
|
|
1104
|
+
* Checks if the Result contains a specific value.
|
|
1105
|
+
* Corresponds to Rust `contains`.
|
|
1106
1106
|
*/
|
|
1107
1107
|
function contains(result, value) {
|
|
1108
1108
|
if (!result.isOk()) return false;
|
|
@@ -1112,8 +1112,8 @@ function contains(result, value) {
|
|
|
1112
1112
|
//#endregion
|
|
1113
1113
|
//#region src/core/containsErr.ts
|
|
1114
1114
|
/**
|
|
1115
|
-
*
|
|
1116
|
-
*
|
|
1115
|
+
* Checks if the Result contains a specific error.
|
|
1116
|
+
* Analogous to `contains` for the Err case.
|
|
1117
1117
|
*/
|
|
1118
1118
|
function containsErr(result, error) {
|
|
1119
1119
|
if (!result.isErr()) return false;
|