@carbonteq/fp 0.8.0-alpha.5 → 0.8.0

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.
Files changed (65) hide show
  1. package/README.md +270 -49
  2. package/dist/chunk-CUT6urMc.cjs +30 -0
  3. package/dist/index.cjs +13 -664
  4. package/dist/index.d.cts +5 -190
  5. package/dist/index.d.ts +5 -190
  6. package/dist/index.js +5 -650
  7. package/dist/match-BQP0RNSk.js +13 -0
  8. package/dist/match-BQP0RNSk.js.map +1 -0
  9. package/dist/match-COIWq-e8.d.cts +18 -0
  10. package/dist/match-COIWq-e8.d.cts.map +1 -0
  11. package/dist/match-DXKW7ym5.d.ts +18 -0
  12. package/dist/match-DXKW7ym5.d.ts.map +1 -0
  13. package/dist/match-DyAuOeqx.cjs +25 -0
  14. package/dist/match-DyAuOeqx.cjs.map +1 -0
  15. package/dist/match.cjs +4 -0
  16. package/dist/match.d.cts +5 -0
  17. package/dist/match.d.ts +5 -0
  18. package/dist/match.js +3 -0
  19. package/dist/option-8_eiz02z.d.cts +45 -0
  20. package/dist/option-8_eiz02z.d.cts.map +1 -0
  21. package/dist/option-Be_DXVE6.js +181 -0
  22. package/dist/option-Be_DXVE6.js.map +1 -0
  23. package/dist/option-CoelyMEg.d.ts +45 -0
  24. package/dist/option-CoelyMEg.d.ts.map +1 -0
  25. package/dist/option-DHCdfOco.cjs +193 -0
  26. package/dist/option-DHCdfOco.cjs.map +1 -0
  27. package/dist/option.cjs +5 -0
  28. package/dist/option.d.cts +3 -0
  29. package/dist/option.d.ts +3 -0
  30. package/dist/option.js +4 -0
  31. package/dist/result-CdGWA5IL.js +318 -0
  32. package/dist/result-CdGWA5IL.js.map +1 -0
  33. package/dist/result-DsVjSFnO.d.ts +132 -0
  34. package/dist/result-DsVjSFnO.d.ts.map +1 -0
  35. package/dist/result-Dy_C5dC6.d.cts +132 -0
  36. package/dist/result-Dy_C5dC6.d.cts.map +1 -0
  37. package/dist/result-nuhyCRyu.cjs +336 -0
  38. package/dist/result-nuhyCRyu.cjs.map +1 -0
  39. package/dist/result.cjs +6 -0
  40. package/dist/result.d.cts +3 -0
  41. package/dist/result.d.ts +3 -0
  42. package/dist/result.js +4 -0
  43. package/dist/tuple.cjs +14 -0
  44. package/dist/tuple.cjs.map +1 -0
  45. package/dist/tuple.d.cts +12 -0
  46. package/dist/tuple.d.cts.map +1 -0
  47. package/dist/tuple.d.ts +12 -0
  48. package/dist/tuple.d.ts.map +1 -0
  49. package/dist/tuple.js +12 -0
  50. package/dist/tuple.js.map +1 -0
  51. package/dist/unit-CnjFzJN1.cjs +12 -0
  52. package/dist/unit-CnjFzJN1.cjs.map +1 -0
  53. package/dist/unit-CoBLIJFc.d.ts +9 -0
  54. package/dist/unit-CoBLIJFc.d.ts.map +1 -0
  55. package/dist/unit-D0y_qAUW.d.cts +9 -0
  56. package/dist/unit-D0y_qAUW.d.cts.map +1 -0
  57. package/dist/unit-udy5j53P.js +6 -0
  58. package/dist/unit-udy5j53P.js.map +1 -0
  59. package/dist/unit.cjs +3 -0
  60. package/dist/unit.d.cts +2 -0
  61. package/dist/unit.d.ts +2 -0
  62. package/dist/unit.js +3 -0
  63. package/package.json +23 -25
  64. package/dist/index.cjs.map +0 -1
  65. package/dist/index.js.map +0 -1
package/dist/index.d.cts CHANGED
@@ -1,190 +1,5 @@
1
- declare const UNIT: unique symbol;
2
- type UNIT = typeof UNIT;
3
-
4
- declare class UnwrappedErrWithOk extends Error {
5
- constructor(r: Result<unknown, unknown>);
6
- }
7
- declare class UnwrappedOkWithErr extends Error {
8
- constructor(r: Result<unknown, unknown>);
9
- }
10
- type UnitResult<E = never> = Result<UNIT, E>;
11
- type Mapper<T, U> = (val: T) => U;
12
- type UnwrapResult<T extends Result<unknown, unknown>> = T extends Result<infer U, infer E> ? {
13
- ok: U;
14
- err: E;
15
- } : never;
16
- type CombinedResultOk<T extends Result<unknown, unknown>[]> = {
17
- [K in keyof T]: UnwrapResult<T[K]>["ok"];
18
- };
19
- type CombinedResultErr<T extends Result<unknown, unknown>[]> = {
20
- [K in keyof T]: UnwrapResult<T[K]>["err"];
21
- }[number];
22
- type CombineResults<T extends Result<unknown, unknown>[]> = Result<CombinedResultOk<T>, CombinedResultErr<T>>;
23
- type UnwrapPromises<T extends Result<unknown, unknown>[]> = {
24
- [K in keyof T]: Awaited<UnwrapResult<T[K]>["ok"]>;
25
- };
26
- type IsPromise<T> = T extends Promise<unknown> ? true : false;
27
- type HasPromise<T extends readonly Result<unknown, unknown>[]> = true extends {
28
- [K in keyof T]: IsPromise<T[K] extends Result<infer U, unknown> ? U : never>;
29
- }[number] ? true : false;
30
- /** Sentinel value */
31
- declare const Sentinel: unique symbol;
32
- type Sentinel = typeof Sentinel;
33
- type ResultCtx<E> = {
34
- errSlot: E | Sentinel;
35
- };
36
- declare class Result<T, E> {
37
- #private;
38
- static readonly UNIT_RESULT: UnitResult;
39
- protected constructor(val: T | Sentinel, ctx: ResultCtx<E>);
40
- static Ok<T, E = never>(val: T): Result<T, E>;
41
- static Err<E, T = never>(err: E): Result<T, E>;
42
- isOk(): this is Result<T, never>;
43
- isErr(): this is Result<never, E>;
44
- isUnit(): this is Result<UNIT, never>;
45
- toString(): string;
46
- unwrap(this: Result<Promise<T>, E>): Promise<T>;
47
- unwrap(this: Result<T, E>): T;
48
- unwrapErr<T, E>(this: Result<Promise<T>, E>): Promise<E>;
49
- unwrapErr<T, E>(this: Result<T, E>): E;
50
- safeUnwrap(): T | null;
51
- /**
52
- *
53
- * @param {Mapper<T, U>} fn The mapping/endofunctor to apply to the internal Ok value (T -> U)
54
- * @returns Result<U, E> containing the mapped value
55
- * @example
56
- * const r = Result.Ok({data: 42})
57
- * const r2 = Result.Err(new Error("something went wrong"))
58
- * const mapper = (d: {data: 42}) => d.data * 2
59
- * const result1 = r.map(mapper) // Result containing 64 as the ok value
60
- * const result2 = r2.map(mapper) // mapper won't be applied, as r2 was in error state
61
- */
62
- map<T, U, E>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<U>): Result<Promise<U>, E>;
63
- map<T, U, E>(this: Result<Promise<T>, E>, fn: (val: T) => U): Result<Promise<U>, E>;
64
- map<In, U>(this: Result<In, E>, fn: (val: In) => Promise<U>): Result<Promise<U>, E>;
65
- map<In, U>(this: Result<In, E>, fn: (val: In) => U): Result<U, E>;
66
- /** Same as map, but for the error value instead of the Ok value */
67
- mapErr<U>(fn: Mapper<E, U>): Result<T, U>;
68
- mapBoth<T2, E2>(fnOk: (val: T) => T2, fnErr: (val: E) => E2): Result<T2, E2>;
69
- mapBoth<T2, E2, In = Awaited<T2>>(fnOk: (val: In) => T2, fnErr: (val: E) => E2): Result<Promise<In>, E2>;
70
- mapBoth<T2, E2, In = Awaited<T2>>(this: Result<Promise<In>, E>, fnOk: (val: In) => Promise<T2>, fnErr: (val: E) => E2): Result<Promise<In>, E2>;
71
- /**
72
- * Also known as `andThen`, `bind` or `fmap`
73
- * Like Result.map but used in cases where the mapper func returns a Result instead of a containable value
74
- * @example
75
- * const r: Result<string, SomeError> = Result.Ok("42")
76
- * const mapper = (d: string) => {
77
- * const n = Number.parseInt(d)
78
- * if(Number.isNumber(n) && !Number.isNaN(n)) return Result.Ok(n)
79
- *
80
- * return Result.Err(new InvalidNumStringError(d))
81
- * }
82
- * const result1 = r.map(mapper) // Type is Result<Result<number, InvalidNumStringError>, SomeError>
83
- * const result2 = r.flatMap(mapper) // Type is Result<number, SomeError | InvalidNumStringError>
84
- */
85
- flatMap<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Result<Promise<U>, E2>): Result<Promise<U>, E | E2>;
86
- flatMap<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<Promise<U>, E2>>): Result<Promise<U>, E | E2>;
87
- flatMap<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<U, E2>>): Result<Promise<U>, E | E2>;
88
- flatMap<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Result<U, E | E2>): Result<Promise<U>, E | E2>;
89
- flatMap<T, U, E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<Promise<U>, E | E2>>): Result<Promise<U>, E | E2>;
90
- flatMap<T, U, E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<U, E | E2>>): Result<Promise<U>, E | E2>;
91
- flatMap<T, U, E2>(this: Result<T, E>, fn: (val: T) => Result<U, E2>): Result<U, E | E2>;
92
- flatMap<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<Promise<Result<unknown, unknown>>, E2>>): never;
93
- flatMap<T, U, E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<Promise<Result<unknown, unknown>>, E | E2>>): never;
94
- private static flatMapHelper;
95
- private static flatMapInnerHelper;
96
- /**
97
- * For combining the errors only while retaining the original value.
98
- * @example
99
- * const r: Result<string, SomeError> = Result.Ok('42')
100
- * const binderOk = (d: string) => Result.Ok<number, SomeOtherError>(0909)
101
- * const binderErr = (d: string) => Result.Err<number, SomeOtherError>(new SomeOtherError())
102
- * const binderErrAsync = async (d: string) => Result.Err<number, SomeOtherError>(new SomeOtherError())
103
- *
104
- * const res1 = r.zipErr(binderOk) // Result<string, SomeError | SomeOtherError> - Value: Result.Ok('42') - No change in Ok type
105
- * const res2 = r.zipErr(binderErr) // Result<string, SomeError | SomeOtherError> - Value: Result.Err(new SomeOtherError())
106
- * const res3 = r.zipErr(binderErrAsync) // Result<Promise<string>, SomeError | SomeOtherError> - Value: Result.Err(new SomeOtherError())
107
- */
108
- zipErr<E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<unknown, E2>>): Result<Promise<T>, E | E2>;
109
- zipErr<E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<unknown, E2>>): Result<Promise<T>, E | E2>;
110
- zipErr<E2>(this: Result<T, E>, fn: (val: T) => Result<unknown, E2>): Result<T, E | E2>;
111
- private static zipErrHelper;
112
- zip<T, U>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<U>): Result<Promise<[T, U]>, E>;
113
- zip<T, U>(this: Result<Promise<T>, E>, fn: (val: T) => U): Result<Promise<[T, U]>, E>;
114
- zip<T, U>(this: Result<T, E>, fn: (val: T) => Promise<U>): Result<Promise<[T, U]>, E>;
115
- zip<T, U>(this: Result<T, E>, fn: (val: T) => U): Result<[T, U], E>;
116
- /** For combining two results lazily. For the eager eval version, see {@link and} */
117
- flatZip<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<Promise<Result<unknown, unknown>>, E2>>): never;
118
- flatZip<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<Promise<U>, E2>>): Result<Promise<[T, U]>, E | E2>;
119
- flatZip<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<U, E2>>): Result<Promise<[T, U]>, E | E2>;
120
- flatZip<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Result<Promise<U>, E2>): Result<Promise<[T, U]>, E | E2>;
121
- flatZip<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Result<U, E2>): Result<Promise<[T, U]>, E | E2>;
122
- flatZip<T, U, E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<Promise<Result<unknown, unknown>>, E2>>): never;
123
- flatZip<T, U, E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<Promise<U>, E2>>): Result<Promise<[T, U]>, E | E2>;
124
- flatZip<T, U, E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<U, E2>>): Result<Promise<[T, U]>, E | E2>;
125
- flatZip<T, U, E2>(this: Result<T, E>, fn: (val: T) => Result<Promise<U>, E2>): Result<Promise<[T, U]>, E | E2>;
126
- flatZip<T, U, E2>(this: Result<T, E>, fn: (val: T) => Result<U, E2>): Result<[T, U], E | E2>;
127
- private static flatZipHelper;
128
- private static flatZipInnerHelper;
129
- innerMap<In, E, Out>(this: Result<Array<In>, E>, mapper: (val: NoInfer<In>) => Out): Result<Array<Out>, E>;
130
- toPromise(): Promise<Result<Awaited<T>, E>>;
131
- validate<VE extends unknown[]>(this: Result<T, E>, validators: {
132
- [K in keyof VE]: (val: T) => Result<unknown, VE[K]>;
133
- }): Result<T, E | VE[number][]>;
134
- validate<VE extends unknown[]>(this: Result<T, E>, validators: {
135
- [K in keyof VE]: (val: T) => Promise<Result<unknown, VE[K]>>;
136
- }): Result<Promise<T>, E | VE[number][]>;
137
- validate<VE extends unknown[], In = Awaited<T>>(this: Result<T, E> | Result<Promise<T>, E>, validators: {
138
- [K in keyof VE]: (val: In) => Promise<Result<unknown, VE[K]>> | Result<unknown, VE[K]>;
139
- }): Result<Promise<T>, E | VE[number][]>;
140
- private static validateHelper;
141
- static all<T extends Result<unknown, unknown>[]>(...results: T): HasPromise<T> extends true ? Result<Promise<UnwrapPromises<T>>, CombinedResultErr<T>[]> : Result<CombinedResultOk<T>, CombinedResultErr<T>[]>;
142
- }
143
-
144
- declare class UnwrappedNone extends Error {
145
- constructor();
146
- }
147
- type UnitOption = Option<UNIT>;
148
- type UnwrapOption<T> = T extends Option<infer R> ? R : never;
149
- declare class Option<T> {
150
- #private;
151
- private val;
152
- private constructor();
153
- static readonly None: Option<never>;
154
- private static safeMap;
155
- static Some<Inner>(val: Inner): Option<Inner>;
156
- static fromNullable<T, Out = NonNullable<T>>(val: T): Option<Out>;
157
- isSome(): this is Option<T>;
158
- isNone(): this is Option<never>;
159
- isUnit(): this is Option<UNIT>;
160
- unwrap(): T;
161
- safeUnwrap(): T | null;
162
- map<U, Curr = Awaited<T>>(mapper: (val: Curr) => U): Option<U>;
163
- map<U, Curr = Awaited<T>>(mapper: (val: Curr) => Promise<U>): Option<Promise<U>>;
164
- mapOr<U, Curr = Awaited<T>>(default_: U, fn: (val: Curr) => U): Option<U>;
165
- mapOr<U, Curr = Awaited<T>>(default_: U, fn: (val: Curr) => Promise<U>): Option<Promise<U>>;
166
- flatMap<U, Curr = Awaited<T>>(mapper: (val: Curr) => Option<U>): Option<U>;
167
- flatMap<U, Curr = Awaited<T>>(mapper: (val: Curr) => Promise<Option<U>>): Option<Promise<U>>;
168
- zip<U, Curr>(this: Option<Promise<Curr>>, fn: (val: Curr) => U): Option<Promise<[Curr, U]>>;
169
- zip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => Promise<U>): Option<Promise<[T, U]>>;
170
- zip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => U): Option<[T, U]>;
171
- flatZip<U, Curr>(this: Option<Promise<Curr>>, fn: (val: Curr) => Promise<Option<U>>): Option<Promise<[Curr, U]>>;
172
- flatZip<U, Curr>(this: Option<Promise<Curr>>, fn: (val: Curr) => Option<U>): Option<Promise<[Curr, U]>>;
173
- flatZip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => Promise<Option<U>>): Option<Promise<[T, U]>>;
174
- flatZip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => Option<U>): Option<[T, U]>;
175
- toPromise<Curr = Awaited<T>>(): Promise<Option<Curr>>;
176
- static fromPromise<U>(o: Promise<Option<U>>): Option<Promise<U>>;
177
- toString(): string;
178
- innerMap<Inner, Out>(this: Option<Array<Inner>>, mapper: (val: Inner) => Out): Option<NoInfer<Out>[]>;
179
- }
180
-
181
- declare const matchRes: <T, E, U>(r: Result<T, E>, branches: {
182
- Ok: (val: T) => U;
183
- Err: (err: E) => U;
184
- }) => U;
185
- declare const matchOpt: <T, U>(o: Option<T>, branches: {
186
- Some: (val: T) => U;
187
- None: () => U;
188
- }) => U;
189
-
190
- export { type CombineResults, Option, Result, UNIT, type UnitOption, type UnitResult, type UnwrapOption, type UnwrapResult, UnwrappedErrWithOk, UnwrappedNone, UnwrappedOkWithErr, matchOpt, matchRes };
1
+ import { UNIT } from "./unit-D0y_qAUW.cjs";
2
+ import { CombineResults, Result, UnitResult, UnwrapResult, UnwrappedErrWithOk, UnwrappedOkWithErr } from "./result-Dy_C5dC6.cjs";
3
+ import { Option, UnitOption, UnwrapOption, UnwrappedNone } from "./option-8_eiz02z.cjs";
4
+ import { matchOpt, matchRes } from "./match-COIWq-e8.cjs";
5
+ export { CombineResults, Option, Result, UNIT, UnitOption, UnitResult, UnwrapOption, UnwrapResult, UnwrappedErrWithOk, UnwrappedNone, UnwrappedOkWithErr, matchOpt, matchRes };
package/dist/index.d.ts CHANGED
@@ -1,190 +1,5 @@
1
- declare const UNIT: unique symbol;
2
- type UNIT = typeof UNIT;
3
-
4
- declare class UnwrappedErrWithOk extends Error {
5
- constructor(r: Result<unknown, unknown>);
6
- }
7
- declare class UnwrappedOkWithErr extends Error {
8
- constructor(r: Result<unknown, unknown>);
9
- }
10
- type UnitResult<E = never> = Result<UNIT, E>;
11
- type Mapper<T, U> = (val: T) => U;
12
- type UnwrapResult<T extends Result<unknown, unknown>> = T extends Result<infer U, infer E> ? {
13
- ok: U;
14
- err: E;
15
- } : never;
16
- type CombinedResultOk<T extends Result<unknown, unknown>[]> = {
17
- [K in keyof T]: UnwrapResult<T[K]>["ok"];
18
- };
19
- type CombinedResultErr<T extends Result<unknown, unknown>[]> = {
20
- [K in keyof T]: UnwrapResult<T[K]>["err"];
21
- }[number];
22
- type CombineResults<T extends Result<unknown, unknown>[]> = Result<CombinedResultOk<T>, CombinedResultErr<T>>;
23
- type UnwrapPromises<T extends Result<unknown, unknown>[]> = {
24
- [K in keyof T]: Awaited<UnwrapResult<T[K]>["ok"]>;
25
- };
26
- type IsPromise<T> = T extends Promise<unknown> ? true : false;
27
- type HasPromise<T extends readonly Result<unknown, unknown>[]> = true extends {
28
- [K in keyof T]: IsPromise<T[K] extends Result<infer U, unknown> ? U : never>;
29
- }[number] ? true : false;
30
- /** Sentinel value */
31
- declare const Sentinel: unique symbol;
32
- type Sentinel = typeof Sentinel;
33
- type ResultCtx<E> = {
34
- errSlot: E | Sentinel;
35
- };
36
- declare class Result<T, E> {
37
- #private;
38
- static readonly UNIT_RESULT: UnitResult;
39
- protected constructor(val: T | Sentinel, ctx: ResultCtx<E>);
40
- static Ok<T, E = never>(val: T): Result<T, E>;
41
- static Err<E, T = never>(err: E): Result<T, E>;
42
- isOk(): this is Result<T, never>;
43
- isErr(): this is Result<never, E>;
44
- isUnit(): this is Result<UNIT, never>;
45
- toString(): string;
46
- unwrap(this: Result<Promise<T>, E>): Promise<T>;
47
- unwrap(this: Result<T, E>): T;
48
- unwrapErr<T, E>(this: Result<Promise<T>, E>): Promise<E>;
49
- unwrapErr<T, E>(this: Result<T, E>): E;
50
- safeUnwrap(): T | null;
51
- /**
52
- *
53
- * @param {Mapper<T, U>} fn The mapping/endofunctor to apply to the internal Ok value (T -> U)
54
- * @returns Result<U, E> containing the mapped value
55
- * @example
56
- * const r = Result.Ok({data: 42})
57
- * const r2 = Result.Err(new Error("something went wrong"))
58
- * const mapper = (d: {data: 42}) => d.data * 2
59
- * const result1 = r.map(mapper) // Result containing 64 as the ok value
60
- * const result2 = r2.map(mapper) // mapper won't be applied, as r2 was in error state
61
- */
62
- map<T, U, E>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<U>): Result<Promise<U>, E>;
63
- map<T, U, E>(this: Result<Promise<T>, E>, fn: (val: T) => U): Result<Promise<U>, E>;
64
- map<In, U>(this: Result<In, E>, fn: (val: In) => Promise<U>): Result<Promise<U>, E>;
65
- map<In, U>(this: Result<In, E>, fn: (val: In) => U): Result<U, E>;
66
- /** Same as map, but for the error value instead of the Ok value */
67
- mapErr<U>(fn: Mapper<E, U>): Result<T, U>;
68
- mapBoth<T2, E2>(fnOk: (val: T) => T2, fnErr: (val: E) => E2): Result<T2, E2>;
69
- mapBoth<T2, E2, In = Awaited<T2>>(fnOk: (val: In) => T2, fnErr: (val: E) => E2): Result<Promise<In>, E2>;
70
- mapBoth<T2, E2, In = Awaited<T2>>(this: Result<Promise<In>, E>, fnOk: (val: In) => Promise<T2>, fnErr: (val: E) => E2): Result<Promise<In>, E2>;
71
- /**
72
- * Also known as `andThen`, `bind` or `fmap`
73
- * Like Result.map but used in cases where the mapper func returns a Result instead of a containable value
74
- * @example
75
- * const r: Result<string, SomeError> = Result.Ok("42")
76
- * const mapper = (d: string) => {
77
- * const n = Number.parseInt(d)
78
- * if(Number.isNumber(n) && !Number.isNaN(n)) return Result.Ok(n)
79
- *
80
- * return Result.Err(new InvalidNumStringError(d))
81
- * }
82
- * const result1 = r.map(mapper) // Type is Result<Result<number, InvalidNumStringError>, SomeError>
83
- * const result2 = r.flatMap(mapper) // Type is Result<number, SomeError | InvalidNumStringError>
84
- */
85
- flatMap<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Result<Promise<U>, E2>): Result<Promise<U>, E | E2>;
86
- flatMap<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<Promise<U>, E2>>): Result<Promise<U>, E | E2>;
87
- flatMap<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<U, E2>>): Result<Promise<U>, E | E2>;
88
- flatMap<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Result<U, E | E2>): Result<Promise<U>, E | E2>;
89
- flatMap<T, U, E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<Promise<U>, E | E2>>): Result<Promise<U>, E | E2>;
90
- flatMap<T, U, E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<U, E | E2>>): Result<Promise<U>, E | E2>;
91
- flatMap<T, U, E2>(this: Result<T, E>, fn: (val: T) => Result<U, E2>): Result<U, E | E2>;
92
- flatMap<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<Promise<Result<unknown, unknown>>, E2>>): never;
93
- flatMap<T, U, E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<Promise<Result<unknown, unknown>>, E | E2>>): never;
94
- private static flatMapHelper;
95
- private static flatMapInnerHelper;
96
- /**
97
- * For combining the errors only while retaining the original value.
98
- * @example
99
- * const r: Result<string, SomeError> = Result.Ok('42')
100
- * const binderOk = (d: string) => Result.Ok<number, SomeOtherError>(0909)
101
- * const binderErr = (d: string) => Result.Err<number, SomeOtherError>(new SomeOtherError())
102
- * const binderErrAsync = async (d: string) => Result.Err<number, SomeOtherError>(new SomeOtherError())
103
- *
104
- * const res1 = r.zipErr(binderOk) // Result<string, SomeError | SomeOtherError> - Value: Result.Ok('42') - No change in Ok type
105
- * const res2 = r.zipErr(binderErr) // Result<string, SomeError | SomeOtherError> - Value: Result.Err(new SomeOtherError())
106
- * const res3 = r.zipErr(binderErrAsync) // Result<Promise<string>, SomeError | SomeOtherError> - Value: Result.Err(new SomeOtherError())
107
- */
108
- zipErr<E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<unknown, E2>>): Result<Promise<T>, E | E2>;
109
- zipErr<E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<unknown, E2>>): Result<Promise<T>, E | E2>;
110
- zipErr<E2>(this: Result<T, E>, fn: (val: T) => Result<unknown, E2>): Result<T, E | E2>;
111
- private static zipErrHelper;
112
- zip<T, U>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<U>): Result<Promise<[T, U]>, E>;
113
- zip<T, U>(this: Result<Promise<T>, E>, fn: (val: T) => U): Result<Promise<[T, U]>, E>;
114
- zip<T, U>(this: Result<T, E>, fn: (val: T) => Promise<U>): Result<Promise<[T, U]>, E>;
115
- zip<T, U>(this: Result<T, E>, fn: (val: T) => U): Result<[T, U], E>;
116
- /** For combining two results lazily. For the eager eval version, see {@link and} */
117
- flatZip<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<Promise<Result<unknown, unknown>>, E2>>): never;
118
- flatZip<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<Promise<U>, E2>>): Result<Promise<[T, U]>, E | E2>;
119
- flatZip<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Promise<Result<U, E2>>): Result<Promise<[T, U]>, E | E2>;
120
- flatZip<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Result<Promise<U>, E2>): Result<Promise<[T, U]>, E | E2>;
121
- flatZip<T, U, E2>(this: Result<Promise<T>, E>, fn: (val: T) => Result<U, E2>): Result<Promise<[T, U]>, E | E2>;
122
- flatZip<T, U, E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<Promise<Result<unknown, unknown>>, E2>>): never;
123
- flatZip<T, U, E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<Promise<U>, E2>>): Result<Promise<[T, U]>, E | E2>;
124
- flatZip<T, U, E2>(this: Result<T, E>, fn: (val: T) => Promise<Result<U, E2>>): Result<Promise<[T, U]>, E | E2>;
125
- flatZip<T, U, E2>(this: Result<T, E>, fn: (val: T) => Result<Promise<U>, E2>): Result<Promise<[T, U]>, E | E2>;
126
- flatZip<T, U, E2>(this: Result<T, E>, fn: (val: T) => Result<U, E2>): Result<[T, U], E | E2>;
127
- private static flatZipHelper;
128
- private static flatZipInnerHelper;
129
- innerMap<In, E, Out>(this: Result<Array<In>, E>, mapper: (val: NoInfer<In>) => Out): Result<Array<Out>, E>;
130
- toPromise(): Promise<Result<Awaited<T>, E>>;
131
- validate<VE extends unknown[]>(this: Result<T, E>, validators: {
132
- [K in keyof VE]: (val: T) => Result<unknown, VE[K]>;
133
- }): Result<T, E | VE[number][]>;
134
- validate<VE extends unknown[]>(this: Result<T, E>, validators: {
135
- [K in keyof VE]: (val: T) => Promise<Result<unknown, VE[K]>>;
136
- }): Result<Promise<T>, E | VE[number][]>;
137
- validate<VE extends unknown[], In = Awaited<T>>(this: Result<T, E> | Result<Promise<T>, E>, validators: {
138
- [K in keyof VE]: (val: In) => Promise<Result<unknown, VE[K]>> | Result<unknown, VE[K]>;
139
- }): Result<Promise<T>, E | VE[number][]>;
140
- private static validateHelper;
141
- static all<T extends Result<unknown, unknown>[]>(...results: T): HasPromise<T> extends true ? Result<Promise<UnwrapPromises<T>>, CombinedResultErr<T>[]> : Result<CombinedResultOk<T>, CombinedResultErr<T>[]>;
142
- }
143
-
144
- declare class UnwrappedNone extends Error {
145
- constructor();
146
- }
147
- type UnitOption = Option<UNIT>;
148
- type UnwrapOption<T> = T extends Option<infer R> ? R : never;
149
- declare class Option<T> {
150
- #private;
151
- private val;
152
- private constructor();
153
- static readonly None: Option<never>;
154
- private static safeMap;
155
- static Some<Inner>(val: Inner): Option<Inner>;
156
- static fromNullable<T, Out = NonNullable<T>>(val: T): Option<Out>;
157
- isSome(): this is Option<T>;
158
- isNone(): this is Option<never>;
159
- isUnit(): this is Option<UNIT>;
160
- unwrap(): T;
161
- safeUnwrap(): T | null;
162
- map<U, Curr = Awaited<T>>(mapper: (val: Curr) => U): Option<U>;
163
- map<U, Curr = Awaited<T>>(mapper: (val: Curr) => Promise<U>): Option<Promise<U>>;
164
- mapOr<U, Curr = Awaited<T>>(default_: U, fn: (val: Curr) => U): Option<U>;
165
- mapOr<U, Curr = Awaited<T>>(default_: U, fn: (val: Curr) => Promise<U>): Option<Promise<U>>;
166
- flatMap<U, Curr = Awaited<T>>(mapper: (val: Curr) => Option<U>): Option<U>;
167
- flatMap<U, Curr = Awaited<T>>(mapper: (val: Curr) => Promise<Option<U>>): Option<Promise<U>>;
168
- zip<U, Curr>(this: Option<Promise<Curr>>, fn: (val: Curr) => U): Option<Promise<[Curr, U]>>;
169
- zip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => Promise<U>): Option<Promise<[T, U]>>;
170
- zip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => U): Option<[T, U]>;
171
- flatZip<U, Curr>(this: Option<Promise<Curr>>, fn: (val: Curr) => Promise<Option<U>>): Option<Promise<[Curr, U]>>;
172
- flatZip<U, Curr>(this: Option<Promise<Curr>>, fn: (val: Curr) => Option<U>): Option<Promise<[Curr, U]>>;
173
- flatZip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => Promise<Option<U>>): Option<Promise<[T, U]>>;
174
- flatZip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => Option<U>): Option<[T, U]>;
175
- toPromise<Curr = Awaited<T>>(): Promise<Option<Curr>>;
176
- static fromPromise<U>(o: Promise<Option<U>>): Option<Promise<U>>;
177
- toString(): string;
178
- innerMap<Inner, Out>(this: Option<Array<Inner>>, mapper: (val: Inner) => Out): Option<NoInfer<Out>[]>;
179
- }
180
-
181
- declare const matchRes: <T, E, U>(r: Result<T, E>, branches: {
182
- Ok: (val: T) => U;
183
- Err: (err: E) => U;
184
- }) => U;
185
- declare const matchOpt: <T, U>(o: Option<T>, branches: {
186
- Some: (val: T) => U;
187
- None: () => U;
188
- }) => U;
189
-
190
- export { type CombineResults, Option, Result, UNIT, type UnitOption, type UnitResult, type UnwrapOption, type UnwrapResult, UnwrappedErrWithOk, UnwrappedNone, UnwrappedOkWithErr, matchOpt, matchRes };
1
+ import { UNIT$1 as UNIT } from "./unit-CoBLIJFc.js";
2
+ import { CombineResults, Result$1 as Result, UnitResult, UnwrapResult, UnwrappedErrWithOk$1 as UnwrappedErrWithOk, UnwrappedOkWithErr$1 as UnwrappedOkWithErr } from "./result-DsVjSFnO.js";
3
+ import { Option$1 as Option, UnitOption, UnwrapOption, UnwrappedNone$1 as UnwrappedNone } from "./option-CoelyMEg.js";
4
+ import { matchOpt$1 as matchOpt, matchRes$1 as matchRes } from "./match-DXKW7ym5.js";
5
+ export { CombineResults, Option, Result, UNIT, UnitOption, UnitResult, UnwrapOption, UnwrapResult, UnwrappedErrWithOk, UnwrappedNone, UnwrappedOkWithErr, matchOpt, matchRes };