ts-data-forge 1.5.1 → 1.5.2
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/array/array-utils.d.mts +133 -286
- package/dist/array/array-utils.d.mts.map +1 -1
- package/dist/array/array-utils.mjs +191 -1898
- package/dist/array/array-utils.mjs.map +1 -1
- package/dist/array/tuple-utils.d.mts +9 -23
- package/dist/array/tuple-utils.d.mts.map +1 -1
- package/dist/array/tuple-utils.mjs +10 -56
- package/dist/array/tuple-utils.mjs.map +1 -1
- package/dist/collections/imap-mapped.mjs.map +1 -1
- package/dist/collections/imap.mjs.map +1 -1
- package/dist/collections/iset-mapped.mjs.map +1 -1
- package/dist/collections/iset.mjs.map +1 -1
- package/dist/collections/queue.mjs.map +1 -1
- package/dist/collections/stack.mjs.map +1 -1
- package/dist/functional/match.d.mts +2 -33
- package/dist/functional/match.d.mts.map +1 -1
- package/dist/functional/match.mjs +2 -119
- package/dist/functional/match.mjs.map +1 -1
- package/dist/functional/optional.d.mts +29 -51
- package/dist/functional/optional.d.mts.map +1 -1
- package/dist/functional/optional.mjs +40 -171
- package/dist/functional/optional.mjs.map +1 -1
- package/dist/functional/pipe.d.mts +2 -15
- package/dist/functional/pipe.d.mts.map +1 -1
- package/dist/functional/pipe.mjs +2 -110
- package/dist/functional/pipe.mjs.map +1 -1
- package/dist/functional/result.d.mts +18 -45
- package/dist/functional/result.d.mts.map +1 -1
- package/dist/functional/result.mjs +40 -196
- package/dist/functional/result.mjs.map +1 -1
- package/dist/iterator/range.d.mts +2 -6
- package/dist/iterator/range.d.mts.map +1 -1
- package/dist/iterator/range.mjs +2 -93
- package/dist/iterator/range.mjs.map +1 -1
- package/dist/json/json.mjs.map +1 -1
- package/dist/number/num.d.mts +2 -5
- package/dist/number/num.d.mts.map +1 -1
- package/dist/number/num.mjs +4 -22
- package/dist/number/num.mjs.map +1 -1
- package/dist/number/refined-number-utils.mjs.map +1 -1
- package/dist/object/object.d.mts +4 -10
- package/dist/object/object.d.mts.map +1 -1
- package/dist/object/object.mjs +8 -140
- package/dist/object/object.mjs.map +1 -1
- package/dist/others/map-nullable.d.mts +2 -6
- package/dist/others/map-nullable.d.mts.map +1 -1
- package/dist/others/map-nullable.mjs +2 -146
- package/dist/others/map-nullable.mjs.map +1 -1
- package/dist/others/memoize-function.mjs.map +1 -1
- package/dist/others/unknown-to-string.mjs.map +1 -1
- package/package.json +11 -11
- package/src/array/array-utils.mts +703 -809
- package/src/array/tuple-utils.mts +20 -41
- package/src/functional/match.mts +18 -44
- package/src/functional/optional.mts +88 -102
- package/src/functional/pipe.mts +25 -20
- package/src/functional/result.mts +114 -124
- package/src/iterator/range.mts +14 -17
- package/src/number/num.mts +15 -11
- package/src/object/object.mts +30 -45
- package/src/others/map-nullable.mts +13 -15
package/dist/functional/pipe.mjs
CHANGED
|
@@ -1,114 +1,6 @@
|
|
|
1
1
|
import { Optional } from './optional.mjs';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
* Creates a new pipe object that allows for chaining operations on a value.
|
|
5
|
-
*
|
|
6
|
-
* This function provides a fluent interface for applying transformations to values,
|
|
7
|
-
* with intelligent method selection based on the input type:
|
|
8
|
-
* - For `Optional` values: Provides `mapOptional` for safe Optional transformations
|
|
9
|
-
* - For other values: Provides `mapNullable` for null-safe transformations
|
|
10
|
-
* - All types get the basic `map` method for general transformations
|
|
11
|
-
*
|
|
12
|
-
* The pipe maintains type safety throughout the chain, automatically selecting
|
|
13
|
-
* the appropriate overload based on the current value type.
|
|
14
|
-
*
|
|
15
|
-
* @template A The type of the initial value to wrap in a pipe.
|
|
16
|
-
* @param a The initial value to wrap in a pipe.
|
|
17
|
-
* @returns A pipe object with chaining methods appropriate for the value type.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* Basic value transformation chaining:
|
|
21
|
-
* ```typescript
|
|
22
|
-
* // Simple sequential transformations
|
|
23
|
-
* const result = pipe(10)
|
|
24
|
-
* .map(x => x * 2) // 20
|
|
25
|
-
* .map(x => x + 5) // 25
|
|
26
|
-
* .map(x => x.toString()) // '25'
|
|
27
|
-
* .value;
|
|
28
|
-
*
|
|
29
|
-
* // String processing pipeline
|
|
30
|
-
* const processed = pipe(" Hello World ")
|
|
31
|
-
* .map(s => s.trim()) // "Hello World"
|
|
32
|
-
* .map(s => s.toLowerCase()) // "hello world"
|
|
33
|
-
* .map(s => s.split(' ')) // ["hello", "world"]
|
|
34
|
-
* .map(arr => arr.join('-')) // "hello-world"
|
|
35
|
-
* .value;
|
|
36
|
-
* ```
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* Nullable value handling with automatic null checking:
|
|
40
|
-
* ```typescript
|
|
41
|
-
* // Safe operations on potentially null values
|
|
42
|
-
* const maybeNumber: number | null = getNumberFromAPI();
|
|
43
|
-
* const result = pipe(maybeNumber)
|
|
44
|
-
* .mapNullable(x => x * 2) // Only applies if not null
|
|
45
|
-
* .mapNullable(x => `Result: ${x}`) // Only applies if previous step succeeded
|
|
46
|
-
* .value; // 'Result: 20' or undefined
|
|
47
|
-
*
|
|
48
|
-
* // Handling undefined values
|
|
49
|
-
* const maybeUser: User | undefined = findUser(id);
|
|
50
|
-
* const userName = pipe(maybeUser)
|
|
51
|
-
* .mapNullable(user => user.name)
|
|
52
|
-
* .mapNullable(name => name.toUpperCase())
|
|
53
|
-
* .value; // string or undefined
|
|
54
|
-
* ```
|
|
55
|
-
*
|
|
56
|
-
* @example
|
|
57
|
-
* Optional value handling with monadic operations:
|
|
58
|
-
* ```typescript
|
|
59
|
-
* // Working with Optional types
|
|
60
|
-
* const optional = Optional.some(42);
|
|
61
|
-
* const result = pipe(optional)
|
|
62
|
-
* .mapOptional(x => x / 2) // Optional.some(21)
|
|
63
|
-
* .mapOptional(x => Math.sqrt(x)) // Optional.some(~4.58)
|
|
64
|
-
* .value; // Optional.some(4.58...)
|
|
65
|
-
*
|
|
66
|
-
* // Optional chains that can become None
|
|
67
|
-
* const parseAndProcess = (input: string) =>
|
|
68
|
-
* pipe(Optional.fromNullable(input))
|
|
69
|
-
* .mapOptional(s => s.trim())
|
|
70
|
-
* .mapOptional(s => s.length > 0 ? s : null) // Could become None
|
|
71
|
-
* .mapOptional(s => parseInt(s, 10))
|
|
72
|
-
* .mapOptional(n => isNaN(n) ? null : n)
|
|
73
|
-
* .value; // Optional<number>
|
|
74
|
-
* ```
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* Mixed type transformations:
|
|
78
|
-
* ```typescript
|
|
79
|
-
* // Starting with a string, transforming through different types
|
|
80
|
-
* const complex = pipe('hello')
|
|
81
|
-
* .map(s => s.length) // number: 5
|
|
82
|
-
* .map(n => n > 3 ? n : null) // number | null: 5
|
|
83
|
-
* .mapNullable(n => n * 10) // number: 50 (or undefined if null)
|
|
84
|
-
* .value; // 50 or undefined
|
|
85
|
-
*
|
|
86
|
-
* // API response processing
|
|
87
|
-
* const processApiResponse = (response: ApiResponse) =>
|
|
88
|
-
* pipe(response)
|
|
89
|
-
* .map(r => r.data) // Extract data
|
|
90
|
-
* .mapNullable(data => data.user) // Safe user access
|
|
91
|
-
* .mapNullable(user => user.profile) // Safe profile access
|
|
92
|
-
* .mapNullable(profile => profile.avatar) // Safe avatar access
|
|
93
|
-
* .value; // string | undefined
|
|
94
|
-
* ```
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
* Error-safe computation chains:
|
|
98
|
-
* ```typescript
|
|
99
|
-
* // Building complex computations safely
|
|
100
|
-
* const safeCalculation = (input: unknown) =>
|
|
101
|
-
* pipe(input)
|
|
102
|
-
* .map(val => typeof val === 'number' ? val : null)
|
|
103
|
-
* .mapNullable(n => n > 0 ? n : null) // Positive numbers only
|
|
104
|
-
* .mapNullable(n => Math.sqrt(n)) // Safe square root
|
|
105
|
-
* .mapNullable(n => n < 100 ? n : null) // Limit result
|
|
106
|
-
* .mapNullable(n => Math.round(n * 100) / 100) // Round to 2 decimals
|
|
107
|
-
* .value; // number | undefined
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
111
|
-
const pipe = ((a) => {
|
|
3
|
+
function pipe(a) {
|
|
112
4
|
if (Optional.isOptional(a)) {
|
|
113
5
|
return {
|
|
114
6
|
value: a,
|
|
@@ -123,7 +15,7 @@ const pipe = ((a) => {
|
|
|
123
15
|
mapNullable: (fn) => pipe(a == null ? undefined : fn(a)),
|
|
124
16
|
};
|
|
125
17
|
}
|
|
126
|
-
}
|
|
18
|
+
}
|
|
127
19
|
|
|
128
20
|
export { pipe };
|
|
129
21
|
//# sourceMappingURL=pipe.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pipe.mjs","sources":["../../src/functional/pipe.mts"],"sourcesContent":[null],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"pipe.mjs","sources":["../../src/functional/pipe.mts"],"sourcesContent":[null],"names":[],"mappings":";;AA6HM,SAAU,IAAI,CAAU,CAAI,EAAA;AAChC,IAAA,IAAI,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE;QAC1B,OAAO;AACL,YAAA,KAAK,EAAE,CAAC;AACR,YAAA,GAAG,EAAE,CAAC,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AACxB,YAAA,WAAW,EAAE,CAAC,EAAE,KAAK,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;SAC/C;IACH;SAAO;QACL,OAAO;AACL,YAAA,KAAK,EAAE,CAAC;AACR,YAAA,GAAG,EAAE,CAAC,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YACxB,WAAW,EAAE,CAAC,EAAE,KAAK,IAAI,CAAC,CAAC,IAAI,IAAI,GAAG,SAAS,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;SACzD;IACH;AACF;;;;"}
|
|
@@ -304,11 +304,8 @@ export declare namespace Result {
|
|
|
304
304
|
* };
|
|
305
305
|
* ```
|
|
306
306
|
*/
|
|
307
|
-
export
|
|
308
|
-
|
|
309
|
-
<R extends Ok<unknown>>(result: R): UnwrapOk<R>;
|
|
310
|
-
<R extends Base>(result: R): UnwrapOk<R> | undefined;
|
|
311
|
-
};
|
|
307
|
+
export function unwrapOk<R extends Ok<unknown>>(result: R): UnwrapOk<R>;
|
|
308
|
+
export function unwrapOk<R extends Base>(result: R): UnwrapOk<R> | undefined;
|
|
312
309
|
/**
|
|
313
310
|
* Unwraps a `Result`, returning the success value or a default value if it is `Result.Err`.
|
|
314
311
|
* @template R The `Result.Base` type to unwrap.
|
|
@@ -323,11 +320,8 @@ export declare namespace Result {
|
|
|
323
320
|
* console.log(value); // 42
|
|
324
321
|
* ```
|
|
325
322
|
*/
|
|
326
|
-
export
|
|
327
|
-
|
|
328
|
-
<R extends Base, D>(result: R, defaultValue: D): D | UnwrapOk<R>;
|
|
329
|
-
<S, D>(defaultValue: D): <E>(result: Result<S, E>) => D | S;
|
|
330
|
-
};
|
|
323
|
+
export function unwrapOkOr<R extends Base, D>(result: R, defaultValue: D): D | UnwrapOk<R>;
|
|
324
|
+
export function unwrapOkOr<S, D>(defaultValue: D): <E>(result: Result<S, E>) => D | S;
|
|
331
325
|
/**
|
|
332
326
|
* Unwraps a `Result`, returning the error value.
|
|
333
327
|
* Throws an error if the `Result` is `Result.Ok`.
|
|
@@ -389,11 +383,8 @@ export declare namespace Result {
|
|
|
389
383
|
* console.log(error); // "failed"
|
|
390
384
|
* ```
|
|
391
385
|
*/
|
|
392
|
-
export
|
|
393
|
-
|
|
394
|
-
<R extends Base, D>(result: R, defaultValue: D): D | UnwrapErr<R>;
|
|
395
|
-
<E, D>(defaultValue: D): <S>(result: Result<S, E>) => D | E;
|
|
396
|
-
};
|
|
386
|
+
export function unwrapErrOr<R extends Base, D>(result: R, defaultValue: D): D | UnwrapErr<R>;
|
|
387
|
+
export function unwrapErrOr<E, D>(defaultValue: D): <S>(result: Result<S, E>) => D | E;
|
|
397
388
|
/**
|
|
398
389
|
* Maps a `Result<S, E>` to `Result<S2, E>` by applying a function to the success value.
|
|
399
390
|
* If the `Result` is `Result.Err`, returns the original `Err`.
|
|
@@ -415,11 +406,8 @@ export declare namespace Result {
|
|
|
415
406
|
* console.log(Result.unwrap(result2)); // 10
|
|
416
407
|
* ```
|
|
417
408
|
*/
|
|
418
|
-
export
|
|
419
|
-
|
|
420
|
-
<R extends Base, S2>(result: R, mapFn: (value: UnwrapOk<R>) => S2): Result<S2, UnwrapErr<R>>;
|
|
421
|
-
<S, S2>(mapFn: (value: S) => S2): <E>(result: Result<S, E>) => Result<S2, E>;
|
|
422
|
-
};
|
|
409
|
+
export function map<R extends Base, S2>(result: R, mapFn: (value: UnwrapOk<R>) => S2): Result<S2, UnwrapErr<R>>;
|
|
410
|
+
export function map<S, S2>(mapFn: (value: S) => S2): <E>(result: Result<S, E>) => Result<S2, E>;
|
|
423
411
|
/**
|
|
424
412
|
* Maps a `Result<S, E>` to `Result<S, E2>` by applying a function to the error value.
|
|
425
413
|
* If the `Result` is `Result.Ok`, returns the original `Ok`.
|
|
@@ -435,11 +423,8 @@ export declare namespace Result {
|
|
|
435
423
|
* console.log(Result.unwrapErr(mapped)); // "ERROR"
|
|
436
424
|
* ```
|
|
437
425
|
*/
|
|
438
|
-
export
|
|
439
|
-
|
|
440
|
-
<R extends Base, E2>(result: R, mapFn: (error: UnwrapErr<R>) => E2): Result<UnwrapOk<R>, E2>;
|
|
441
|
-
<E, E2>(mapFn: (error: E) => E2): <S>(result: Result<S, E>) => Result<S, E2>;
|
|
442
|
-
};
|
|
426
|
+
export function mapErr<R extends Base, E2>(result: R, mapFn: (error: UnwrapErr<R>) => E2): Result<UnwrapOk<R>, E2>;
|
|
427
|
+
export function mapErr<E, E2>(mapFn: (error: E) => E2): <S>(result: Result<S, E>) => Result<S, E2>;
|
|
443
428
|
/**
|
|
444
429
|
* Applies one of two functions depending on whether the `Result` is `Ok` or `Err`.
|
|
445
430
|
* @template R The input `Result.Base` type.
|
|
@@ -456,11 +441,8 @@ export declare namespace Result {
|
|
|
456
441
|
* console.log(Result.unwrapOk(folded)); // 84
|
|
457
442
|
* ```
|
|
458
443
|
*/
|
|
459
|
-
export
|
|
460
|
-
|
|
461
|
-
<R extends Base, S2, E2>(result: R, mapFn: (value: UnwrapOk<R>) => S2, mapErrFn: (error: UnwrapErr<R>) => E2): Result<S2, E2>;
|
|
462
|
-
<S, E, S2, E2>(mapFn: (value: S) => S2, mapErrFn: (error: E) => E2): (result: Result<S, E>) => Result<S2, E2>;
|
|
463
|
-
};
|
|
444
|
+
export function fold<R extends Base, S2, E2>(result: R, mapFn: (value: UnwrapOk<R>) => S2, mapErrFn: (error: UnwrapErr<R>) => E2): Result<S2, E2>;
|
|
445
|
+
export function fold<S, E, S2, E2>(mapFn: (value: S) => S2, mapErrFn: (error: E) => E2): (result: Result<S, E>) => Result<S2, E2>;
|
|
464
446
|
/**
|
|
465
447
|
* Applies a function that returns a `Result` to the success value of a `Result`.
|
|
466
448
|
* If the input is `Err`, returns the original `Err`.
|
|
@@ -480,11 +462,8 @@ export declare namespace Result {
|
|
|
480
462
|
* console.log(Result.unwrapOk(result)); // 5
|
|
481
463
|
* ```
|
|
482
464
|
*/
|
|
483
|
-
export
|
|
484
|
-
|
|
485
|
-
<R extends Base, S2, E2>(result: R, flatMapFn: (value: UnwrapOk<R>) => Result<S2, E2>): Result<S2, E2 | UnwrapErr<R>>;
|
|
486
|
-
<S, S2, E2>(flatMapFn: (value: S) => Result<S2, E2>): <E>(result: Result<S, E>) => Result<S2, E | E2>;
|
|
487
|
-
};
|
|
465
|
+
export function flatMap<R extends Base, S2, E2>(result: R, flatMapFn: (value: UnwrapOk<R>) => Result<S2, E2>): Result<S2, E2 | UnwrapErr<R>>;
|
|
466
|
+
export function flatMap<S, S2, E2>(flatMapFn: (value: S) => Result<S2, E2>): <E>(result: Result<S, E>) => Result<S2, E | E2>;
|
|
488
467
|
/**
|
|
489
468
|
* Unwraps a `Result`, returning the success value or throwing an error with the provided message.
|
|
490
469
|
* @template R The `Result.Base` type to unwrap.
|
|
@@ -499,11 +478,8 @@ export declare namespace Result {
|
|
|
499
478
|
* console.log(value); // 42
|
|
500
479
|
* ```
|
|
501
480
|
*/
|
|
502
|
-
export
|
|
503
|
-
|
|
504
|
-
<R extends Base>(result: R, message: string): UnwrapOk<R>;
|
|
505
|
-
<S>(message: string): <E>(result: Result<S, E>) => S;
|
|
506
|
-
};
|
|
481
|
+
export function expectToBe<R extends Base>(result: R, message: string): UnwrapOk<R>;
|
|
482
|
+
export function expectToBe<S>(message: string): <E>(result: Result<S, E>) => S;
|
|
507
483
|
/**
|
|
508
484
|
* @internal
|
|
509
485
|
* Utility type to extract the resolved value type from a Promise.
|
|
@@ -605,11 +581,8 @@ export declare namespace Result {
|
|
|
605
581
|
* console.log(Result.unwrapOk(result)); // "default"
|
|
606
582
|
* ```
|
|
607
583
|
*/
|
|
608
|
-
export
|
|
609
|
-
|
|
610
|
-
<R extends Base, R2 extends Base>(result: R, alternative: R2): NarrowToOk<R> | R2;
|
|
611
|
-
<S, E, S2, E2>(alternative: Result<S2, E2>): (result: Result<S, E>) => Result<S, E> | Result<S2, E2>;
|
|
612
|
-
};
|
|
584
|
+
export function orElse<R extends Base, R2 extends Base>(result: R, alternative: R2): NarrowToOk<R> | R2;
|
|
585
|
+
export function orElse<S, E, S2, E2>(alternative: Result<S2, E2>): (result: Result<S, E>) => Result<S, E> | Result<S2, E2>;
|
|
613
586
|
/**
|
|
614
587
|
* Combines two `Result` values into a single `Result` containing a tuple.
|
|
615
588
|
* If either `Result` is `Err`, returns the first `Err` encountered.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result.d.mts","sourceRoot":"","sources":["../../src/functional/result.mts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,2EAA2E;A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AAU,MAAM,CAAC,CAAC,SAAS,IAAI,EAAE,EAAE,SAAS,IAAI,EACpD,MAAM,EAAE,CAAC,EACT,WAAW,EAAE,EAAE,GACd,UAAU,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;IAGtB,MAAM,UAAU,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EACjC,WAAW,EAAE,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,GAC1B,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IAwB3D;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,MAAM,CAAC,MAAM,GAAG,GAAI,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAChC,SAAS,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,EACvB,SAAS,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,KACtB,MAAM,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,CAKvB,CAAC;;CACf"}
|
|
@@ -222,60 +222,14 @@ var Result;
|
|
|
222
222
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
223
223
|
return result.value;
|
|
224
224
|
};
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
* @template R The `Result.Base` type to unwrap.
|
|
234
|
-
* @param result The `Result` to unwrap.
|
|
235
|
-
* @returns The success value if `Result.Ok`, otherwise `undefined`.
|
|
236
|
-
* @example
|
|
237
|
-
* ```typescript
|
|
238
|
-
* // With guaranteed Ok - returns the value
|
|
239
|
-
* const success = Result.ok(42);
|
|
240
|
-
* const value = Result.unwrapOk(success); // Type: number, Value: 42
|
|
241
|
-
*
|
|
242
|
-
* // With general Result - may return undefined
|
|
243
|
-
* const maybeResult: Result<string, Error> = fetchData();
|
|
244
|
-
* const data = Result.unwrapOk(maybeResult); // Type: string | undefined
|
|
245
|
-
*
|
|
246
|
-
* // Safe pattern for handling both cases
|
|
247
|
-
* const result = Result.ok("hello");
|
|
248
|
-
* const unwrapped = Result.unwrapOk(result);
|
|
249
|
-
* if (unwrapped !== undefined) {
|
|
250
|
-
* console.log(unwrapped.toUpperCase()); // "HELLO"
|
|
251
|
-
* }
|
|
252
|
-
*
|
|
253
|
-
* // Useful in conditional chains
|
|
254
|
-
* const processResult = (r: Result<number, string>) => {
|
|
255
|
-
* const value = Result.unwrapOk(r);
|
|
256
|
-
* return value !== undefined ? value * 2 : 0;
|
|
257
|
-
* };
|
|
258
|
-
* ```
|
|
259
|
-
*/
|
|
260
|
-
Result.unwrapOk = ((result) => Result.isErr(result)
|
|
261
|
-
? undefined
|
|
262
|
-
: // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
263
|
-
result.value);
|
|
264
|
-
/**
|
|
265
|
-
* Unwraps a `Result`, returning the success value or a default value if it is `Result.Err`.
|
|
266
|
-
* @template R The `Result.Base` type to unwrap.
|
|
267
|
-
* @template D The type of the default value.
|
|
268
|
-
* @param result The `Result` to unwrap.
|
|
269
|
-
* @param defaultValue The value to return if `result` is `Result.Err`.
|
|
270
|
-
* @returns The success value if `Result.Ok`, otherwise `defaultValue`.
|
|
271
|
-
* @example
|
|
272
|
-
* ```typescript
|
|
273
|
-
* const result = Result.ok(42);
|
|
274
|
-
* const value = Result.unwrapOkOr(result, 0);
|
|
275
|
-
* console.log(value); // 42
|
|
276
|
-
* ```
|
|
277
|
-
*/
|
|
278
|
-
Result.unwrapOkOr = ((...args) => {
|
|
225
|
+
function unwrapOk(result) {
|
|
226
|
+
return Result.isErr(result)
|
|
227
|
+
? undefined
|
|
228
|
+
: // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
229
|
+
result.value;
|
|
230
|
+
}
|
|
231
|
+
Result.unwrapOk = unwrapOk;
|
|
232
|
+
function unwrapOkOr(...args) {
|
|
279
233
|
switch (args.length) {
|
|
280
234
|
case 2: {
|
|
281
235
|
// Direct version: first argument is result
|
|
@@ -286,10 +240,11 @@ var Result;
|
|
|
286
240
|
case 1: {
|
|
287
241
|
// Curried version: first argument is default value
|
|
288
242
|
const [defaultValue] = args;
|
|
289
|
-
return (result) =>
|
|
243
|
+
return (result) => unwrapOkOr(result, defaultValue);
|
|
290
244
|
}
|
|
291
245
|
}
|
|
292
|
-
}
|
|
246
|
+
}
|
|
247
|
+
Result.unwrapOkOr = unwrapOkOr;
|
|
293
248
|
/**
|
|
294
249
|
* Unwraps a `Result`, returning the error value.
|
|
295
250
|
* Throws an error if the `Result` is `Result.Ok`.
|
|
@@ -347,21 +302,7 @@ var Result;
|
|
|
347
302
|
Result.unwrapErr = (result) =>
|
|
348
303
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
349
304
|
Result.isErr(result) ? result.value : undefined;
|
|
350
|
-
|
|
351
|
-
* Unwraps a `Result`, returning the error value or a default value if it is `Result.Ok`.
|
|
352
|
-
* @template R The `Result.Base` type to unwrap.
|
|
353
|
-
* @template D The type of the default value.
|
|
354
|
-
* @param result The `Result` to unwrap.
|
|
355
|
-
* @param defaultValue The value to return if `result` is `Result.Ok`.
|
|
356
|
-
* @returns The error value if `Result.Err`, otherwise `defaultValue`.
|
|
357
|
-
* @example
|
|
358
|
-
* ```typescript
|
|
359
|
-
* const result = Result.err("failed");
|
|
360
|
-
* const error = Result.unwrapErrOr(result, "default");
|
|
361
|
-
* console.log(error); // "failed"
|
|
362
|
-
* ```
|
|
363
|
-
*/
|
|
364
|
-
Result.unwrapErrOr = ((...args) => {
|
|
305
|
+
function unwrapErrOr(...args) {
|
|
365
306
|
switch (args.length) {
|
|
366
307
|
case 2: {
|
|
367
308
|
// Direct version: first argument is result
|
|
@@ -372,33 +313,12 @@ var Result;
|
|
|
372
313
|
case 1: {
|
|
373
314
|
// Curried version: first argument is default value
|
|
374
315
|
const [defaultValue] = args;
|
|
375
|
-
return (result) =>
|
|
316
|
+
return (result) => unwrapErrOr(result, defaultValue);
|
|
376
317
|
}
|
|
377
318
|
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
* If the `Result` is `Result.Err`, returns the original `Err`.
|
|
382
|
-
* @template R The input `Result.Base` type.
|
|
383
|
-
* @template S2 The type of the success value returned by the mapping function.
|
|
384
|
-
* @param result The `Result` to map.
|
|
385
|
-
* @param mapFn The function to apply to the success value if present.
|
|
386
|
-
* @returns A new `Result<S2, UnwrapErr<R>>`.
|
|
387
|
-
* @example
|
|
388
|
-
* ```typescript
|
|
389
|
-
* // Regular usage
|
|
390
|
-
* const result = Result.ok(5);
|
|
391
|
-
* const mapped = Result.map(result, x => x * 2);
|
|
392
|
-
* console.log(Result.unwrap(mapped)); // 10
|
|
393
|
-
*
|
|
394
|
-
* // Curried version for use with pipe
|
|
395
|
-
* const doubler = Result.map((x: number) => x * 2);
|
|
396
|
-
* const result2 = pipe(Result.ok(5)).map(doubler).value;
|
|
397
|
-
* console.log(Result.unwrap(result2)); // 10
|
|
398
|
-
* ```
|
|
399
|
-
*/
|
|
400
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
401
|
-
Result.map = ((...args) => {
|
|
319
|
+
}
|
|
320
|
+
Result.unwrapErrOr = unwrapErrOr;
|
|
321
|
+
function map(...args) {
|
|
402
322
|
switch (args.length) {
|
|
403
323
|
case 2: {
|
|
404
324
|
const [result, mapFn] = args;
|
|
@@ -411,27 +331,12 @@ var Result;
|
|
|
411
331
|
case 1: {
|
|
412
332
|
// Curried version: first argument is mapping function
|
|
413
333
|
const [mapFn] = args;
|
|
414
|
-
return (result) =>
|
|
334
|
+
return (result) => map(result, mapFn);
|
|
415
335
|
}
|
|
416
336
|
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
* If the `Result` is `Result.Ok`, returns the original `Ok`.
|
|
421
|
-
* @template R The input `Result.Base` type.
|
|
422
|
-
* @template E2 The type of the error value returned by the mapping function.
|
|
423
|
-
* @param result The `Result` to map.
|
|
424
|
-
* @param mapFn The function to apply to the error value if present.
|
|
425
|
-
* @returns A new `Result<UnwrapOk<R>, E2>`.
|
|
426
|
-
* @example
|
|
427
|
-
* ```typescript
|
|
428
|
-
* const result = Result.err("error");
|
|
429
|
-
* const mapped = Result.mapErr(result, e => e.toUpperCase());
|
|
430
|
-
* console.log(Result.unwrapErr(mapped)); // "ERROR"
|
|
431
|
-
* ```
|
|
432
|
-
*/
|
|
433
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
434
|
-
Result.mapErr = ((...args) => {
|
|
337
|
+
}
|
|
338
|
+
Result.map = map;
|
|
339
|
+
function mapErr(...args) {
|
|
435
340
|
switch (args.length) {
|
|
436
341
|
case 2: {
|
|
437
342
|
const [result, mapFn] = args;
|
|
@@ -444,28 +349,12 @@ var Result;
|
|
|
444
349
|
case 1: {
|
|
445
350
|
// Curried version: first argument is mapping function
|
|
446
351
|
const [mapFn] = args;
|
|
447
|
-
return (result) =>
|
|
352
|
+
return (result) => mapErr(result, mapFn);
|
|
448
353
|
}
|
|
449
354
|
}
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
* @template R The input `Result.Base` type.
|
|
454
|
-
* @template S2 The type of the success value returned by `mapFn`.
|
|
455
|
-
* @template E2 The type of the error value returned by `mapErrFn`.
|
|
456
|
-
* @param result The `Result` to fold.
|
|
457
|
-
* @param mapFn The function to apply if `result` is `Ok`.
|
|
458
|
-
* @param mapErrFn The function to apply if `result` is `Err`.
|
|
459
|
-
* @returns A new `Result<S2, E2>` based on the applied function.
|
|
460
|
-
* @example
|
|
461
|
-
* ```typescript
|
|
462
|
-
* const result = Result.ok(42);
|
|
463
|
-
* const folded = Result.fold(result, x => x * 2, () => 0);
|
|
464
|
-
* console.log(Result.unwrapOk(folded)); // 84
|
|
465
|
-
* ```
|
|
466
|
-
*/
|
|
467
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
468
|
-
Result.fold = ((...args) => {
|
|
355
|
+
}
|
|
356
|
+
Result.mapErr = mapErr;
|
|
357
|
+
function fold(...args) {
|
|
469
358
|
switch (args.length) {
|
|
470
359
|
case 3: {
|
|
471
360
|
const [result, mapFn, mapErrFn] = args;
|
|
@@ -480,28 +369,9 @@ var Result;
|
|
|
480
369
|
return (result) => Result.isOk(result) ? Result.ok(mapFn(result.value)) : Result.err(mapErrFn(result.value));
|
|
481
370
|
}
|
|
482
371
|
}
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
* If the input is `Err`, returns the original `Err`.
|
|
487
|
-
* This is the monadic bind operation for `Result`.
|
|
488
|
-
* @template R The input `Result.Base` type.
|
|
489
|
-
* @template S2 The success type of the `Result` returned by the function.
|
|
490
|
-
* @template E2 The error type of the `Result` returned by the function.
|
|
491
|
-
* @param result The `Result` to flat map.
|
|
492
|
-
* @param flatMapFn The function to apply that returns a `Result`.
|
|
493
|
-
* @returns The result of applying the function, or the original `Err`.
|
|
494
|
-
* @example
|
|
495
|
-
* ```typescript
|
|
496
|
-
* const divide = (a: number, b: number): Result<number, string> =>
|
|
497
|
-
* b === 0 ? Result.err("Division by zero") : Result.ok(a / b);
|
|
498
|
-
*
|
|
499
|
-
* const result = Result.flatMap(Result.ok(10), x => divide(x, 2));
|
|
500
|
-
* console.log(Result.unwrapOk(result)); // 5
|
|
501
|
-
* ```
|
|
502
|
-
*/
|
|
503
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
504
|
-
Result.flatMap = ((...args) => {
|
|
372
|
+
}
|
|
373
|
+
Result.fold = fold;
|
|
374
|
+
function flatMap(...args) {
|
|
505
375
|
switch (args.length) {
|
|
506
376
|
case 2: {
|
|
507
377
|
const [result, flatMapFn] = args;
|
|
@@ -516,39 +386,26 @@ var Result;
|
|
|
516
386
|
return (result) => Result.isErr(result) ? result : flatMapFn(result.value);
|
|
517
387
|
}
|
|
518
388
|
}
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
* @template R The `Result.Base` type to unwrap.
|
|
523
|
-
* @param result The `Result` to unwrap.
|
|
524
|
-
* @param message The error message to throw if the `Result` is `Result.Err`.
|
|
525
|
-
* @returns The success value if `Result.Ok`.
|
|
526
|
-
* @throws Error with the provided message if the `Result` is `Result.Err`.
|
|
527
|
-
* @example
|
|
528
|
-
* ```typescript
|
|
529
|
-
* const result = Result.ok(42);
|
|
530
|
-
* const value = Result.expectToBe(result, "Operation must succeed");
|
|
531
|
-
* console.log(value); // 42
|
|
532
|
-
* ```
|
|
533
|
-
*/
|
|
534
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
535
|
-
Result.expectToBe = ((...args) => {
|
|
389
|
+
}
|
|
390
|
+
Result.flatMap = flatMap;
|
|
391
|
+
function expectToBe(...args) {
|
|
536
392
|
switch (args.length) {
|
|
537
393
|
case 2: {
|
|
538
394
|
// Direct version: first argument is result
|
|
539
395
|
const [result, message] = args;
|
|
540
396
|
if (Result.isOk(result)) {
|
|
541
|
-
return
|
|
397
|
+
return unwrapOk(result);
|
|
542
398
|
}
|
|
543
399
|
throw new Error(message);
|
|
544
400
|
}
|
|
545
401
|
case 1: {
|
|
546
402
|
// Curried version: first argument is message
|
|
547
403
|
const [message] = args;
|
|
548
|
-
return (result) =>
|
|
404
|
+
return (result) => expectToBe(result, message);
|
|
549
405
|
}
|
|
550
406
|
}
|
|
551
|
-
}
|
|
407
|
+
}
|
|
408
|
+
Result.expectToBe = expectToBe;
|
|
552
409
|
/**
|
|
553
410
|
* Converts a Promise into a Promise that resolves to a `Result`.
|
|
554
411
|
* If the input Promise resolves, the `Result` will be `Ok` with the resolved value.
|
|
@@ -618,7 +475,7 @@ var Result;
|
|
|
618
475
|
*/
|
|
619
476
|
Result.swap = (result) =>
|
|
620
477
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
621
|
-
Result.isOk(result) ? Result.err(
|
|
478
|
+
Result.isOk(result) ? Result.err(unwrapOk(result)) : Result.ok(result.value);
|
|
622
479
|
/**
|
|
623
480
|
* Converts a `Result` to an `Optional`.
|
|
624
481
|
*
|
|
@@ -646,22 +503,8 @@ var Result;
|
|
|
646
503
|
*
|
|
647
504
|
* ```
|
|
648
505
|
*/
|
|
649
|
-
Result.toOptional = (result) => Result.isOk(result) ? Optional.some(
|
|
650
|
-
|
|
651
|
-
* Returns the `Result` if it is `Ok`, otherwise returns the alternative.
|
|
652
|
-
* @template R The input `Result.Base` type.
|
|
653
|
-
* @param result The `Result` to check.
|
|
654
|
-
* @param alternative The alternative `Result` to return if the first is `Err`.
|
|
655
|
-
* @returns The first `Result` if `Ok`, otherwise the alternative.
|
|
656
|
-
* @example
|
|
657
|
-
* ```typescript
|
|
658
|
-
* const primary = Result.err("error");
|
|
659
|
-
* const fallback = Result.ok("default");
|
|
660
|
-
* const result = Result.orElse(primary, fallback);
|
|
661
|
-
* console.log(Result.unwrapOk(result)); // "default"
|
|
662
|
-
* ```
|
|
663
|
-
*/
|
|
664
|
-
Result.orElse = ((...args) => {
|
|
506
|
+
Result.toOptional = (result) => Result.isOk(result) ? Optional.some(unwrapOk(result)) : Optional.none;
|
|
507
|
+
function orElse(...args) {
|
|
665
508
|
switch (args.length) {
|
|
666
509
|
case 2: {
|
|
667
510
|
const [result, alternative] = args;
|
|
@@ -670,10 +513,11 @@ var Result;
|
|
|
670
513
|
case 1: {
|
|
671
514
|
// Curried version: one argument (alternative) provided
|
|
672
515
|
const [alternative] = args;
|
|
673
|
-
return (result) =>
|
|
516
|
+
return (result) => orElse(result, alternative);
|
|
674
517
|
}
|
|
675
518
|
}
|
|
676
|
-
}
|
|
519
|
+
}
|
|
520
|
+
Result.orElse = orElse;
|
|
677
521
|
/**
|
|
678
522
|
* Combines two `Result` values into a single `Result` containing a tuple.
|
|
679
523
|
* If either `Result` is `Err`, returns the first `Err` encountered.
|