@railway-ts/pipelines 0.1.29 → 0.2.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.
- package/README.md +4 -4
- package/dist/composition/index.d.mts +367 -0
- package/dist/composition/index.mjs +37 -59
- package/dist/composition/index.mjs.map +1 -1
- package/dist/{result/index.d.ts → index-DDkNG41b.d.mts} +63 -138
- package/dist/option/index.d.mts +7 -0
- package/dist/option/index.mjs +1 -116
- package/dist/option-BQCaaEBP.mjs +909 -0
- package/dist/option-BQCaaEBP.mjs.map +1 -0
- package/dist/result/index.d.mts +7 -0
- package/dist/result/index.mjs +1 -187
- package/dist/schema/{index.d.ts → index.d.mts} +227 -258
- package/dist/schema/index.mjs +1646 -982
- package/dist/schema/index.mjs.map +1 -1
- package/docs/GETTING_STARTED.md +1 -1
- package/docs/RECIPES.md +3 -3
- package/package.json +21 -70
- package/dist/composition/index.cjs +0 -94
- package/dist/composition/index.cjs.map +0 -1
- package/dist/composition/index.d.cts +0 -372
- package/dist/composition/index.d.ts +0 -372
- package/dist/option/index.cjs +0 -139
- package/dist/option/index.cjs.map +0 -1
- package/dist/option/index.d.cts +0 -1
- package/dist/option/index.d.ts +0 -1
- package/dist/option/index.mjs.map +0 -1
- package/dist/result/index.cjs +0 -221
- package/dist/result/index.cjs.map +0 -1
- package/dist/result/index.d.cts +0 -1062
- package/dist/result/index.mjs.map +0 -1
- package/dist/schema/index.cjs +0 -1169
- package/dist/schema/index.cjs.map +0 -1
- package/dist/schema/index.d.cts +0 -1265
package/dist/result/index.d.cts
DELETED
|
@@ -1,1062 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents a Result type.
|
|
3
|
-
*
|
|
4
|
-
* @example
|
|
5
|
-
* const result: Result<number, string> = ok(123);
|
|
6
|
-
* const error: Result<number, string> = error("An error occurred");
|
|
7
|
-
*
|
|
8
|
-
* @param T - The type of the value contained in the Ok variant
|
|
9
|
-
* @param E - The type of the error contained in the Error variant
|
|
10
|
-
* @returns A Result containing a value or an error
|
|
11
|
-
*/
|
|
12
|
-
type Result<T, E> = {
|
|
13
|
-
readonly ok: true;
|
|
14
|
-
readonly value: T;
|
|
15
|
-
readonly [RESULT_BRAND]: 'ok';
|
|
16
|
-
} | {
|
|
17
|
-
readonly ok: false;
|
|
18
|
-
readonly error: E;
|
|
19
|
-
readonly [RESULT_BRAND]: 'error';
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* Creates a Result containing a value.
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* const result: Result<number, string> = ok(123);
|
|
26
|
-
*
|
|
27
|
-
* @param value - The value to contain
|
|
28
|
-
* @returns A Result containing the value
|
|
29
|
-
*/
|
|
30
|
-
declare function ok<T, E = never>(value: T): Result<T, E>;
|
|
31
|
-
/**
|
|
32
|
-
* Creates a Result containing an error.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* const error: Result<number, string> = err("An error occurred");
|
|
36
|
-
*
|
|
37
|
-
* @param error - The error to contain
|
|
38
|
-
* @returns A Result containing the error
|
|
39
|
-
*/
|
|
40
|
-
declare function err<E>(error: E): Result<never, E>;
|
|
41
|
-
/**
|
|
42
|
-
* Type guard that checks if a Result is an Ok variant containing a value.
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* const result: Result<number, string> = ok(123);
|
|
46
|
-
* if (isOk(result)) {
|
|
47
|
-
* // TypeScript knows result.value exists here
|
|
48
|
-
* console.log(result.value);
|
|
49
|
-
* }
|
|
50
|
-
*
|
|
51
|
-
* @param result - The result to check
|
|
52
|
-
* @returns A type predicate indicating if the result is an Ok variant
|
|
53
|
-
*/
|
|
54
|
-
declare function isOk<T, E>(result: Result<T, E>): result is {
|
|
55
|
-
readonly ok: true;
|
|
56
|
-
readonly value: T;
|
|
57
|
-
readonly [RESULT_BRAND]: 'ok';
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* Type guard that checks if a Result is an Error variant.
|
|
61
|
-
*
|
|
62
|
-
* @example
|
|
63
|
-
* const result: Result<number, string> = err("error");
|
|
64
|
-
* if (isErr(result)) {
|
|
65
|
-
* // TypeScript knows result.error exists here
|
|
66
|
-
* console.log(result.error);
|
|
67
|
-
* }
|
|
68
|
-
*
|
|
69
|
-
* @param result - The result to check
|
|
70
|
-
* @returns A type predicate indicating if the result is an Error variant
|
|
71
|
-
*/
|
|
72
|
-
declare function isErr<T, E>(result: Result<T, E>): result is {
|
|
73
|
-
readonly ok: false;
|
|
74
|
-
readonly error: E;
|
|
75
|
-
readonly [RESULT_BRAND]: 'error';
|
|
76
|
-
};
|
|
77
|
-
/**
|
|
78
|
-
* Maps the value inside a Result using a transformation function.
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* const result: Result<number, string> = ok(123);
|
|
82
|
-
* const transformed: Result<string, string> = map(result, (value) => value.toString());
|
|
83
|
-
*
|
|
84
|
-
* @param result - The Result to transform
|
|
85
|
-
* @param fn - The function to apply to the contained value
|
|
86
|
-
* @returns A new Result containing the transformed value, or the original error if the input was an error
|
|
87
|
-
*/
|
|
88
|
-
declare function map$1<T, E, U>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
|
|
89
|
-
/**
|
|
90
|
-
* Maps the value inside a Result using a transformation function that returns a Result.
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* const result: Result<number, string> = ok(123);
|
|
94
|
-
* const transformed: Result<string, string> = flatMap(result, (value) => ok(value.toString()));
|
|
95
|
-
*
|
|
96
|
-
* @param result - The Result to transform
|
|
97
|
-
* @param fn - The function to apply to the contained value, returning a Result
|
|
98
|
-
* @returns The Result returned by the transformation function, or the original error if the input was an error
|
|
99
|
-
*/
|
|
100
|
-
declare function flatMap$1<T, U, E1, E2>(result: Result<T, E1>, fn: (value: T) => Result<U, E2>): Result<U, E1 | E2>;
|
|
101
|
-
/**
|
|
102
|
-
* Maps the error inside a Result using a transformation function.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* const result: Result<number, string> = err("error");
|
|
106
|
-
* const transformed: Result<number, Error> = mapErr(result, (err) => new Error(err));
|
|
107
|
-
*
|
|
108
|
-
* @param result - The Result to transform
|
|
109
|
-
* @param fn - The function to apply to the contained error
|
|
110
|
-
* @returns A new Result containing the transformed error, or the original value if the input was ok
|
|
111
|
-
*/
|
|
112
|
-
declare function mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
|
|
113
|
-
/**
|
|
114
|
-
* Maps both the value and error of a Result using separate transformation functions.
|
|
115
|
-
* This is equivalent to chaining map and mapErr, but more concise.
|
|
116
|
-
*
|
|
117
|
-
* @example
|
|
118
|
-
* const result: Result<number, string> = ok(123);
|
|
119
|
-
* const transformed = bimap(
|
|
120
|
-
* result,
|
|
121
|
-
* (value) => value.toString(),
|
|
122
|
-
* (error) => new Error(error)
|
|
123
|
-
* ); // ok("123")
|
|
124
|
-
*
|
|
125
|
-
* const error: Result<number, string> = err("failed");
|
|
126
|
-
* const transformed2 = bimap(
|
|
127
|
-
* error,
|
|
128
|
-
* (value) => value.toString(),
|
|
129
|
-
* (error) => new Error(error)
|
|
130
|
-
* ); // err(Error("failed"))
|
|
131
|
-
*
|
|
132
|
-
* @param result - The Result to transform
|
|
133
|
-
* @param okFn - The function to apply to the contained value if Ok
|
|
134
|
-
* @param errFn - The function to apply to the contained error if Err
|
|
135
|
-
* @returns A new Result with both branches transformed
|
|
136
|
-
*/
|
|
137
|
-
declare function bimap$1<T, E, U, F>(result: Result<T, E>, okFn: (value: T) => U, errFn: (error: E) => F): Result<U, F>;
|
|
138
|
-
/**
|
|
139
|
-
* Filters a Result based on a predicate function.
|
|
140
|
-
*
|
|
141
|
-
* @example
|
|
142
|
-
* const result: Result<number, string> = ok(123);
|
|
143
|
-
* const filtered: Result<number, string> = filter(result, value => value > 100, "Value too small"); // still Ok(123)
|
|
144
|
-
* const filtered2: Result<number, string> = filter(result, value => value > 200, "Value too small"); // Err("Value too small")
|
|
145
|
-
*
|
|
146
|
-
* @param result - The Result to filter
|
|
147
|
-
* @param predicate - A function that determines if the value should be kept
|
|
148
|
-
* @param error - The error to return if the predicate fails
|
|
149
|
-
* @returns The original Result if it contains a value that satisfies the predicate, otherwise an Error
|
|
150
|
-
*/
|
|
151
|
-
declare function filter$1<T, E1, E2>(result: Result<T, E1>, predicate: (value: T) => boolean, error: E2): Result<T, E1 | E2>;
|
|
152
|
-
/**
|
|
153
|
-
* Executes a callback with the value if the Result is Ok, without changing the Result.
|
|
154
|
-
* Useful for side effects like logging while maintaining a processing chain.
|
|
155
|
-
*
|
|
156
|
-
* @example
|
|
157
|
-
* const finalResult = pipe(
|
|
158
|
-
* ok(123),
|
|
159
|
-
* r => map(r, x => x * 2),
|
|
160
|
-
* r => tap(r, x => console.log(`Value: ${x}`)), // Logs but doesn't change the Result
|
|
161
|
-
* r => filter(r, x => x > 200, "Value too small")
|
|
162
|
-
* );
|
|
163
|
-
*
|
|
164
|
-
* @param result - The Result to tap into
|
|
165
|
-
* @param fn - The function to execute with the value if Ok
|
|
166
|
-
* @returns The original Result unchanged
|
|
167
|
-
*/
|
|
168
|
-
declare function tap$1<T, E>(result: Result<T, E>, fn: (value: T) => void): Result<T, E>;
|
|
169
|
-
/**
|
|
170
|
-
* Executes a callback with the error if the Result is an Error, without changing the Result.
|
|
171
|
-
* Useful for side effects like logging while maintaining a processing chain.
|
|
172
|
-
*
|
|
173
|
-
* @example
|
|
174
|
-
* const finalResult = pipe(
|
|
175
|
-
* err("something went wrong"),
|
|
176
|
-
* r => tapErr(r, e => console.error(`Error: ${e}`)), // Logs but doesn't change the Result
|
|
177
|
-
* r => mapErr(r, e => new Error(e))
|
|
178
|
-
* );
|
|
179
|
-
*
|
|
180
|
-
* @param result - The Result to tap into
|
|
181
|
-
* @param fn - The function to execute with the error if Error
|
|
182
|
-
* @returns The original Result unchanged
|
|
183
|
-
*/
|
|
184
|
-
declare function tapErr<T, E>(result: Result<T, E>, fn: (error: E) => void): Result<T, E>;
|
|
185
|
-
/**
|
|
186
|
-
* Recovers from an Err by applying `fn` to the error value. Returns the original Ok unchanged.
|
|
187
|
-
* This is the error-channel equivalent of `flatMap`.
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* const result: Result<number, string> = err("not found");
|
|
191
|
-
* const recovered = orElse(result, (e) => ok(e.length)); // ok(9)
|
|
192
|
-
*
|
|
193
|
-
* const okResult: Result<number, string> = ok(42);
|
|
194
|
-
* const kept = orElse(okResult, (e) => ok(e.length)); // ok(42)
|
|
195
|
-
*
|
|
196
|
-
* @param result - The Result to recover from
|
|
197
|
-
* @param fn - A function that receives the Err value and returns a fallback Result
|
|
198
|
-
* @returns The original Ok, or the Result of applying fn to the error
|
|
199
|
-
*/
|
|
200
|
-
declare function orElse<T1, E1, T2, E2>(result: Result<T1, E1>, fn: (error: E1) => Result<T2, E2>): Result<T1 | T2, E2>;
|
|
201
|
-
/**
|
|
202
|
-
* Pattern matches on a Result to handle both Ok and Error cases.
|
|
203
|
-
*
|
|
204
|
-
* @example
|
|
205
|
-
* const result = ok(42);
|
|
206
|
-
* const message = match(result, {
|
|
207
|
-
* ok: (value) => `Got value: ${value}`,
|
|
208
|
-
* err: (error) => `Got error: ${error}`
|
|
209
|
-
* }); // "Got value: 42"
|
|
210
|
-
*
|
|
211
|
-
* @param result - The Result to match against
|
|
212
|
-
* @param patterns - An object containing handler functions for Ok and Error cases
|
|
213
|
-
* @returns The result of calling the appropriate handler function
|
|
214
|
-
*/
|
|
215
|
-
declare function match$1<T, E, R1, R2>(result: Result<T, E>, patterns: {
|
|
216
|
-
ok: (value: T) => R1;
|
|
217
|
-
err: (error: E) => R2;
|
|
218
|
-
}): R1 | R2;
|
|
219
|
-
/**
|
|
220
|
-
* Unwraps the value inside a Result, throwing an error if the Result is an error.
|
|
221
|
-
*
|
|
222
|
-
* @remarks
|
|
223
|
-
* This function is intended for prototyping only and should not be used in production.
|
|
224
|
-
* In production code, prefer using pattern matching or `unwrapOr` to handle both Ok and Error cases safely.
|
|
225
|
-
*
|
|
226
|
-
* @example
|
|
227
|
-
* const result: Result<number, string> = ok(123);
|
|
228
|
-
* const value: number = unwrap(result);
|
|
229
|
-
*
|
|
230
|
-
* // With custom error message
|
|
231
|
-
* try {
|
|
232
|
-
* const badResult: Result<number, string> = err("Invalid data");
|
|
233
|
-
* unwrap(badResult, "Custom error message");
|
|
234
|
-
* } catch (error) {
|
|
235
|
-
* // Error will contain "Custom error message" instead of the default
|
|
236
|
-
* }
|
|
237
|
-
*
|
|
238
|
-
* @param result - The Result to unwrap
|
|
239
|
-
* @param errorMsg - Optional custom error message to use if Result is an error
|
|
240
|
-
* @returns The contained value
|
|
241
|
-
* @throws If the Result is an error
|
|
242
|
-
*/
|
|
243
|
-
declare function unwrap$1<T, E>(result: Result<T, E>, errorMsg?: string): T;
|
|
244
|
-
/**
|
|
245
|
-
* Unwraps the value inside a Result, providing a default value if the Result is an error.
|
|
246
|
-
*
|
|
247
|
-
* @example
|
|
248
|
-
* const result: Result<number, string> = err("error");
|
|
249
|
-
* const value: number = unwrapOr(result, 123);
|
|
250
|
-
*
|
|
251
|
-
* @param result - The Result to unwrap
|
|
252
|
-
* @param defaultValue - The value to return if the Result is an error
|
|
253
|
-
* @returns The contained value, or the default value if the Result is an error
|
|
254
|
-
*/
|
|
255
|
-
declare function unwrapOr$1<T, E>(result: Result<T, E>, defaultValue: T): T;
|
|
256
|
-
/**
|
|
257
|
-
* Unwraps the value inside a Result, calling a function to generate a default value if the Result is an error.
|
|
258
|
-
* Unlike unwrapOr, this only computes the default when needed.
|
|
259
|
-
*
|
|
260
|
-
* @example
|
|
261
|
-
* const result: Result<number, string> = err("error");
|
|
262
|
-
* const value = unwrapOrElse(result, () => expensiveComputation());
|
|
263
|
-
*
|
|
264
|
-
* @param result - The Result to unwrap
|
|
265
|
-
* @param defaultFn - A function that returns a default value if the Result is an error
|
|
266
|
-
* @returns The contained value, or the result of calling defaultFn if the Result is an error
|
|
267
|
-
*/
|
|
268
|
-
declare function unwrapOrElse$1<T, E>(result: Result<T, E>, defaultFn: () => T): T;
|
|
269
|
-
/**
|
|
270
|
-
* Combines an array of Results into a single Result containing an array of values.
|
|
271
|
-
* Returns an Error if any Result in the array is an Error, using the first encountered error.
|
|
272
|
-
* If an empty array is provided, returns `ok([])` (an Ok containing an empty array).
|
|
273
|
-
*
|
|
274
|
-
* @example
|
|
275
|
-
* const results = [ok(1), ok(2), ok(3)];
|
|
276
|
-
* const combined = combine(results); // ok([1, 2, 3])
|
|
277
|
-
*
|
|
278
|
-
* const withError = [ok(1), err("error"), ok(3)];
|
|
279
|
-
* const result = combine(withError); // err("error")
|
|
280
|
-
*
|
|
281
|
-
* const emptyResults = [];
|
|
282
|
-
* const emptyResult = combine(emptyResults); // ok([])
|
|
283
|
-
*
|
|
284
|
-
* @param results - An array of Results to combine
|
|
285
|
-
* @returns A Result containing an array of all values if all inputs are Ok, or the first Error if any input is an Error
|
|
286
|
-
*/
|
|
287
|
-
declare function combine$1<T1, E1>(results: readonly [Result<T1, E1>]): Result<[T1], E1>;
|
|
288
|
-
declare function combine$1<T1, E1, T2, E2>(results: readonly [Result<T1, E1>, Result<T2, E2>]): Result<[T1, T2], E1 | E2>;
|
|
289
|
-
declare function combine$1<T1, E1, T2, E2, T3, E3>(results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>]): Result<[T1, T2, T3], E1 | E2 | E3>;
|
|
290
|
-
declare function combine$1<T1, E1, T2, E2, T3, E3, T4, E4>(results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>]): Result<[T1, T2, T3, T4], E1 | E2 | E3 | E4>;
|
|
291
|
-
declare function combine$1<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5>(results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>, Result<T5, E5>]): Result<[T1, T2, T3, T4, T5], E1 | E2 | E3 | E4 | E5>;
|
|
292
|
-
declare function combine$1<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6>(results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>, Result<T5, E5>, Result<T6, E6>]): Result<[T1, T2, T3, T4, T5, T6], E1 | E2 | E3 | E4 | E5 | E6>;
|
|
293
|
-
declare function combine$1<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7>(results: readonly [
|
|
294
|
-
Result<T1, E1>,
|
|
295
|
-
Result<T2, E2>,
|
|
296
|
-
Result<T3, E3>,
|
|
297
|
-
Result<T4, E4>,
|
|
298
|
-
Result<T5, E5>,
|
|
299
|
-
Result<T6, E6>,
|
|
300
|
-
Result<T7, E7>
|
|
301
|
-
]): Result<[T1, T2, T3, T4, T5, T6, T7], E1 | E2 | E3 | E4 | E5 | E6 | E7>;
|
|
302
|
-
declare function combine$1<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8>(results: readonly [
|
|
303
|
-
Result<T1, E1>,
|
|
304
|
-
Result<T2, E2>,
|
|
305
|
-
Result<T3, E3>,
|
|
306
|
-
Result<T4, E4>,
|
|
307
|
-
Result<T5, E5>,
|
|
308
|
-
Result<T6, E6>,
|
|
309
|
-
Result<T7, E7>,
|
|
310
|
-
Result<T8, E8>
|
|
311
|
-
]): Result<[T1, T2, T3, T4, T5, T6, T7, T8], E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8>;
|
|
312
|
-
declare function combine$1<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8, T9, E9>(results: readonly [
|
|
313
|
-
Result<T1, E1>,
|
|
314
|
-
Result<T2, E2>,
|
|
315
|
-
Result<T3, E3>,
|
|
316
|
-
Result<T4, E4>,
|
|
317
|
-
Result<T5, E5>,
|
|
318
|
-
Result<T6, E6>,
|
|
319
|
-
Result<T7, E7>,
|
|
320
|
-
Result<T8, E8>,
|
|
321
|
-
Result<T9, E9>
|
|
322
|
-
]): Result<[T1, T2, T3, T4, T5, T6, T7, T8, T9], E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9>;
|
|
323
|
-
declare function combine$1<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8, T9, E9, T10, E10>(results: readonly [
|
|
324
|
-
Result<T1, E1>,
|
|
325
|
-
Result<T2, E2>,
|
|
326
|
-
Result<T3, E3>,
|
|
327
|
-
Result<T4, E4>,
|
|
328
|
-
Result<T5, E5>,
|
|
329
|
-
Result<T6, E6>,
|
|
330
|
-
Result<T7, E7>,
|
|
331
|
-
Result<T8, E8>,
|
|
332
|
-
Result<T9, E9>,
|
|
333
|
-
Result<T10, E10>
|
|
334
|
-
]): Result<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9 | E10>;
|
|
335
|
-
declare function combine$1<T, E>(results: readonly Result<T, E>[]): Result<T[], E>;
|
|
336
|
-
/**
|
|
337
|
-
* Combines an array of Results into a single Result containing an array of values.
|
|
338
|
-
* Unlike combine, this collects all errors instead of returning just the first one.
|
|
339
|
-
* If all Results are Ok, returns an Ok containing an array of all values.
|
|
340
|
-
* If any Results are Error, returns an Error containing an array of all errors.
|
|
341
|
-
*
|
|
342
|
-
* @example
|
|
343
|
-
* const results = [ok(1), ok(2), ok(3)];
|
|
344
|
-
* const combined = combineAll(results); // ok([1, 2, 3])
|
|
345
|
-
*
|
|
346
|
-
* const withErrors = [ok(1), err("error1"), ok(3), err("error2")];
|
|
347
|
-
* const result = combineAll(withErrors); // err(["error1", "error2"])
|
|
348
|
-
*
|
|
349
|
-
* const emptyResults = [];
|
|
350
|
-
* const emptyResult = combineAll(emptyResults); // ok([])
|
|
351
|
-
*
|
|
352
|
-
* @param results - An array of Results to combine
|
|
353
|
-
* @returns A Result containing an array of all values if all inputs are Ok, or an array of all errors if any input is an Error
|
|
354
|
-
*/
|
|
355
|
-
declare function combineAll<T1, E1>(results: readonly [Result<T1, E1>]): Result<[T1], E1[]>;
|
|
356
|
-
declare function combineAll<T1, E1, T2, E2>(results: readonly [Result<T1, E1>, Result<T2, E2>]): Result<[T1, T2], (E1 | E2)[]>;
|
|
357
|
-
declare function combineAll<T1, E1, T2, E2, T3, E3>(results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>]): Result<[T1, T2, T3], (E1 | E2 | E3)[]>;
|
|
358
|
-
declare function combineAll<T1, E1, T2, E2, T3, E3, T4, E4>(results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>]): Result<[T1, T2, T3, T4], (E1 | E2 | E3 | E4)[]>;
|
|
359
|
-
declare function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5>(results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>, Result<T5, E5>]): Result<[T1, T2, T3, T4, T5], (E1 | E2 | E3 | E4 | E5)[]>;
|
|
360
|
-
declare function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6>(results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>, Result<T5, E5>, Result<T6, E6>]): Result<[T1, T2, T3, T4, T5, T6], (E1 | E2 | E3 | E4 | E5 | E6)[]>;
|
|
361
|
-
declare function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7>(results: readonly [
|
|
362
|
-
Result<T1, E1>,
|
|
363
|
-
Result<T2, E2>,
|
|
364
|
-
Result<T3, E3>,
|
|
365
|
-
Result<T4, E4>,
|
|
366
|
-
Result<T5, E5>,
|
|
367
|
-
Result<T6, E6>,
|
|
368
|
-
Result<T7, E7>
|
|
369
|
-
]): Result<[T1, T2, T3, T4, T5, T6, T7], (E1 | E2 | E3 | E4 | E5 | E6 | E7)[]>;
|
|
370
|
-
declare function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8>(results: readonly [
|
|
371
|
-
Result<T1, E1>,
|
|
372
|
-
Result<T2, E2>,
|
|
373
|
-
Result<T3, E3>,
|
|
374
|
-
Result<T4, E4>,
|
|
375
|
-
Result<T5, E5>,
|
|
376
|
-
Result<T6, E6>,
|
|
377
|
-
Result<T7, E7>,
|
|
378
|
-
Result<T8, E8>
|
|
379
|
-
]): Result<[T1, T2, T3, T4, T5, T6, T7, T8], (E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8)[]>;
|
|
380
|
-
declare function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8, T9, E9>(results: readonly [
|
|
381
|
-
Result<T1, E1>,
|
|
382
|
-
Result<T2, E2>,
|
|
383
|
-
Result<T3, E3>,
|
|
384
|
-
Result<T4, E4>,
|
|
385
|
-
Result<T5, E5>,
|
|
386
|
-
Result<T6, E6>,
|
|
387
|
-
Result<T7, E7>,
|
|
388
|
-
Result<T8, E8>,
|
|
389
|
-
Result<T9, E9>
|
|
390
|
-
]): Result<[T1, T2, T3, T4, T5, T6, T7, T8, T9], (E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9)[]>;
|
|
391
|
-
declare function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8, T9, E9, T10, E10>(results: readonly [
|
|
392
|
-
Result<T1, E1>,
|
|
393
|
-
Result<T2, E2>,
|
|
394
|
-
Result<T3, E3>,
|
|
395
|
-
Result<T4, E4>,
|
|
396
|
-
Result<T5, E5>,
|
|
397
|
-
Result<T6, E6>,
|
|
398
|
-
Result<T7, E7>,
|
|
399
|
-
Result<T8, E8>,
|
|
400
|
-
Result<T9, E9>,
|
|
401
|
-
Result<T10, E10>
|
|
402
|
-
]): Result<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], (E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9 | E10)[]>;
|
|
403
|
-
declare function combineAll<T, E>(results: readonly Result<T, E>[]): Result<T[], E[]>;
|
|
404
|
-
/**
|
|
405
|
-
* Separates an array of Results into successes and failures.
|
|
406
|
-
* Unlike `combineAll` which is all-or-nothing, `partition` preserves both sides —
|
|
407
|
-
* useful for batch processing where partial success is meaningful.
|
|
408
|
-
*
|
|
409
|
-
* @example
|
|
410
|
-
* const results = [ok(1), err('bad'), ok(3), err('worse')];
|
|
411
|
-
* partition(results);
|
|
412
|
-
* // { successes: [1, 3], failures: ['bad', 'worse'] }
|
|
413
|
-
*/
|
|
414
|
-
declare const partition: <T, E>(results: Result<T, E>[]) => {
|
|
415
|
-
successes: T[];
|
|
416
|
-
failures: E[];
|
|
417
|
-
};
|
|
418
|
-
/**
|
|
419
|
-
* Converts a Result to an Option.
|
|
420
|
-
* If the Result is Ok, returns a Some variant with the value.
|
|
421
|
-
* If the Result is an Error, returns a None variant.
|
|
422
|
-
*
|
|
423
|
-
* @example
|
|
424
|
-
* import { some, none } from "@/option";
|
|
425
|
-
*
|
|
426
|
-
* const option1 = mapToOption(ok(123)); // some(123)
|
|
427
|
-
* const option2 = mapToOption(err("error")); // none()
|
|
428
|
-
*
|
|
429
|
-
* @param result - The Result to convert
|
|
430
|
-
* @returns An Option containing the value if Ok, or None if Error
|
|
431
|
-
*/
|
|
432
|
-
declare function mapToOption<T, E>(result: Result<T, E>): Option<T>;
|
|
433
|
-
/**
|
|
434
|
-
* Safely executes a function that might throw and converts the result into a Result type.
|
|
435
|
-
* If the function executes successfully, returns an Ok variant with the return value.
|
|
436
|
-
* If the function throws an error, returns an Error variant containing the error message as a string.
|
|
437
|
-
*
|
|
438
|
-
* @example
|
|
439
|
-
* const parseJson = (str: string) => fromTry(() => JSON.parse(str));
|
|
440
|
-
*
|
|
441
|
-
* const validResult = parseJson('{"name":"John"}'); // ok({ name: 'John' })
|
|
442
|
-
* const invalidResult = parseJson('invalid json'); // err("Unexpected token...")
|
|
443
|
-
*
|
|
444
|
-
* @param f - The function to execute
|
|
445
|
-
* @returns A Result containing either the function's return value or the error message
|
|
446
|
-
*/
|
|
447
|
-
declare function fromTry<T>(f: () => T): Result<T, string>;
|
|
448
|
-
/**
|
|
449
|
-
* Safely executes a function that might throw and converts the result into a Result type.
|
|
450
|
-
* Unlike fromTry, this preserves the full Error object instead of just the message.
|
|
451
|
-
* Use this when you need access to error stack traces or custom error properties.
|
|
452
|
-
*
|
|
453
|
-
* @example
|
|
454
|
-
* const result = fromTryWithError(() => JSON.parse('invalid'));
|
|
455
|
-
* if (isErr(result)) {
|
|
456
|
-
* console.log(result.error.stack); // Access to full stack trace
|
|
457
|
-
* }
|
|
458
|
-
*
|
|
459
|
-
* @param f - The function to execute
|
|
460
|
-
* @returns A Result containing either the function's return value or the full Error object
|
|
461
|
-
*/
|
|
462
|
-
declare function fromTryWithError<T>(f: () => T): Result<T, Error>;
|
|
463
|
-
/**
|
|
464
|
-
* Converts a Promise to a Result, capturing any errors that occur during promise resolution.
|
|
465
|
-
* Returns error messages as strings for simplicity.
|
|
466
|
-
*
|
|
467
|
-
* @example
|
|
468
|
-
* const result = await fromPromise(fetch('https://api.example.com/data'));
|
|
469
|
-
* if (isErr(result)) {
|
|
470
|
-
* console.log(result.error); // Error message as string
|
|
471
|
-
* }
|
|
472
|
-
*
|
|
473
|
-
* @param promise - The Promise to convert
|
|
474
|
-
* @returns A Promise that resolves to a Result with string error messages
|
|
475
|
-
*/
|
|
476
|
-
declare function fromPromise<T>(promise: Promise<T>): Promise<Result<T, string>>;
|
|
477
|
-
/**
|
|
478
|
-
* Converts a Promise to a Result, preserving the full error object or custom error type.
|
|
479
|
-
* Use this when you need access to error stack traces, custom properties, or specific error types.
|
|
480
|
-
*
|
|
481
|
-
* @example
|
|
482
|
-
* // With default error handling (preserves full error)
|
|
483
|
-
* const result = await fromPromiseWithError(fetch('https://api.example.com/data'));
|
|
484
|
-
*
|
|
485
|
-
* @example
|
|
486
|
-
* // With specific error type and transformer
|
|
487
|
-
* type ApiError = { code: number; message: string };
|
|
488
|
-
*
|
|
489
|
-
* const result = await fromPromiseWithError<Response, ApiError>(
|
|
490
|
-
* fetch('https://api.example.com/data'),
|
|
491
|
-
* (error) => ({
|
|
492
|
-
* code: error instanceof Error ? 500 : 400,
|
|
493
|
-
* message: error instanceof Error ? error.message : String(error)
|
|
494
|
-
* })
|
|
495
|
-
* );
|
|
496
|
-
*
|
|
497
|
-
* @param promise - The Promise to convert
|
|
498
|
-
* @param errorFn - Optional function to transform caught errors to the expected error type
|
|
499
|
-
* @returns A Promise that resolves to a Result
|
|
500
|
-
*/
|
|
501
|
-
declare function fromPromiseWithError<T, E = unknown>(promise: Promise<T>, errorFn?: (error: unknown) => E): Promise<Result<T, E>>;
|
|
502
|
-
/**
|
|
503
|
-
* Converts a Result to a Promise.
|
|
504
|
-
*
|
|
505
|
-
* @example
|
|
506
|
-
* const result = ok(123);
|
|
507
|
-
* const promise = toPromise(result);
|
|
508
|
-
* const value = await promise; // 123
|
|
509
|
-
*
|
|
510
|
-
* @param result - The Result to convert
|
|
511
|
-
* @returns A Promise that resolves to the value if Ok, or rejects with the error if Error
|
|
512
|
-
*/
|
|
513
|
-
declare function toPromise<T, E>(result: Result<T, E>): Promise<T>;
|
|
514
|
-
/**
|
|
515
|
-
* Curried version of `map`. Returns a function that transforms the Ok value using `fn`,
|
|
516
|
-
* passing through any Err unchanged.
|
|
517
|
-
*
|
|
518
|
-
* @example
|
|
519
|
-
* const double = mapWith((n: number) => n * 2);
|
|
520
|
-
*
|
|
521
|
-
* const result: Result<number, string> = ok(5);
|
|
522
|
-
* const doubled: Result<number, string> = double(result); // ok(10)
|
|
523
|
-
*
|
|
524
|
-
* const errResult: Result<number, string> = err("fail");
|
|
525
|
-
* const stillErr: Result<number, string> = double(errResult); // err("fail")
|
|
526
|
-
*
|
|
527
|
-
* // Point-free in a pipe
|
|
528
|
-
* const result2 = pipe(ok(3), mapWith((n: number) => n + 1)); // ok(4)
|
|
529
|
-
*
|
|
530
|
-
* @param fn - The function to apply to the Ok value
|
|
531
|
-
* @returns A function that takes a Result and returns a new Result with the Ok value transformed
|
|
532
|
-
*/
|
|
533
|
-
declare function mapWith$1<T, U>(fn: (value: T) => U): <E>(result: Result<T, E>) => Result<U, E>;
|
|
534
|
-
/**
|
|
535
|
-
* Curried version of `flatMap`. Returns a function that chains on an Ok value using `fn`,
|
|
536
|
-
* passing through any Err unchanged. Supports both sync and async step functions.
|
|
537
|
-
*
|
|
538
|
-
* @example
|
|
539
|
-
* const toStr = flatMapWith((n: number) => ok(n.toString()));
|
|
540
|
-
*
|
|
541
|
-
* const result: Result<number, string> = ok(5);
|
|
542
|
-
* const chained: Result<string, string> = toStr(result); // ok("5")
|
|
543
|
-
*
|
|
544
|
-
* const errResult: Result<number, string> = err("fail");
|
|
545
|
-
* const stillErr: Result<string, string> = toStr(errResult); // err("fail")
|
|
546
|
-
*
|
|
547
|
-
* // Async step function in a pipe
|
|
548
|
-
* const fetchUser = flatMapWith(async (id: number) => {
|
|
549
|
-
* const user = await getUser(id);
|
|
550
|
-
* return user ? ok(user) : err("not found" as const);
|
|
551
|
-
* });
|
|
552
|
-
*
|
|
553
|
-
* @param fn - The function to apply to the Ok value, returning a Result or Promise<Result>
|
|
554
|
-
* @returns A function that takes a Result and returns a (possibly async) Result
|
|
555
|
-
*/
|
|
556
|
-
declare function flatMapWith$1<T, U, E2>(fn: (value: T) => Promise<Result<U, E2>>): <E1>(result: Result<T, E1>) => Promise<Result<U, E1 | E2>>;
|
|
557
|
-
declare function flatMapWith$1<T, U, E2>(fn: (value: T) => Result<U, E2>): <E1>(result: Result<T, E1>) => Result<U, E1 | E2>;
|
|
558
|
-
/**
|
|
559
|
-
* Curried version of `mapErr`. Returns a function that transforms the Err value using `fn`,
|
|
560
|
-
* passing through any Ok unchanged.
|
|
561
|
-
*
|
|
562
|
-
* @example
|
|
563
|
-
* const wrapError = mapErrWith((msg: string) => new Error(msg));
|
|
564
|
-
*
|
|
565
|
-
* const result: Result<number, string> = err("fail");
|
|
566
|
-
* const wrapped: Result<number, Error> = wrapError(result); // err(Error("fail"))
|
|
567
|
-
*
|
|
568
|
-
* const okResult: Result<number, string> = ok(42);
|
|
569
|
-
* const stillOk: Result<number, Error> = wrapError(okResult); // ok(42)
|
|
570
|
-
*
|
|
571
|
-
* // Point-free in a pipe
|
|
572
|
-
* const result2 = pipe(err("oops"), mapErrWith((s: string) => s.toUpperCase())); // err("OOPS")
|
|
573
|
-
*
|
|
574
|
-
* @param fn - The function to apply to the Err value
|
|
575
|
-
* @returns A function that takes a Result and returns a new Result with the Err value transformed
|
|
576
|
-
*/
|
|
577
|
-
declare function mapErrWith<E, F>(fn: (error: E) => F): <T>(result: Result<T, E>) => Result<T, F>;
|
|
578
|
-
/**
|
|
579
|
-
* Curried version of `filter`. Returns a function that keeps an Ok value matching the predicate,
|
|
580
|
-
* or returns the provided error if the predicate fails.
|
|
581
|
-
*
|
|
582
|
-
* @example
|
|
583
|
-
* const isPositive = filterWith((n: number) => n > 0, "must be positive");
|
|
584
|
-
*
|
|
585
|
-
* const result: Result<number, string> = ok(5);
|
|
586
|
-
* const filtered: Result<number, string> = isPositive(result); // ok(5)
|
|
587
|
-
*
|
|
588
|
-
* const zero: Result<number, string> = ok(-1);
|
|
589
|
-
* const failed: Result<number, string> = isPositive(zero); // err("must be positive")
|
|
590
|
-
*
|
|
591
|
-
* // Point-free in a pipe
|
|
592
|
-
* const result2 = pipe(ok(10), filterWith((n: number) => n < 100, "too large")); // ok(10)
|
|
593
|
-
*
|
|
594
|
-
* @param predicate - A function that determines if the Ok value should be kept
|
|
595
|
-
* @param error - The error to return if the predicate fails
|
|
596
|
-
* @returns A function that takes a Result and returns the original Result or an Err
|
|
597
|
-
*/
|
|
598
|
-
declare function filterWith$1<T, E2>(predicate: (value: T) => boolean, error: E2): <E1>(result: Result<T, E1>) => Result<T, E1 | E2>;
|
|
599
|
-
/**
|
|
600
|
-
* Curried version of `tap`. Returns a function that performs a side effect on the Ok value
|
|
601
|
-
* without changing the Result. Err values pass through untouched. Supports both sync and async
|
|
602
|
-
* side effects.
|
|
603
|
-
*
|
|
604
|
-
* @example
|
|
605
|
-
* const log = tapWith((n: number) => console.log("value:", n));
|
|
606
|
-
*
|
|
607
|
-
* const result: Result<number, string> = ok(5);
|
|
608
|
-
* const same: Result<number, string> = log(result); // logs "value: 5", returns ok(5)
|
|
609
|
-
*
|
|
610
|
-
* const errResult: Result<number, string> = err("fail");
|
|
611
|
-
* const stillErr: Result<number, string> = log(errResult); // no log, returns err("fail")
|
|
612
|
-
*
|
|
613
|
-
* // Async side effect in a pipe
|
|
614
|
-
* const audit = tapWith(async (user: User) => {
|
|
615
|
-
* await saveAuditLog(user.id);
|
|
616
|
-
* });
|
|
617
|
-
*
|
|
618
|
-
* @param fn - The side-effect function to execute on the Ok value
|
|
619
|
-
* @returns A function that takes a Result, performs the side effect if Ok, and returns the original Result
|
|
620
|
-
*/
|
|
621
|
-
declare function tapWith$1<T>(fn: (value: T) => Promise<void>): <E>(result: Result<T, E>) => Promise<Result<T, E>>;
|
|
622
|
-
declare function tapWith$1<T>(fn: (value: T) => void): <E>(result: Result<T, E>) => Result<T, E>;
|
|
623
|
-
/**
|
|
624
|
-
* Curried version of `tapErr`. Returns a function that performs a side effect on the Err value
|
|
625
|
-
* without changing the Result. Ok values pass through untouched.
|
|
626
|
-
*
|
|
627
|
-
* @example
|
|
628
|
-
* const logErr = tapErrWith((msg: string) => console.error("error:", msg));
|
|
629
|
-
*
|
|
630
|
-
* const errResult: Result<number, string> = err("fail");
|
|
631
|
-
* const same: Result<number, string> = logErr(errResult); // logs "error: fail", returns err("fail")
|
|
632
|
-
*
|
|
633
|
-
* const okResult: Result<number, string> = ok(42);
|
|
634
|
-
* const stillOk: Result<number, string> = logErr(okResult); // no log, returns ok(42)
|
|
635
|
-
*
|
|
636
|
-
* // Point-free in a pipe
|
|
637
|
-
* const result = pipe(err("oops"), tapErrWith((e: string) => reportError(e)));
|
|
638
|
-
*
|
|
639
|
-
* // Async side effect in a pipe
|
|
640
|
-
* const audit = tapErrWith(async (error: string) => {
|
|
641
|
-
* await logErrorToService(error);
|
|
642
|
-
* });
|
|
643
|
-
*
|
|
644
|
-
* @param fn - The side-effect function to execute on the Err value
|
|
645
|
-
* @returns A function that takes a Result, performs the side effect if Err, and returns the original Result
|
|
646
|
-
*/
|
|
647
|
-
declare function tapErrWith<E>(fn: (error: E) => Promise<void>): <T>(result: Result<T, E>) => Promise<Result<T, E>>;
|
|
648
|
-
declare function tapErrWith<E>(fn: (error: E) => void): <T>(result: Result<T, E>) => Result<T, E>;
|
|
649
|
-
/**
|
|
650
|
-
* Curried version of `orElse`. Recovers from an Err by applying `fn` to the error value;
|
|
651
|
-
* passes through Ok unchanged.
|
|
652
|
-
*
|
|
653
|
-
* @example
|
|
654
|
-
* const recover = orElseWith((msg: string) => ok(msg.length));
|
|
655
|
-
*
|
|
656
|
-
* const errResult: Result<number, string> = err("fail");
|
|
657
|
-
* const recovered: Result<number, string> = recover(errResult); // ok(4)
|
|
658
|
-
*
|
|
659
|
-
* const okResult: Result<number, string> = ok(42);
|
|
660
|
-
* const kept: Result<number, string> = recover(okResult); // ok(42)
|
|
661
|
-
*
|
|
662
|
-
* // Point-free in a pipe — recover using the error
|
|
663
|
-
* const result = pipe(
|
|
664
|
-
* err("not found"),
|
|
665
|
-
* orElseWith((e: string) => ok(`recovered from: ${e}`))
|
|
666
|
-
* ); // ok("recovered from: not found")
|
|
667
|
-
*
|
|
668
|
-
* @param fn - A function that receives the Err value and returns a fallback Result
|
|
669
|
-
* @returns A function that takes a Result and returns either the original Ok or the result of calling fn
|
|
670
|
-
*/
|
|
671
|
-
declare function orElseWith<E1, T2, E2>(fn: (error: E1) => Result<T2, E2>): <T1>(result: Result<T1, E1>) => Result<T1 | T2, E2>;
|
|
672
|
-
|
|
673
|
-
/**
|
|
674
|
-
* Represents an Option type.
|
|
675
|
-
*
|
|
676
|
-
* @example
|
|
677
|
-
* const some: Option<number> = some(123);
|
|
678
|
-
* const none: Option<number> = none();
|
|
679
|
-
*
|
|
680
|
-
* @param T - The type of the value contained in the Some variant
|
|
681
|
-
* @returns An Option containing a value or nothing
|
|
682
|
-
*/
|
|
683
|
-
type Option<T> = {
|
|
684
|
-
readonly some: true;
|
|
685
|
-
readonly value: T;
|
|
686
|
-
readonly [OPTION_BRAND]: 'some';
|
|
687
|
-
} | {
|
|
688
|
-
readonly some: false;
|
|
689
|
-
readonly [OPTION_BRAND]: 'none';
|
|
690
|
-
};
|
|
691
|
-
/**
|
|
692
|
-
* Creates an Option containing a value.
|
|
693
|
-
*
|
|
694
|
-
* @param value - The value to contain
|
|
695
|
-
* @returns An Option containing the value
|
|
696
|
-
*/
|
|
697
|
-
declare function some<T>(value: T): Option<T>;
|
|
698
|
-
/**
|
|
699
|
-
* Creates an Option containing nothing.
|
|
700
|
-
*
|
|
701
|
-
* @example
|
|
702
|
-
* const some: Option<number> = some(123);
|
|
703
|
-
* const none: Option<number> = none();
|
|
704
|
-
*
|
|
705
|
-
* @returns An Option containing nothing
|
|
706
|
-
*/
|
|
707
|
-
declare function none<T = never>(): Option<T>;
|
|
708
|
-
/**
|
|
709
|
-
* Type guard that checks if an Option is a Some variant containing a value.
|
|
710
|
-
*
|
|
711
|
-
* @example
|
|
712
|
-
* const option: Option<number> = some(123);
|
|
713
|
-
* if (isSome(option)) {
|
|
714
|
-
* // TypeScript knows option.value exists here
|
|
715
|
-
* console.log(option.value);
|
|
716
|
-
* }
|
|
717
|
-
*
|
|
718
|
-
* @param option - The option to check
|
|
719
|
-
* @returns A type predicate indicating if the option is a Some variant
|
|
720
|
-
*/
|
|
721
|
-
declare function isSome<T>(option: Option<T>): option is {
|
|
722
|
-
readonly some: true;
|
|
723
|
-
readonly value: T;
|
|
724
|
-
readonly [OPTION_BRAND]: 'some';
|
|
725
|
-
};
|
|
726
|
-
/**
|
|
727
|
-
* Type guard that checks if an Option is a None variant containing no value.
|
|
728
|
-
*
|
|
729
|
-
* @example
|
|
730
|
-
* const option: Option<number> = none();
|
|
731
|
-
* if (isNone(option)) {
|
|
732
|
-
* // TypeScript knows this option has no value
|
|
733
|
-
* // Attempting to access option.value would be a type error
|
|
734
|
-
* }
|
|
735
|
-
*
|
|
736
|
-
* @param option - The option to check
|
|
737
|
-
* @returns A type predicate indicating if the option is a None variant
|
|
738
|
-
*/
|
|
739
|
-
declare function isNone<T>(option: Option<T>): option is {
|
|
740
|
-
readonly some: false;
|
|
741
|
-
readonly [OPTION_BRAND]: 'none';
|
|
742
|
-
};
|
|
743
|
-
/**
|
|
744
|
-
* Maps the value inside an Option using a transformation function.
|
|
745
|
-
*
|
|
746
|
-
* @example
|
|
747
|
-
* const option: Option<number> = some(123);
|
|
748
|
-
* const transformed: Option<string> = map(option, (value) => value.toString());
|
|
749
|
-
*
|
|
750
|
-
* @param option - The Option to transform
|
|
751
|
-
* @param fn - The function to apply to the contained value
|
|
752
|
-
* @returns A new Option containing the transformed value, or none if the input was none
|
|
753
|
-
*/
|
|
754
|
-
declare function map<T, U>(option: Option<T>, fn: (value: T) => U): Option<U>;
|
|
755
|
-
/**
|
|
756
|
-
* Maps the value inside an Option using a transformation function that returns an Option.
|
|
757
|
-
*
|
|
758
|
-
* @example
|
|
759
|
-
* const option: Option<number> = some(123);
|
|
760
|
-
* const transformed: Option<string> = flatMap(option, (value) => some(value.toString()));
|
|
761
|
-
*
|
|
762
|
-
* @param option - The Option to transform
|
|
763
|
-
* @param fn - The function to apply to the contained value, returning an Option
|
|
764
|
-
* @returns The Option returned by the transformation function, or none if the input was none
|
|
765
|
-
*/
|
|
766
|
-
declare function flatMap<T, U>(option: Option<T>, fn: (value: T) => Option<U>): Option<U>;
|
|
767
|
-
/**
|
|
768
|
-
* Maps both the Some and None branches of an Option using separate transformation functions.
|
|
769
|
-
* This is similar to combining a match with flatMap: it lets you decide the resulting Option in both cases.
|
|
770
|
-
*
|
|
771
|
-
* @example
|
|
772
|
-
* const o1 = some(123);
|
|
773
|
-
* const r1 = bimap(o1, (n) => some(n.toString()), () => none()); // some("123")
|
|
774
|
-
*
|
|
775
|
-
* const o2 = none<number>();
|
|
776
|
-
* const r2 = bimap(o2, (n) => some(n.toString()), () => some('fallback')); // some("fallback")
|
|
777
|
-
*
|
|
778
|
-
* @param option - The Option to transform
|
|
779
|
-
* @param someFn - The function to apply if the Option is Some, returning a new Option
|
|
780
|
-
* @param noneFn - The function to apply if the Option is None, returning a new Option
|
|
781
|
-
* @returns The Option returned by the appropriate transformation function
|
|
782
|
-
*/
|
|
783
|
-
declare function bimap<T, U>(option: Option<T>, someFn: (value: T) => Option<U>, noneFn: () => Option<U>): Option<U>;
|
|
784
|
-
/**
|
|
785
|
-
* Filters an Option based on a predicate function.
|
|
786
|
-
*
|
|
787
|
-
* @example
|
|
788
|
-
* const option: Option<number> = some(123);
|
|
789
|
-
* const filtered: Option<number> = filter(option, value => value > 100); // still Some(123)
|
|
790
|
-
* const filtered2: Option<number> = filter(option, value => value < 100); // None
|
|
791
|
-
*
|
|
792
|
-
* @param option - The Option to filter
|
|
793
|
-
* @param predicate - A function that determines if the value should be kept
|
|
794
|
-
* @returns The original Option if it contains a value that satisfies the predicate, otherwise None
|
|
795
|
-
*/
|
|
796
|
-
declare function filter<T>(option: Option<T>, predicate: (value: T) => boolean): Option<T>;
|
|
797
|
-
/**
|
|
798
|
-
* Executes a callback with the value if the Option is Some, without changing the Option.
|
|
799
|
-
* Useful for side effects like logging while maintaining a processing chain.
|
|
800
|
-
*
|
|
801
|
-
* @example
|
|
802
|
-
* const result = pipe(
|
|
803
|
-
* some(123),
|
|
804
|
-
* opt => map(opt, x => x * 2),
|
|
805
|
-
* opt => tap(opt, x => console.log(`Value: ${x}`)), // Logs but doesn't change the Option
|
|
806
|
-
* opt => filter(opt, x => x > 200)
|
|
807
|
-
* );
|
|
808
|
-
*
|
|
809
|
-
* @param option - The Option to tap into
|
|
810
|
-
* @param fn - The function to execute with the value if Some
|
|
811
|
-
* @returns The original Option unchanged
|
|
812
|
-
*/
|
|
813
|
-
declare function tap<T>(option: Option<T>, fn: (value: T) => void): Option<T>;
|
|
814
|
-
/**
|
|
815
|
-
* Pattern matches on an Option to handle both Some and None cases.
|
|
816
|
-
*
|
|
817
|
-
* @example
|
|
818
|
-
* const option = some(42);
|
|
819
|
-
* const result = match(option, {
|
|
820
|
-
* some: (value) => `Got value: ${value}`,
|
|
821
|
-
* none: () => "Got nothing"
|
|
822
|
-
* }); // "Got value: 42"
|
|
823
|
-
*
|
|
824
|
-
* const empty = none<number>();
|
|
825
|
-
* const result2 = match(empty, {
|
|
826
|
-
* some: (value) => `Got value: ${value}`,
|
|
827
|
-
* none: () => "Got nothing"
|
|
828
|
-
* }); // "Got nothing"
|
|
829
|
-
*
|
|
830
|
-
* @param option - The Option to match against
|
|
831
|
-
* @param patterns - An object containing handler functions for Some and None cases
|
|
832
|
-
* @returns The result of calling the appropriate handler function
|
|
833
|
-
*/
|
|
834
|
-
declare function match<T, R1, R2>(option: Option<T>, patterns: {
|
|
835
|
-
some: (value: T) => R1;
|
|
836
|
-
none: () => R2;
|
|
837
|
-
}): R1 | R2;
|
|
838
|
-
/**
|
|
839
|
-
* Unwraps the value inside an Option, throwing an error if the Option is none.
|
|
840
|
-
*
|
|
841
|
-
* @remarks
|
|
842
|
-
* This function is intended for prototyping only and should not be used in production.
|
|
843
|
-
* In production code, prefer using pattern matching or `unwrapOr` to handle both Some and None cases safely.
|
|
844
|
-
*
|
|
845
|
-
* @example
|
|
846
|
-
* const option: Option<number> = some(123);
|
|
847
|
-
* const value: number = unwrap(option);
|
|
848
|
-
*
|
|
849
|
-
* @param option - The Option to unwrap
|
|
850
|
-
* @param [errorMsg] - Optional custom error message to throw when the Option is none
|
|
851
|
-
* @returns The contained value
|
|
852
|
-
*/
|
|
853
|
-
declare function unwrap<T>(option: Option<T>, errorMsg?: string): T;
|
|
854
|
-
/**
|
|
855
|
-
* Unwraps the value inside an Option, providing a default value if the Option is none.
|
|
856
|
-
*
|
|
857
|
-
* @example
|
|
858
|
-
* const option: Option<number> = none();
|
|
859
|
-
* const value: number = unwrapOr(option, 123);
|
|
860
|
-
*
|
|
861
|
-
* @param option - The Option to unwrap
|
|
862
|
-
* @param defaultValue - The value to return if the Option is none
|
|
863
|
-
* @returns The contained value, or the default value if the Option is none
|
|
864
|
-
*/
|
|
865
|
-
declare function unwrapOr<T>(option: Option<T>, defaultValue: T): T;
|
|
866
|
-
/**
|
|
867
|
-
* Unwraps the value inside an Option, calling a function to generate a default value if the Option is none.
|
|
868
|
-
* Unlike unwrapOr, this only computes the default when needed.
|
|
869
|
-
*
|
|
870
|
-
* @example
|
|
871
|
-
* const option: Option<number> = none();
|
|
872
|
-
* const value = unwrapOrElse(option, () => expensiveComputation());
|
|
873
|
-
*
|
|
874
|
-
* @param option - The Option to unwrap
|
|
875
|
-
* @param defaultFn - A function that returns a default value if the Option is none
|
|
876
|
-
* @returns The contained value, or the result of calling defaultFn if the Option is none
|
|
877
|
-
*/
|
|
878
|
-
declare function unwrapOrElse<T>(option: Option<T>, defaultFn: () => T): T;
|
|
879
|
-
/**
|
|
880
|
-
* Combines an array of Options into a single Option containing an array of values.
|
|
881
|
-
* Returns None if any Option in the array is None.
|
|
882
|
-
*
|
|
883
|
-
* @example
|
|
884
|
-
* const options = [some(1), some(2), some(3)];
|
|
885
|
-
* const combined = combine(options); // some([1, 2, 3])
|
|
886
|
-
*
|
|
887
|
-
* const withNone = [some(1), none(), some(3)];
|
|
888
|
-
* const result = combine(withNone); // none()
|
|
889
|
-
*
|
|
890
|
-
* @param options - An array of Options to combine
|
|
891
|
-
* @returns An Option containing an array of all values if all inputs are Some, or None if any input is None
|
|
892
|
-
*/
|
|
893
|
-
declare function combine<T1>(options: readonly [Option<T1>]): Option<[T1]>;
|
|
894
|
-
declare function combine<T1, T2>(options: readonly [Option<T1>, Option<T2>]): Option<[T1, T2]>;
|
|
895
|
-
declare function combine<T1, T2, T3>(options: readonly [Option<T1>, Option<T2>, Option<T3>]): Option<[T1, T2, T3]>;
|
|
896
|
-
declare function combine<T1, T2, T3, T4>(options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>]): Option<[T1, T2, T3, T4]>;
|
|
897
|
-
declare function combine<T1, T2, T3, T4, T5>(options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>]): Option<[T1, T2, T3, T4, T5]>;
|
|
898
|
-
declare function combine<T1, T2, T3, T4, T5, T6>(options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>]): Option<[T1, T2, T3, T4, T5, T6]>;
|
|
899
|
-
declare function combine<T1, T2, T3, T4, T5, T6, T7>(options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>, Option<T7>]): Option<[T1, T2, T3, T4, T5, T6, T7]>;
|
|
900
|
-
declare function combine<T1, T2, T3, T4, T5, T6, T7, T8>(options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>, Option<T7>, Option<T8>]): Option<[T1, T2, T3, T4, T5, T6, T7, T8]>;
|
|
901
|
-
declare function combine<T1, T2, T3, T4, T5, T6, T7, T8, T9>(options: readonly [
|
|
902
|
-
Option<T1>,
|
|
903
|
-
Option<T2>,
|
|
904
|
-
Option<T3>,
|
|
905
|
-
Option<T4>,
|
|
906
|
-
Option<T5>,
|
|
907
|
-
Option<T6>,
|
|
908
|
-
Option<T7>,
|
|
909
|
-
Option<T8>,
|
|
910
|
-
Option<T9>
|
|
911
|
-
]): Option<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
|
|
912
|
-
declare function combine<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(options: readonly [
|
|
913
|
-
Option<T1>,
|
|
914
|
-
Option<T2>,
|
|
915
|
-
Option<T3>,
|
|
916
|
-
Option<T4>,
|
|
917
|
-
Option<T5>,
|
|
918
|
-
Option<T6>,
|
|
919
|
-
Option<T7>,
|
|
920
|
-
Option<T8>,
|
|
921
|
-
Option<T9>,
|
|
922
|
-
Option<T10>
|
|
923
|
-
]): Option<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
|
|
924
|
-
declare function combine<T>(options: readonly Option<T>[]): Option<T[]>;
|
|
925
|
-
/**
|
|
926
|
-
* Creates an Option from a nullable value.
|
|
927
|
-
*
|
|
928
|
-
* @example
|
|
929
|
-
* const option = fromNullable(123); // some(123)
|
|
930
|
-
* const option = fromNullable(null); // none()
|
|
931
|
-
*
|
|
932
|
-
* @param value - The value to wrap in the Some variant
|
|
933
|
-
* @returns An Option containing the value (Some) if the value is not null or undefined, or None if the value is null or undefined
|
|
934
|
-
*/
|
|
935
|
-
declare const fromNullable: <T>(value: T | null | undefined) => Option<T>;
|
|
936
|
-
/**
|
|
937
|
-
* Converts an Option to a Result.
|
|
938
|
-
* If the Option is Some, returns an Ok variant with the value.
|
|
939
|
-
* If the Option is None, returns an Err variant with the provided error.
|
|
940
|
-
*
|
|
941
|
-
* @example
|
|
942
|
-
* const result = mapToResult(some(123), 'Some error message');
|
|
943
|
-
* if (isOk(result)) {
|
|
944
|
-
* console.log('Value:', result.value);
|
|
945
|
-
* } else {
|
|
946
|
-
* console.error('Error:', result.error);
|
|
947
|
-
* }
|
|
948
|
-
*
|
|
949
|
-
* @param option - The Option to convert
|
|
950
|
-
* @param error - The error to return if the Option is None
|
|
951
|
-
* @returns A Result containing either the value (Ok) or the error (Err)
|
|
952
|
-
*/
|
|
953
|
-
declare function mapToResult<T, E>(option: Option<T>, error: E): Result<T, E>;
|
|
954
|
-
/**
|
|
955
|
-
* Curried version of `map`. Returns a function that transforms the Some value using `fn`,
|
|
956
|
-
* passing through None unchanged.
|
|
957
|
-
*
|
|
958
|
-
* @example
|
|
959
|
-
* const double = mapWith((n: number) => n * 2);
|
|
960
|
-
*
|
|
961
|
-
* const option: Option<number> = some(5);
|
|
962
|
-
* const doubled: Option<number> = double(option); // some(10)
|
|
963
|
-
*
|
|
964
|
-
* const empty: Option<number> = none();
|
|
965
|
-
* const stillNone: Option<number> = double(empty); // none()
|
|
966
|
-
*
|
|
967
|
-
* // Point-free in a pipe
|
|
968
|
-
* const result = pipe(some(3), mapWith((n: number) => n + 1)); // some(4)
|
|
969
|
-
*
|
|
970
|
-
* @param fn - The function to apply to the Some value
|
|
971
|
-
* @returns A function that takes an Option and returns a new Option with the Some value transformed
|
|
972
|
-
*/
|
|
973
|
-
declare function mapWith<T, U>(fn: (value: T) => U): (option: Option<T>) => Option<U>;
|
|
974
|
-
/**
|
|
975
|
-
* Curried version of `flatMap`. Returns a function that chains on a Some value using `fn`,
|
|
976
|
-
* passing through None unchanged. Supports both sync and async step functions.
|
|
977
|
-
*
|
|
978
|
-
* @example
|
|
979
|
-
* const safeParse = flatMapWith((s: string) => {
|
|
980
|
-
* const n = parseInt(s, 10);
|
|
981
|
-
* return isNaN(n) ? none() : some(n);
|
|
982
|
-
* });
|
|
983
|
-
*
|
|
984
|
-
* const option: Option<string> = some("42");
|
|
985
|
-
* const parsed: Option<number> = safeParse(option); // some(42)
|
|
986
|
-
*
|
|
987
|
-
* const empty: Option<string> = none();
|
|
988
|
-
* const stillNone: Option<number> = safeParse(empty); // none()
|
|
989
|
-
*
|
|
990
|
-
* // Async step function in a pipe
|
|
991
|
-
* const fetchProfile = flatMapWith(async (id: number) => {
|
|
992
|
-
* const profile = await getProfile(id);
|
|
993
|
-
* return profile ? some(profile) : none();
|
|
994
|
-
* });
|
|
995
|
-
*
|
|
996
|
-
* @param fn - The function to apply to the Some value, returning an Option or Promise<Option>
|
|
997
|
-
* @returns A function that takes an Option and returns a (possibly async) Option
|
|
998
|
-
*/
|
|
999
|
-
declare function flatMapWith<T, U>(fn: (value: T) => Promise<Option<U>>): (option: Option<T>) => Promise<Option<U>>;
|
|
1000
|
-
declare function flatMapWith<T, U>(fn: (value: T) => Option<U>): (option: Option<T>) => Option<U>;
|
|
1001
|
-
/**
|
|
1002
|
-
* Curried version of `filter`. Returns a function that keeps a Some value matching the predicate,
|
|
1003
|
-
* or returns None if the predicate fails.
|
|
1004
|
-
*
|
|
1005
|
-
* @example
|
|
1006
|
-
* const isPositive = filterWith((n: number) => n > 0);
|
|
1007
|
-
*
|
|
1008
|
-
* const option: Option<number> = some(5);
|
|
1009
|
-
* const filtered: Option<number> = isPositive(option); // some(5)
|
|
1010
|
-
*
|
|
1011
|
-
* const negative: Option<number> = some(-1);
|
|
1012
|
-
* const gone: Option<number> = isPositive(negative); // none()
|
|
1013
|
-
*
|
|
1014
|
-
* // Point-free in a pipe
|
|
1015
|
-
* const result = pipe(some(10), filterWith((n: number) => n < 100)); // some(10)
|
|
1016
|
-
*
|
|
1017
|
-
* @param predicate - A function that determines if the Some value should be kept
|
|
1018
|
-
* @returns A function that takes an Option and returns the original Option or None
|
|
1019
|
-
*/
|
|
1020
|
-
declare function filterWith<T>(predicate: (value: T) => boolean): (option: Option<T>) => Option<T>;
|
|
1021
|
-
/**
|
|
1022
|
-
* Curried version of `tap`. Returns a function that performs a side effect on the Some value
|
|
1023
|
-
* without changing the Option. None values pass through untouched. Supports both sync and async
|
|
1024
|
-
* side effects.
|
|
1025
|
-
*
|
|
1026
|
-
* @example
|
|
1027
|
-
* const log = tapWith((n: number) => console.log("value:", n));
|
|
1028
|
-
*
|
|
1029
|
-
* const option: Option<number> = some(5);
|
|
1030
|
-
* const same: Option<number> = log(option); // logs "value: 5", returns some(5)
|
|
1031
|
-
*
|
|
1032
|
-
* const empty: Option<number> = none();
|
|
1033
|
-
* const stillNone: Option<number> = log(empty); // no log, returns none()
|
|
1034
|
-
*
|
|
1035
|
-
* // Async side effect in a pipe
|
|
1036
|
-
* const audit = tapWith(async (user: User) => {
|
|
1037
|
-
* await saveAuditLog(user.id);
|
|
1038
|
-
* });
|
|
1039
|
-
*
|
|
1040
|
-
* @param fn - The side-effect function to execute on the Some value
|
|
1041
|
-
* @returns A function that takes an Option, performs the side effect if Some, and returns the original Option
|
|
1042
|
-
*/
|
|
1043
|
-
declare function tapWith<T>(fn: (value: T) => Promise<void>): (option: Option<T>) => Promise<Option<T>>;
|
|
1044
|
-
declare function tapWith<T>(fn: (value: T) => void): (option: Option<T>) => Option<T>;
|
|
1045
|
-
/**
|
|
1046
|
-
* Curried version of `mapToResult`. Returns a function that converts an Option
|
|
1047
|
-
* to a Result, using the provided error if the Option is None.
|
|
1048
|
-
*
|
|
1049
|
-
* @example
|
|
1050
|
-
* const toResult = mapToResultWith('not found');
|
|
1051
|
-
* toResult(some(42)); // ok(42)
|
|
1052
|
-
* toResult(none()); // err('not found')
|
|
1053
|
-
*
|
|
1054
|
-
* // Point-free in a pipe
|
|
1055
|
-
* pipe(fromNullable(value), mapToResultWith('missing'));
|
|
1056
|
-
*
|
|
1057
|
-
* @param error - The error to return if the Option is None
|
|
1058
|
-
* @returns A function that takes an Option and returns a Result
|
|
1059
|
-
*/
|
|
1060
|
-
declare function mapToResultWith<E>(error: E): <T>(option: Option<T>) => Result<T, E>;
|
|
1061
|
-
|
|
1062
|
-
export { type Option as O, type Result, isNone as a, bimap as b, bimap$1 as bimap, filter as c, combine$1 as combine, combineAll, match as d, unwrapOr as e, err, flatMap as f, filter$1 as filter, filterWith$1 as filterWith, flatMap$1 as flatMap, flatMapWith$1 as flatMapWith, fromPromise, fromPromiseWithError, fromTry, fromTryWithError, unwrapOrElse as g, combine as h, isSome as i, isErr, isOk, fromNullable as j, mapToResult as k, mapWith as l, map as m, map$1 as map, mapErr, mapErrWith, mapToOption, mapWith$1 as mapWith, match$1 as match, none as n, flatMapWith as o, ok, orElse, orElseWith, filterWith as p, partition, tapWith as q, mapToResultWith as r, some as s, tap as t, tap$1 as tap, tapErr, tapErrWith, tapWith$1 as tapWith, toPromise, unwrap as u, unwrap$1 as unwrap, unwrapOr$1 as unwrapOr, unwrapOrElse$1 as unwrapOrElse };
|