nalloc 0.0.1
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 +282 -0
- package/build/devtools.cjs +79 -0
- package/build/devtools.cjs.map +1 -0
- package/build/devtools.d.ts +82 -0
- package/build/devtools.js +43 -0
- package/build/devtools.js.map +1 -0
- package/build/index.cjs +76 -0
- package/build/index.cjs.map +1 -0
- package/build/index.d.ts +4 -0
- package/build/index.js +5 -0
- package/build/index.js.map +1 -0
- package/build/option.cjs +279 -0
- package/build/option.cjs.map +1 -0
- package/build/option.d.ts +356 -0
- package/build/option.js +157 -0
- package/build/option.js.map +1 -0
- package/build/result.cjs +381 -0
- package/build/result.cjs.map +1 -0
- package/build/result.d.ts +442 -0
- package/build/result.js +229 -0
- package/build/result.js.map +1 -0
- package/build/safe.cjs +88 -0
- package/build/safe.cjs.map +1 -0
- package/build/safe.d.ts +29 -0
- package/build/safe.js +18 -0
- package/build/safe.js.map +1 -0
- package/build/testing.cjs +111 -0
- package/build/testing.cjs.map +1 -0
- package/build/testing.d.ts +85 -0
- package/build/testing.js +81 -0
- package/build/testing.js.map +1 -0
- package/build/types.cjs +78 -0
- package/build/types.cjs.map +1 -0
- package/build/types.d.ts +137 -0
- package/build/types.js +36 -0
- package/build/types.js.map +1 -0
- package/build/unsafe.cjs +82 -0
- package/build/unsafe.cjs.map +1 -0
- package/build/unsafe.d.ts +27 -0
- package/build/unsafe.js +11 -0
- package/build/unsafe.js.map +1 -0
- package/package.json +93 -0
- package/src/__tests__/index.ts +13 -0
- package/src/__tests__/option.ts +610 -0
- package/src/__tests__/option.types.ts +120 -0
- package/src/__tests__/result.ts +721 -0
- package/src/__tests__/result.types.ts +249 -0
- package/src/__tests__/safe.ts +24 -0
- package/src/__tests__/tooling.ts +86 -0
- package/src/__tests__/unsafe.ts +24 -0
- package/src/devtools.ts +97 -0
- package/src/index.ts +18 -0
- package/src/option.ts +510 -0
- package/src/result.ts +676 -0
- package/src/safe.ts +58 -0
- package/src/testing.ts +159 -0
- package/src/types.ts +201 -0
- package/src/unsafe.ts +47 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
import { isOk, isErr } from './types.js';
|
|
2
|
+
import type { Ok, Err, Result, Option, Widen, WidenNever } from './types.js';
|
|
3
|
+
export type { Ok, Err, Result };
|
|
4
|
+
export { isOk, isErr };
|
|
5
|
+
/**
|
|
6
|
+
* Executes a function and captures the result or error.
|
|
7
|
+
* @param fn - Function to execute
|
|
8
|
+
* @param onError - Optional error transformer
|
|
9
|
+
* @returns Ok(result) if successful, Err(error) if thrown
|
|
10
|
+
* @example
|
|
11
|
+
* tryCatch(() => JSON.parse('{"a":1}')) // Ok({a: 1})
|
|
12
|
+
* tryCatch(() => JSON.parse('invalid')) // Err(SyntaxError)
|
|
13
|
+
* tryCatch(() => { throw 'oops' }, e => e) // Err('oops')
|
|
14
|
+
*/
|
|
15
|
+
export declare function tryCatch<T, E = unknown>(fn: () => T, onError?: (error: unknown) => E): Result<T, E>;
|
|
16
|
+
/**
|
|
17
|
+
* Alias for tryCatch. Executes a function and captures the result or error.
|
|
18
|
+
* @param fn - Function to execute
|
|
19
|
+
* @returns Ok(result) if successful, Err(error) if thrown
|
|
20
|
+
*/
|
|
21
|
+
export declare function of<T, E = unknown>(fn: () => T): Result<T, E>;
|
|
22
|
+
/**
|
|
23
|
+
* Executes an async function and captures the result or error.
|
|
24
|
+
* @param fn - Async function to execute
|
|
25
|
+
* @param onError - Optional error transformer
|
|
26
|
+
* @returns Promise of Ok(result) if successful, Err(error) if rejected
|
|
27
|
+
* @example
|
|
28
|
+
* await tryAsync(() => fetch('/api').then(r => r.json())) // Ok(data) or Err(error)
|
|
29
|
+
*/
|
|
30
|
+
export declare function tryAsync<T, E = unknown>(fn: () => Promise<T>, onError?: (error: unknown) => E): Promise<Result<T, E>>;
|
|
31
|
+
/**
|
|
32
|
+
* Alias for tryAsync. Executes an async function and captures the result or error.
|
|
33
|
+
* @param fn - Async function to execute
|
|
34
|
+
* @returns Promise of Ok(result) if successful, Err(error) if rejected
|
|
35
|
+
*/
|
|
36
|
+
export declare function ofAsync<T, E = unknown>(fn: () => Promise<T>): Promise<Result<T, E>>;
|
|
37
|
+
/**
|
|
38
|
+
* Converts a Promise to a Result.
|
|
39
|
+
* @param promise - The promise to convert
|
|
40
|
+
* @param onRejected - Optional rejection handler
|
|
41
|
+
* @returns Promise of Ok(value) if resolved, Err(error) if rejected
|
|
42
|
+
* @example
|
|
43
|
+
* await fromPromise(Promise.resolve(42)) // Ok(42)
|
|
44
|
+
* await fromPromise(Promise.reject('error')) // Err('error')
|
|
45
|
+
*/
|
|
46
|
+
export declare function fromPromise<T, E = unknown>(promise: Promise<T>, onRejected?: (reason: unknown) => E): Promise<Result<T, E>>;
|
|
47
|
+
/**
|
|
48
|
+
* Unwraps an Ok value or returns a computed value for Err.
|
|
49
|
+
* @param result - The Result to unwrap
|
|
50
|
+
* @param onErr - Function called with error if Err
|
|
51
|
+
* @returns The Ok value or result of onErr(error)
|
|
52
|
+
* @example
|
|
53
|
+
* unwrapOrReturn(ok(42), e => 0) // 42
|
|
54
|
+
* unwrapOrReturn(err('fail'), e => 0) // 0
|
|
55
|
+
*/
|
|
56
|
+
export declare function unwrapOrReturn<T, E, const R>(result: Result<T, E>, onErr: (error: E) => R): T | R;
|
|
57
|
+
/**
|
|
58
|
+
* Asserts that a Result is Ok, throwing if Err.
|
|
59
|
+
* @param result - The Result to assert
|
|
60
|
+
* @param message - Custom error message
|
|
61
|
+
* @throws Error if result is Err
|
|
62
|
+
* @example
|
|
63
|
+
* assertOk(ok(42)) // passes
|
|
64
|
+
* assertOk(err('failed')) // throws Error
|
|
65
|
+
*/
|
|
66
|
+
export declare function assertOk<T, E>(result: Result<T, E>, message?: string): asserts result is Ok<T>;
|
|
67
|
+
/**
|
|
68
|
+
* Asserts that a Result is Err, throwing if Ok.
|
|
69
|
+
* @param result - The Result to assert
|
|
70
|
+
* @param message - Custom error message
|
|
71
|
+
* @throws Error if result is Ok
|
|
72
|
+
* @example
|
|
73
|
+
* assertErr(err('failed')) // passes
|
|
74
|
+
* assertErr(ok(42)) // throws Error
|
|
75
|
+
*/
|
|
76
|
+
export declare function assertErr<T, E>(result: Result<T, E>, message?: string): asserts result is Err<E>;
|
|
77
|
+
/**
|
|
78
|
+
* Checks if result is Err with a non-null error value.
|
|
79
|
+
* @param result - The Result to check
|
|
80
|
+
* @returns true if Err with Some error value
|
|
81
|
+
*/
|
|
82
|
+
export declare function isSomeErr<T, E>(result: Result<T, E>): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Transforms the Ok value, leaving Err unchanged.
|
|
85
|
+
* @param result - The Result to map
|
|
86
|
+
* @param fn - Transform function
|
|
87
|
+
* @returns Ok(fn(value)) if Ok, Err unchanged
|
|
88
|
+
* @example
|
|
89
|
+
* map(ok(2), x => x * 2) // Ok(4)
|
|
90
|
+
* map(err('e'), x => x * 2) // Err('e')
|
|
91
|
+
*/
|
|
92
|
+
export declare function map<T, U, E>(result: Err<E>, fn: (value: T) => U): Err<E>;
|
|
93
|
+
export declare function map<T, U>(result: Ok<T>, fn: (value: T) => U): Ok<U>;
|
|
94
|
+
export declare function map<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
|
|
95
|
+
/**
|
|
96
|
+
* Transforms the Err value, leaving Ok unchanged.
|
|
97
|
+
* @param result - The Result to map
|
|
98
|
+
* @param fn - Error transform function
|
|
99
|
+
* @returns Err(fn(error)) if Err, Ok unchanged
|
|
100
|
+
* @example
|
|
101
|
+
* mapErr(err('e'), e => e.toUpperCase()) // Err('E')
|
|
102
|
+
* mapErr(ok(42), e => e.toUpperCase()) // Ok(42)
|
|
103
|
+
*/
|
|
104
|
+
export declare function mapErr<T, E, F>(result: Ok<T>, fn: (error: E) => F): Ok<T>;
|
|
105
|
+
export declare function mapErr<E, F>(result: Err<E>, fn: (error: E) => F): Err<F>;
|
|
106
|
+
export declare function mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
|
|
107
|
+
/**
|
|
108
|
+
* Chains Result-returning functions. Returns Err if the input is Err.
|
|
109
|
+
* @param result - The Result to chain
|
|
110
|
+
* @param fn - Function returning a Result
|
|
111
|
+
* @returns The result of fn(value) if Ok, Err unchanged
|
|
112
|
+
* @example
|
|
113
|
+
* flatMap(ok(2), x => ok(x * 2)) // Ok(4)
|
|
114
|
+
* flatMap(ok(2), x => err('fail')) // Err('fail')
|
|
115
|
+
* flatMap(err('e'), x => ok(x * 2)) // Err('e')
|
|
116
|
+
*/
|
|
117
|
+
export declare function flatMap<T, U, E>(result: Err<E>, fn: (value: T) => Result<U, E>): Err<E>;
|
|
118
|
+
export declare function flatMap<T, U, E>(result: Result<T, E>, fn: (value: T) => Result<U, E>): Result<U, E>;
|
|
119
|
+
/**
|
|
120
|
+
* Alias for flatMap. Chains Result-returning functions.
|
|
121
|
+
* @param result - The Result to chain
|
|
122
|
+
* @param fn - Function returning a Result
|
|
123
|
+
* @returns The result of fn(value) if Ok, Err unchanged
|
|
124
|
+
*/
|
|
125
|
+
export declare function andThen<T, U, E>(result: Err<E>, fn: (value: T) => Result<U, E>): Err<E>;
|
|
126
|
+
export declare function andThen<T, U, E>(result: Result<T, E>, fn: (value: T) => Result<U, E>): Result<U, E>;
|
|
127
|
+
/**
|
|
128
|
+
* Executes a side effect if Ok, then returns the original Result.
|
|
129
|
+
* @param result - The Result to tap
|
|
130
|
+
* @param fn - Side effect function
|
|
131
|
+
* @returns The original Result unchanged
|
|
132
|
+
* @example
|
|
133
|
+
* tap(ok(42), x => console.log(x)) // logs 42, returns Ok(42)
|
|
134
|
+
*/
|
|
135
|
+
export declare function tap<T, E>(result: Err<E>, fn: (value: T) => void): Err<E>;
|
|
136
|
+
export declare function tap<T>(result: Ok<T>, fn: (value: T) => void): Ok<T>;
|
|
137
|
+
export declare function tap<T, E>(result: Result<T, E>, fn: (value: T) => void): Result<T, E>;
|
|
138
|
+
/**
|
|
139
|
+
* Executes a side effect if Err, then returns the original Result.
|
|
140
|
+
* @param result - The Result to tap
|
|
141
|
+
* @param fn - Side effect function for error
|
|
142
|
+
* @returns The original Result unchanged
|
|
143
|
+
* @example
|
|
144
|
+
* tapErr(err('fail'), e => console.log(e)) // logs 'fail', returns Err('fail')
|
|
145
|
+
*/
|
|
146
|
+
export declare function tapErr<T, E>(result: Ok<T>, fn: (error: E) => void): Ok<T>;
|
|
147
|
+
export declare function tapErr<E>(result: Err<E>, fn: (error: E) => void): Err<E>;
|
|
148
|
+
export declare function tapErr<T, E>(result: Result<T, E>, fn: (error: E) => void): Result<T, E>;
|
|
149
|
+
/**
|
|
150
|
+
* Maps both Ok and Err values simultaneously.
|
|
151
|
+
* @param result - The Result to map
|
|
152
|
+
* @param okFn - Transform for Ok value
|
|
153
|
+
* @param errFn - Transform for Err value
|
|
154
|
+
* @returns Ok(okFn(value)) if Ok, Err(errFn(error)) if Err
|
|
155
|
+
* @example
|
|
156
|
+
* bimap(ok(2), x => x * 2, e => e.toUpperCase()) // Ok(4)
|
|
157
|
+
* bimap(err('e'), x => x * 2, e => e.toUpperCase()) // Err('E')
|
|
158
|
+
*/
|
|
159
|
+
export declare function bimap<T, U, E, F>(result: Ok<T>, okFn: (value: T) => U, errFn: (error: E) => F): Ok<U>;
|
|
160
|
+
export declare function bimap<T, U, E, F>(result: Err<E>, okFn: (value: T) => U, errFn: (error: E) => F): Err<F>;
|
|
161
|
+
export declare function bimap<T, U, E, F>(result: Result<T, E>, okFn: (value: T) => U, errFn: (error: E) => F): Result<U, F>;
|
|
162
|
+
/**
|
|
163
|
+
* Extracts the Ok value, throws if Err.
|
|
164
|
+
* @param result - The Result to unwrap
|
|
165
|
+
* @returns The contained Ok value
|
|
166
|
+
* @throws Error if result is Err
|
|
167
|
+
* @example
|
|
168
|
+
* unwrap(ok(42)) // 42
|
|
169
|
+
* unwrap(err('failed')) // throws Error
|
|
170
|
+
*/
|
|
171
|
+
export declare function unwrap<T, E>(result: Result<T, E>): T;
|
|
172
|
+
/**
|
|
173
|
+
* Extracts the Err value, throws if Ok.
|
|
174
|
+
* @param result - The Result to unwrap
|
|
175
|
+
* @returns The contained error
|
|
176
|
+
* @throws Error if result is Ok
|
|
177
|
+
* @example
|
|
178
|
+
* unwrapErr(err('failed')) // 'failed'
|
|
179
|
+
* unwrapErr(ok(42)) // throws Error
|
|
180
|
+
*/
|
|
181
|
+
export declare function unwrapErr<T, E>(result: Result<T, E>): E;
|
|
182
|
+
/**
|
|
183
|
+
* Extracts the Ok value, or returns a default.
|
|
184
|
+
* @param result - The Result to unwrap
|
|
185
|
+
* @param defaultValue - Value if Err
|
|
186
|
+
* @returns The Ok value or defaultValue
|
|
187
|
+
* @example
|
|
188
|
+
* unwrapOr(ok(42), 0) // 42
|
|
189
|
+
* unwrapOr(err('failed'), 0) // 0
|
|
190
|
+
*/
|
|
191
|
+
export declare function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T;
|
|
192
|
+
/**
|
|
193
|
+
* Extracts the Ok value, or computes a default from the error.
|
|
194
|
+
* @param result - The Result to unwrap
|
|
195
|
+
* @param fn - Function to compute default from error
|
|
196
|
+
* @returns The Ok value or fn(error)
|
|
197
|
+
* @example
|
|
198
|
+
* unwrapOrElse(ok(42), e => 0) // 42
|
|
199
|
+
* unwrapOrElse(err('failed'), e => 0) // 0
|
|
200
|
+
*/
|
|
201
|
+
export declare function unwrapOrElse<T, E>(result: Result<T, E>, fn: (error: E) => T): T;
|
|
202
|
+
/**
|
|
203
|
+
* Maps the Ok value and returns it, or returns a default.
|
|
204
|
+
* @param result - The Result to map
|
|
205
|
+
* @param defaultValue - Value if Err
|
|
206
|
+
* @param fn - Transform function
|
|
207
|
+
* @returns fn(value) if Ok, defaultValue otherwise
|
|
208
|
+
* @example
|
|
209
|
+
* mapOr(ok(2), 0, x => x * 2) // 4
|
|
210
|
+
* mapOr(err('e'), 0, x => x * 2) // 0
|
|
211
|
+
*/
|
|
212
|
+
export declare function mapOr<T, E, U>(result: Result<T, E>, defaultValue: U, fn: (value: T) => U): U;
|
|
213
|
+
/**
|
|
214
|
+
* Maps the Ok value and returns it, or computes a default.
|
|
215
|
+
* @param result - The Result to map
|
|
216
|
+
* @param defaultFn - Function to compute default
|
|
217
|
+
* @param fn - Transform function
|
|
218
|
+
* @returns fn(value) if Ok, defaultFn() otherwise
|
|
219
|
+
* @example
|
|
220
|
+
* mapOrElse(ok(2), () => 0, x => x * 2) // 4
|
|
221
|
+
* mapOrElse(err('e'), () => 0, x => x * 2) // 0
|
|
222
|
+
*/
|
|
223
|
+
export declare function mapOrElse<T, E, U>(result: Result<T, E>, defaultFn: () => U, fn: (value: T) => U): U;
|
|
224
|
+
/**
|
|
225
|
+
* Extracts the Ok value, throws with custom message if Err.
|
|
226
|
+
* @param result - The Result to unwrap
|
|
227
|
+
* @param message - Error message prefix if Err
|
|
228
|
+
* @returns The Ok value
|
|
229
|
+
* @throws Error with message if Err
|
|
230
|
+
* @example
|
|
231
|
+
* expect(ok(42), 'missing value') // 42
|
|
232
|
+
* expect(err('fail'), 'missing value') // throws Error('missing value: fail')
|
|
233
|
+
*/
|
|
234
|
+
export declare function expect<T, E>(result: Result<T, E>, message: string): T;
|
|
235
|
+
/**
|
|
236
|
+
* Extracts the Err value, throws with custom message if Ok.
|
|
237
|
+
* @param result - The Result to unwrap
|
|
238
|
+
* @param message - Error message prefix if Ok
|
|
239
|
+
* @returns The error value
|
|
240
|
+
* @throws Error with message if Ok
|
|
241
|
+
* @example
|
|
242
|
+
* expectErr(err('fail'), 'expected error') // 'fail'
|
|
243
|
+
* expectErr(ok(42), 'expected error') // throws Error
|
|
244
|
+
*/
|
|
245
|
+
export declare function expectErr<T, E>(result: Result<T, E>, message: string): E;
|
|
246
|
+
/**
|
|
247
|
+
* Returns other if result is Ok, otherwise returns the Err.
|
|
248
|
+
* @param result - First Result
|
|
249
|
+
* @param other - Second Result
|
|
250
|
+
* @returns other if result is Ok, result (Err) otherwise
|
|
251
|
+
* @example
|
|
252
|
+
* and(ok(1), ok(2)) // Ok(2)
|
|
253
|
+
* and(err('e'), ok(2)) // Err('e')
|
|
254
|
+
*/
|
|
255
|
+
export declare function and<T, U, E>(result: Result<T, E>, other: Result<U, E>): Result<U, E>;
|
|
256
|
+
/**
|
|
257
|
+
* Returns result if Ok, otherwise returns other.
|
|
258
|
+
* @param result - First Result
|
|
259
|
+
* @param other - Fallback Result
|
|
260
|
+
* @returns result if Ok, other otherwise
|
|
261
|
+
* @example
|
|
262
|
+
* or(ok(1), ok(2)) // Ok(1)
|
|
263
|
+
* or(err('e'), ok(2)) // Ok(2)
|
|
264
|
+
*/
|
|
265
|
+
export declare function or<T, E, F>(result: Result<T, E>, other: Result<T, F>): Result<T, F>;
|
|
266
|
+
/**
|
|
267
|
+
* Returns result if Ok, otherwise computes a fallback from the error.
|
|
268
|
+
* @param result - First Result
|
|
269
|
+
* @param fn - Function to compute fallback
|
|
270
|
+
* @returns result if Ok, fn(error) otherwise
|
|
271
|
+
* @example
|
|
272
|
+
* orElse(ok(1), e => ok(0)) // Ok(1)
|
|
273
|
+
* orElse(err('e'), e => ok(0)) // Ok(0)
|
|
274
|
+
*/
|
|
275
|
+
export declare function orElse<T, E, F>(result: Result<T, E>, fn: (error: E) => Result<T, F>): Result<T, F>;
|
|
276
|
+
/**
|
|
277
|
+
* Converts a Result to an Option, discarding the error.
|
|
278
|
+
* @param result - The Result to convert
|
|
279
|
+
* @returns Some(value) if Ok, None if Err
|
|
280
|
+
* @example
|
|
281
|
+
* toOption(ok(42)) // Some(42)
|
|
282
|
+
* toOption(err('failed')) // None
|
|
283
|
+
*/
|
|
284
|
+
export declare function toOption<T, E>(result: Result<T, E>): Option<T>;
|
|
285
|
+
/**
|
|
286
|
+
* Converts a Result's error to an Option.
|
|
287
|
+
* @param result - The Result to convert
|
|
288
|
+
* @returns Some(error) if Err, None if Ok
|
|
289
|
+
* @example
|
|
290
|
+
* toErrorOption(err('failed')) // Some('failed')
|
|
291
|
+
* toErrorOption(ok(42)) // None
|
|
292
|
+
*/
|
|
293
|
+
export declare function toErrorOption<T, E>(result: Result<T, E>): Option<E>;
|
|
294
|
+
/**
|
|
295
|
+
* Combines two Results into a Result of a tuple.
|
|
296
|
+
* @param left - First Result
|
|
297
|
+
* @param right - Second Result
|
|
298
|
+
* @returns Ok([a, b]) if both Ok, first Err otherwise
|
|
299
|
+
* @example
|
|
300
|
+
* zip(ok(1), ok('a')) // Ok([1, 'a'])
|
|
301
|
+
* zip(ok(1), err('e')) // Err('e')
|
|
302
|
+
*/
|
|
303
|
+
export declare function zip<T, U, E>(left: Result<T, E>, right: Result<U, E>): Result<[T, U], E>;
|
|
304
|
+
/**
|
|
305
|
+
* Combines two Results using a function.
|
|
306
|
+
* @param left - First Result
|
|
307
|
+
* @param right - Second Result
|
|
308
|
+
* @param fn - Combining function
|
|
309
|
+
* @returns Ok(fn(a, b)) if both Ok, first Err otherwise
|
|
310
|
+
* @example
|
|
311
|
+
* zipWith(ok(2), ok(3), (a, b) => a + b) // Ok(5)
|
|
312
|
+
*/
|
|
313
|
+
export declare function zipWith<T, U, V, E>(left: Result<T, E>, right: Result<U, E>, fn: (left: T, right: U) => V): Result<V, E>;
|
|
314
|
+
/**
|
|
315
|
+
* Flattens a nested Result.
|
|
316
|
+
* @param result - Result containing a Result
|
|
317
|
+
* @returns The inner Result
|
|
318
|
+
* @example
|
|
319
|
+
* flatten(ok(ok(42))) // Ok(42)
|
|
320
|
+
* flatten(ok(err('e'))) // Err('e')
|
|
321
|
+
* flatten(err('outer')) // Err('outer')
|
|
322
|
+
*/
|
|
323
|
+
export declare function flatten<T, E>(result: Result<Result<T, E>, E>): Result<T, E>;
|
|
324
|
+
/**
|
|
325
|
+
* Pattern matches on a Result, handling both Ok and Err cases.
|
|
326
|
+
* @param result - The Result to match
|
|
327
|
+
* @param onOk - Handler for Ok case
|
|
328
|
+
* @param onErr - Handler for Err case
|
|
329
|
+
* @returns Result of the matching handler
|
|
330
|
+
* @example
|
|
331
|
+
* match(ok(42), x => x * 2, e => 0) // 84
|
|
332
|
+
* match(err('e'), x => x * 2, e => 0) // 0
|
|
333
|
+
*/
|
|
334
|
+
export declare function match<T, E, U>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => U): U;
|
|
335
|
+
/**
|
|
336
|
+
* Separates an array of Results into Ok values and Err values.
|
|
337
|
+
* @param results - Array of Results
|
|
338
|
+
* @returns Tuple of [Ok values, Err values]
|
|
339
|
+
* @example
|
|
340
|
+
* partition([ok(1), err('a'), ok(2)]) // [[1, 2], ['a']]
|
|
341
|
+
*/
|
|
342
|
+
export declare function partition<T, E>(results: Result<T, E>[]): [T[], E[]];
|
|
343
|
+
/**
|
|
344
|
+
* Collects an array of Results into a Result of an array. Fails on first Err.
|
|
345
|
+
* @param results - Array of Results
|
|
346
|
+
* @returns Ok(values) if all Ok, first Err otherwise
|
|
347
|
+
* @example
|
|
348
|
+
* collect([ok(1), ok(2)]) // Ok([1, 2])
|
|
349
|
+
* collect([ok(1), err('e')]) // Err('e')
|
|
350
|
+
*/
|
|
351
|
+
export declare function collect<T, E>(results: Result<T, E>[]): Result<T[], E>;
|
|
352
|
+
/**
|
|
353
|
+
* Collects all Results, returning all errors if any exist.
|
|
354
|
+
* @param results - Array of Results
|
|
355
|
+
* @returns Ok(values) if all Ok, Err(allErrors) otherwise
|
|
356
|
+
* @example
|
|
357
|
+
* collectAll([ok(1), ok(2)]) // Ok([1, 2])
|
|
358
|
+
* collectAll([ok(1), err('a'), err('b')]) // Err(['a', 'b'])
|
|
359
|
+
*/
|
|
360
|
+
export declare function collectAll<T, E>(results: Result<T, E>[]): Result<T[], E[]>;
|
|
361
|
+
/**
|
|
362
|
+
* Alias for collect with widened types. Collects Results into a Result of array.
|
|
363
|
+
* @param results - Array of Results
|
|
364
|
+
* @returns Ok(values) if all Ok, first Err otherwise
|
|
365
|
+
*/
|
|
366
|
+
export declare function all<T, E>(results: Result<T, E>[]): Result<Widen<T>[], WidenNever<E>>;
|
|
367
|
+
/**
|
|
368
|
+
* Returns the first Ok, or all errors if none succeed.
|
|
369
|
+
* @param results - Array of Results
|
|
370
|
+
* @returns First Ok found, or Err(allErrors) if all fail
|
|
371
|
+
* @example
|
|
372
|
+
* any([err('a'), ok(1), err('b')]) // Ok(1)
|
|
373
|
+
* any([err('a'), err('b')]) // Err(['a', 'b'])
|
|
374
|
+
*/
|
|
375
|
+
export declare function any<T, E>(results: Result<T, E>[]): Result<Widen<T>, WidenNever<E>[]>;
|
|
376
|
+
/**
|
|
377
|
+
* Transposes a Result of Option to an Option of Result.
|
|
378
|
+
* @param result - Result containing an Option
|
|
379
|
+
* @returns Some(Ok(value)) if Ok(Some), None if Ok(None), Some(Err) if Err
|
|
380
|
+
* @example
|
|
381
|
+
* transpose(ok(some(42))) // Some(Ok(42))
|
|
382
|
+
* transpose(ok(none)) // None
|
|
383
|
+
* transpose(err('e')) // Some(Err('e'))
|
|
384
|
+
*/
|
|
385
|
+
export declare function transpose<T, E>(result: Result<Option<T>, E>): Option<Result<T, E>>;
|
|
386
|
+
/**
|
|
387
|
+
* Checks if Ok and the value satisfies a predicate.
|
|
388
|
+
* @param result - The Result to check
|
|
389
|
+
* @param predicate - Test function
|
|
390
|
+
* @returns true if Ok and predicate returns true
|
|
391
|
+
* @example
|
|
392
|
+
* isOkAnd(ok(4), x => x > 2) // true
|
|
393
|
+
* isOkAnd(ok(1), x => x > 2) // false
|
|
394
|
+
* isOkAnd(err('e'), x => x > 2) // false
|
|
395
|
+
*/
|
|
396
|
+
export declare function isOkAnd<T, E>(result: Result<T, E>, predicate: (value: T) => boolean): boolean;
|
|
397
|
+
/**
|
|
398
|
+
* Checks if Err and the error satisfies a predicate.
|
|
399
|
+
* @param result - The Result to check
|
|
400
|
+
* @param predicate - Test function
|
|
401
|
+
* @returns true if Err and predicate returns true
|
|
402
|
+
* @example
|
|
403
|
+
* isErrAnd(err('fatal'), e => e.includes('fatal')) // true
|
|
404
|
+
* isErrAnd(ok(42), e => true) // false
|
|
405
|
+
*/
|
|
406
|
+
export declare function isErrAnd<T, E>(result: Result<T, E>, predicate: (error: E) => boolean): boolean;
|
|
407
|
+
/**
|
|
408
|
+
* Maps an async function over an Ok value.
|
|
409
|
+
* @param result - The Result to map
|
|
410
|
+
* @param fn - Async transform function
|
|
411
|
+
* @param onRejected - Optional rejection handler
|
|
412
|
+
* @returns Promise of mapped Result
|
|
413
|
+
* @example
|
|
414
|
+
* await mapAsync(ok(2), async x => x * 2) // Ok(4)
|
|
415
|
+
*/
|
|
416
|
+
export declare function mapAsync<T, U, E = unknown>(result: Result<T, E>, fn: (value: T) => Promise<U>, onRejected?: (error: unknown) => E): Promise<Result<U, E>>;
|
|
417
|
+
/**
|
|
418
|
+
* Chains an async Result-returning function.
|
|
419
|
+
* @param result - The Result to chain
|
|
420
|
+
* @param fn - Async function returning a Result
|
|
421
|
+
* @returns Promise of the chained Result
|
|
422
|
+
* @example
|
|
423
|
+
* await andThenAsync(ok(2), async x => ok(x * 2)) // Ok(4)
|
|
424
|
+
*/
|
|
425
|
+
export declare function andThenAsync<T, U, E>(result: Result<T, E>, fn: (value: T) => Promise<Result<U, E>>): Promise<Result<U, E>>;
|
|
426
|
+
/**
|
|
427
|
+
* Pattern matches with async handlers.
|
|
428
|
+
* @param result - The Result to match
|
|
429
|
+
* @param onOk - Async handler for Ok
|
|
430
|
+
* @param onErr - Async handler for Err
|
|
431
|
+
* @returns Promise of the handler result
|
|
432
|
+
*/
|
|
433
|
+
export declare function matchAsync<T, E, U>(result: Result<T, E>, onOk: (value: T) => Promise<U>, onErr: (error: E) => Promise<U>): Promise<U>;
|
|
434
|
+
/**
|
|
435
|
+
* Partitions an async iterable of Results.
|
|
436
|
+
* @param results - Iterable of Promise Results
|
|
437
|
+
* @returns Promise of [Ok values, Err values]
|
|
438
|
+
* @example
|
|
439
|
+
* await partitionAsync([Promise.resolve(ok(1)), Promise.resolve(err('a'))])
|
|
440
|
+
* // [[1], ['a']]
|
|
441
|
+
*/
|
|
442
|
+
export declare function partitionAsync<T, E>(results: Iterable<Promise<Result<T, E>>>): Promise<[Widen<T>[], WidenNever<E>[]]>;
|
package/build/result.js
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { err as ERR, isOk, isErr, isSome, isNone, NONE, optionOf } from "./types.js";
|
|
2
|
+
export { isOk, isErr };
|
|
3
|
+
export function tryCatch(fn, onError) {
|
|
4
|
+
try {
|
|
5
|
+
return fn();
|
|
6
|
+
} catch (error) {
|
|
7
|
+
return ERR(onError ? onError(error) : error);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function of(fn) {
|
|
11
|
+
return tryCatch(fn);
|
|
12
|
+
}
|
|
13
|
+
export async function tryAsync(fn, onError) {
|
|
14
|
+
try {
|
|
15
|
+
return await fn();
|
|
16
|
+
} catch (error) {
|
|
17
|
+
return ERR(onError ? onError(error) : error);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function ofAsync(fn) {
|
|
21
|
+
return tryAsync(fn);
|
|
22
|
+
}
|
|
23
|
+
export async function fromPromise(promise, onRejected) {
|
|
24
|
+
try {
|
|
25
|
+
return await promise;
|
|
26
|
+
} catch (error) {
|
|
27
|
+
return ERR(onRejected ? onRejected(error) : error);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export function unwrapOrReturn(result, onErr) {
|
|
31
|
+
return isOk(result) ? result : onErr(result.error);
|
|
32
|
+
}
|
|
33
|
+
export function assertOk(result, message) {
|
|
34
|
+
if (isErr(result)) {
|
|
35
|
+
throw new Error(message ?? `Expected Ok result. Received error: ${String(result.error)}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export function assertErr(result, message) {
|
|
39
|
+
if (isOk(result)) {
|
|
40
|
+
throw new Error(message ?? 'Expected Err result.');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export function isSomeErr(result) {
|
|
44
|
+
return isErr(result) && isSome(result.error);
|
|
45
|
+
}
|
|
46
|
+
export function map(result, fn) {
|
|
47
|
+
if (isErr(result)) return result;
|
|
48
|
+
return fn(result);
|
|
49
|
+
}
|
|
50
|
+
export function mapErr(result, fn) {
|
|
51
|
+
if (isOk(result)) return result;
|
|
52
|
+
return ERR(fn(result.error));
|
|
53
|
+
}
|
|
54
|
+
export function flatMap(result, fn) {
|
|
55
|
+
if (isErr(result)) return result;
|
|
56
|
+
return fn(result);
|
|
57
|
+
}
|
|
58
|
+
export function andThen(result, fn) {
|
|
59
|
+
return flatMap(result, fn);
|
|
60
|
+
}
|
|
61
|
+
export function tap(result, fn) {
|
|
62
|
+
if (isOk(result)) {
|
|
63
|
+
fn(result);
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
export function tapErr(result, fn) {
|
|
68
|
+
if (isErr(result)) {
|
|
69
|
+
fn(result.error);
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
export function bimap(result, okFn, errFn) {
|
|
74
|
+
return isOk(result) ? okFn(result) : ERR(errFn(result.error));
|
|
75
|
+
}
|
|
76
|
+
export function unwrap(result) {
|
|
77
|
+
if (isErr(result)) {
|
|
78
|
+
throw new Error(`Called unwrap on Err: ${String(result.error)}`);
|
|
79
|
+
}
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
export function unwrapErr(result) {
|
|
83
|
+
if (isOk(result)) {
|
|
84
|
+
throw new Error(`Called unwrapErr on Ok: ${String(result)}`);
|
|
85
|
+
}
|
|
86
|
+
return result.error;
|
|
87
|
+
}
|
|
88
|
+
export function unwrapOr(result, defaultValue) {
|
|
89
|
+
return isOk(result) ? result : defaultValue;
|
|
90
|
+
}
|
|
91
|
+
export function unwrapOrElse(result, fn) {
|
|
92
|
+
return isOk(result) ? result : fn(result.error);
|
|
93
|
+
}
|
|
94
|
+
export function mapOr(result, defaultValue, fn) {
|
|
95
|
+
return isOk(result) ? fn(result) : defaultValue;
|
|
96
|
+
}
|
|
97
|
+
export function mapOrElse(result, defaultFn, fn) {
|
|
98
|
+
return isOk(result) ? fn(result) : defaultFn();
|
|
99
|
+
}
|
|
100
|
+
export function expect(result, message) {
|
|
101
|
+
if (isErr(result)) {
|
|
102
|
+
throw new Error(`${message}: ${String(result.error)}`);
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
export function expectErr(result, message) {
|
|
107
|
+
if (isOk(result)) {
|
|
108
|
+
throw new Error(`${message}: ${String(result)}`);
|
|
109
|
+
}
|
|
110
|
+
return result.error;
|
|
111
|
+
}
|
|
112
|
+
export function and(result, other) {
|
|
113
|
+
return isOk(result) ? other : result;
|
|
114
|
+
}
|
|
115
|
+
export function or(result, other) {
|
|
116
|
+
return isOk(result) ? result : other;
|
|
117
|
+
}
|
|
118
|
+
export function orElse(result, fn) {
|
|
119
|
+
return isOk(result) ? result : fn(result.error);
|
|
120
|
+
}
|
|
121
|
+
export function toOption(result) {
|
|
122
|
+
return isOk(result) ? optionOf(result) : NONE;
|
|
123
|
+
}
|
|
124
|
+
export function toErrorOption(result) {
|
|
125
|
+
return isErr(result) ? optionOf(result.error) : NONE;
|
|
126
|
+
}
|
|
127
|
+
export function zip(left, right) {
|
|
128
|
+
if (isErr(left)) return left;
|
|
129
|
+
if (isErr(right)) return right;
|
|
130
|
+
return [
|
|
131
|
+
left,
|
|
132
|
+
right
|
|
133
|
+
];
|
|
134
|
+
}
|
|
135
|
+
export function zipWith(left, right, fn) {
|
|
136
|
+
if (isErr(left)) return left;
|
|
137
|
+
if (isErr(right)) return right;
|
|
138
|
+
return fn(left, right);
|
|
139
|
+
}
|
|
140
|
+
export function flatten(result) {
|
|
141
|
+
return isErr(result) ? result : result;
|
|
142
|
+
}
|
|
143
|
+
export function match(result, onOk, onErr) {
|
|
144
|
+
return isOk(result) ? onOk(result) : onErr(result.error);
|
|
145
|
+
}
|
|
146
|
+
export function partition(results) {
|
|
147
|
+
const oks = [];
|
|
148
|
+
const errs = [];
|
|
149
|
+
for (const result of results){
|
|
150
|
+
if (isOk(result)) {
|
|
151
|
+
oks.push(result);
|
|
152
|
+
} else {
|
|
153
|
+
errs.push(result.error);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return [
|
|
157
|
+
oks,
|
|
158
|
+
errs
|
|
159
|
+
];
|
|
160
|
+
}
|
|
161
|
+
export function collect(results) {
|
|
162
|
+
const values = [];
|
|
163
|
+
for (const result of results){
|
|
164
|
+
if (isErr(result)) {
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
values.push(result);
|
|
168
|
+
}
|
|
169
|
+
return values;
|
|
170
|
+
}
|
|
171
|
+
export function collectAll(results) {
|
|
172
|
+
const [oks, errs] = partition(results);
|
|
173
|
+
return errs.length > 0 ? ERR(errs) : oks;
|
|
174
|
+
}
|
|
175
|
+
export function all(results) {
|
|
176
|
+
return collect(results);
|
|
177
|
+
}
|
|
178
|
+
export function any(results) {
|
|
179
|
+
const len = results.length;
|
|
180
|
+
const errors = new Array(len);
|
|
181
|
+
for(let i = 0; i < len; i++){
|
|
182
|
+
const result = results[i];
|
|
183
|
+
if (isOk(result)) return result;
|
|
184
|
+
errors[i] = result.error;
|
|
185
|
+
}
|
|
186
|
+
return ERR(errors);
|
|
187
|
+
}
|
|
188
|
+
export function transpose(result) {
|
|
189
|
+
if (isErr(result)) {
|
|
190
|
+
return ERR(result.error);
|
|
191
|
+
}
|
|
192
|
+
const opt = result;
|
|
193
|
+
return isNone(opt) ? NONE : opt;
|
|
194
|
+
}
|
|
195
|
+
export function isOkAnd(result, predicate) {
|
|
196
|
+
return isOk(result) && predicate(result);
|
|
197
|
+
}
|
|
198
|
+
export function isErrAnd(result, predicate) {
|
|
199
|
+
return isErr(result) && predicate(result.error);
|
|
200
|
+
}
|
|
201
|
+
export async function mapAsync(result, fn, onRejected) {
|
|
202
|
+
if (isErr(result)) return result;
|
|
203
|
+
return fromPromise(fn(result), onRejected);
|
|
204
|
+
}
|
|
205
|
+
export async function andThenAsync(result, fn) {
|
|
206
|
+
if (isErr(result)) return result;
|
|
207
|
+
return fn(result);
|
|
208
|
+
}
|
|
209
|
+
export async function matchAsync(result, onOk, onErr) {
|
|
210
|
+
return isOk(result) ? onOk(result) : onErr(result.error);
|
|
211
|
+
}
|
|
212
|
+
export async function partitionAsync(results) {
|
|
213
|
+
const oks = [];
|
|
214
|
+
const errs = [];
|
|
215
|
+
for (const promise of results){
|
|
216
|
+
const result = await promise;
|
|
217
|
+
if (isOk(result)) {
|
|
218
|
+
oks.push(result);
|
|
219
|
+
} else {
|
|
220
|
+
errs.push(result.error);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return [
|
|
224
|
+
oks,
|
|
225
|
+
errs
|
|
226
|
+
];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
//# sourceMappingURL=result.js.map
|