happy-rusty 1.1.2 → 1.3.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/dist/main.cjs +292 -110
- package/dist/main.cjs.map +1 -1
- package/dist/main.mjs +287 -110
- package/dist/main.mjs.map +1 -1
- package/dist/types.d.ts +95 -36
- package/docs/README.md +6 -1
- package/docs/functions/Err.md +1 -1
- package/docs/functions/Ok.md +1 -1
- package/docs/functions/Some.md +1 -1
- package/docs/functions/isOption.md +35 -0
- package/docs/functions/isResult.md +36 -0
- package/docs/functions/{promiseToResult.md → promiseToAsyncResult.md} +5 -5
- package/docs/interfaces/None.md +51 -28
- package/docs/interfaces/Option.md +49 -25
- package/docs/interfaces/Result.md +51 -27
- package/docs/type-aliases/AsyncIOResult.md +2 -2
- package/docs/type-aliases/AsyncOption.md +1 -1
- package/docs/type-aliases/AsyncResult.md +1 -1
- package/docs/type-aliases/IOResult.md +1 -1
- package/docs/variables/None.md +1 -1
- package/docs/variables/RESULT_FALSE.md +18 -0
- package/docs/variables/RESULT_TRUE.md +18 -0
- package/docs/variables/RESULT_ZERO.md +18 -0
- package/package.json +11 -10
- package/src/enum/constants.ts +25 -0
- package/src/enum/core.ts +569 -0
- package/src/enum/defines.ts +38 -0
- package/src/enum/extensions.ts +31 -0
- package/src/enum/helpers.ts +27 -0
- package/src/enum/mod.ts +6 -0
- package/src/enum/prelude.ts +278 -732
- package/src/enum/symbols.ts +9 -0
- package/src/mod.ts +1 -13
package/src/enum/prelude.ts
CHANGED
|
@@ -1,283 +1,7 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
* And [Result](https://doc.rust-lang.org/core/result/index.html) enum type, used for better error handling.
|
|
7
|
-
*
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Symbol for Option kind: `Some` or `None`.
|
|
12
|
-
*/
|
|
13
|
-
const optionKindSymbol = Symbol('Option kind');
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Symbol for Result kind: `Ok` or `Err`.
|
|
17
|
-
*/
|
|
18
|
-
const resultKindSymbol = Symbol('Result kind');
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Type `Option` represents an optional value: every `Option` is either `Some` and contains a value, or `None`, and does not.
|
|
22
|
-
* This interface includes methods that act on the `Option` type, similar to Rust's `Option` enum.
|
|
23
|
-
*
|
|
24
|
-
* As Rust Code:
|
|
25
|
-
```rust
|
|
26
|
-
pub enum Option<T> {
|
|
27
|
-
None,
|
|
28
|
-
Some(T),
|
|
29
|
-
}
|
|
30
|
-
```
|
|
31
|
-
* @typeParam T - The type of the value contained in the `Some` variant.
|
|
32
|
-
*/
|
|
33
|
-
export interface Option<T> {
|
|
34
|
-
// #region Internal properties
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Identify `Some` or `None`.
|
|
38
|
-
*
|
|
39
|
-
* @private
|
|
40
|
-
*/
|
|
41
|
-
readonly [optionKindSymbol]: 'Some' | 'None';
|
|
42
|
-
|
|
43
|
-
// #endregion
|
|
44
|
-
|
|
45
|
-
// #region Querying the variant
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* The `isSome` and `isNone` methods return `true` if the `Option` is `Some` or `None`, respectively.
|
|
49
|
-
*/
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Returns `true` if the Option is a `Some` value.
|
|
53
|
-
*/
|
|
54
|
-
isSome(): boolean;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Returns `true` if the Option is a `None` value.
|
|
58
|
-
*/
|
|
59
|
-
isNone(): boolean;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Returns `true` if the Option is a `Some` value and the predicate returns `true` for the contained value.
|
|
63
|
-
* @param predicate - A function that takes the contained value and returns a boolean.
|
|
64
|
-
*/
|
|
65
|
-
isSomeAnd(predicate: (value: T) => boolean): boolean;
|
|
66
|
-
|
|
67
|
-
// #endregion
|
|
68
|
-
|
|
69
|
-
// #region Extracting the contained value
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* These methods extract the contained value in an `Option<T>` when it is the `Some` variant:
|
|
73
|
-
*/
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Returns the contained `Some` value, with a provided error message if the value is a `None`.
|
|
77
|
-
* @param msg - The error message to provide if the value is a `None`.
|
|
78
|
-
* @throws {TypeError} Throws an error with the provided message if the Option is a `None`.
|
|
79
|
-
*/
|
|
80
|
-
expect(msg: string): T;
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Returns the contained `Some` value.
|
|
84
|
-
* @throws {TypeError} Throws an error if the value is a `None`.
|
|
85
|
-
*/
|
|
86
|
-
unwrap(): T;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Returns the contained `Some` value or a provided default.
|
|
90
|
-
* @param defaultValue - The value to return if the Option is a `None`.
|
|
91
|
-
*/
|
|
92
|
-
unwrapOr(defaultValue: T): T;
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Returns the contained `Some` value or computes it from a closure.
|
|
96
|
-
* @param fn - A function that returns the default value.
|
|
97
|
-
*/
|
|
98
|
-
unwrapOrElse(fn: () => T): T;
|
|
99
|
-
|
|
100
|
-
// #endregion
|
|
101
|
-
|
|
102
|
-
// #region Transforming contained values
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* These methods transform `Option` to `Result`:
|
|
106
|
-
*/
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
|
|
110
|
-
* @typeParam E - The type of the error value in the `Err` variant of the resulting `Result`.
|
|
111
|
-
* @param error - The error value to use if the Option is a `None`.
|
|
112
|
-
*/
|
|
113
|
-
okOr<E>(error: E): Result<T, E>;
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
|
|
117
|
-
* @typeParam E - The type of the error value in the `Err` variant of the resulting `Result`.
|
|
118
|
-
* @param err - A function that returns the error value.
|
|
119
|
-
*/
|
|
120
|
-
okOrElse<E>(err: () => E): Result<T, E>;
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Transposes an `Option` of a `Result` into a `Result` of an `Option`.
|
|
124
|
-
* @typeParam T - The type of the success value in the `Ok` variant of the `Result`.
|
|
125
|
-
* @typeParam E - The type of the error value in the `Err` variant of the `Result`.
|
|
126
|
-
* @returns `Ok` containing `Some` if the Option is a `Some` containing `Ok`,
|
|
127
|
-
* `Err` containing the error if the Option is a `Some` containing `Err`,
|
|
128
|
-
* `Ok` containing `None` if the Option is `None`.
|
|
129
|
-
*/
|
|
130
|
-
transpose<T, E>(this: Option<Result<T, E>>): Result<Option<T>, E>;
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* These methods transform the `Some` variant:
|
|
134
|
-
*/
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Returns `None` if the Option is `None`, otherwise calls predicate with the wrapped value and returns:
|
|
138
|
-
* - `Some(t)` if predicate returns `true` (where `t` is the wrapped value), and
|
|
139
|
-
* - `None` if predicate returns `false`.
|
|
140
|
-
* @param predicate - A function that takes the contained value and returns a boolean.
|
|
141
|
-
*/
|
|
142
|
-
filter(predicate: (value: T) => boolean): Option<T>;
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Converts from `Option<Option<T>>` to `Option<T>`.
|
|
146
|
-
* @returns `None` if the Option is `None`, otherwise returns the contained `Option`.
|
|
147
|
-
*/
|
|
148
|
-
flatten<T>(this: Option<Option<T>>): Option<T>;
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
|
|
152
|
-
* @typeParam U - The type of the value returned by the map function.
|
|
153
|
-
* @param fn - A function that takes the contained value and returns a new value.
|
|
154
|
-
*/
|
|
155
|
-
map<U>(fn: (value: T) => U): Option<U>;
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Maps an `Option<T>` to `U` by applying a function to the contained value (if any), or returns the provided default (if not).
|
|
159
|
-
* @typeParam U - The type of the value returned by the map function or the default value.
|
|
160
|
-
* @param defaultValue - The value to return if the Option is `None`.
|
|
161
|
-
* @param fn - A function that takes the contained value and returns a new value.
|
|
162
|
-
*/
|
|
163
|
-
mapOr<U>(defaultValue: U, fn: (value: T) => U): U;
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Maps an `Option<T>` to `U` by applying a function to a contained value (if any), or computes a default (if not).
|
|
167
|
-
* @typeParam U - The type of the value returned by the map function or the default function.
|
|
168
|
-
* @param defaultFn - A function that returns the default value.
|
|
169
|
-
* @param fn - A function that takes the contained value and returns a new value.
|
|
170
|
-
*/
|
|
171
|
-
mapOrElse<U>(defaultFn: () => U, fn: (value: T) => U): U;
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* These methods combine the `Some` variants of two `Option` values:
|
|
175
|
-
*/
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Combines `this` with another `Option` by zipping their contained values.
|
|
179
|
-
* If `this` is `Some(s)` and `other` is `Some(o)`, returns `Some([s, o])`.
|
|
180
|
-
* If either `this` or `other` is `None`, returns `None`.
|
|
181
|
-
* @typeParam U - The type of the value in the other `Option`.
|
|
182
|
-
* @param other - The other `Option` to zip with.
|
|
183
|
-
* @returns An `Option` containing a tuple of the values if both are `Some`, otherwise `None`.
|
|
184
|
-
*/
|
|
185
|
-
zip<U>(other: Option<U>): Option<[T, U]>;
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Zips `this` with another `Option` using a provided function to combine their contained values.
|
|
189
|
-
* If `this` is `Some(s)` and `other` is `Some(o)`, returns `Some(fn(s, o))`.
|
|
190
|
-
* If either `this` or `other` is `None`, returns `None`.
|
|
191
|
-
* @typeParam U - The type of the value in the other `Option`.
|
|
192
|
-
* @typeParam R - The return type of the combining function.
|
|
193
|
-
* @param other - The other `Option` to zip with.
|
|
194
|
-
* @param fn - The function to combine the values from both `Options`.
|
|
195
|
-
* @returns An `Option` containing the result of `fn` if both `Options` are `Some`, otherwise `None`.
|
|
196
|
-
*/
|
|
197
|
-
zipWith<U, R>(other: Option<U>, fn: (value: T, otherValue: U) => R): Option<R>;
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Converts from `Option<[T, U]>` to `[Option<T>, Option<U>]`.
|
|
201
|
-
* If `this` is `Some([a, b])`, returns `[Some(a), Some(b)]`.
|
|
202
|
-
* If `this` is `None`, returns `[None, None]`.
|
|
203
|
-
* @typeParam T - The type of the first value in the tuple.
|
|
204
|
-
* @typeParam U - The type of the second value in the tuple.
|
|
205
|
-
* @returns A tuple of `Options`, one for each element in the original `Option` of a tuple.
|
|
206
|
-
*/
|
|
207
|
-
unzip<T, U>(this: Option<[T, U]>): [Option<T>, Option<U>];
|
|
208
|
-
|
|
209
|
-
// #endregion
|
|
210
|
-
|
|
211
|
-
// #region Boolean operators
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* These methods treat the `Option` as a boolean value, where `Some` acts like `true` and `None` acts like `false`.
|
|
215
|
-
*/
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Returns `None` if the Option is `None`, otherwise returns `other`.
|
|
219
|
-
* This is sometimes called "and then" because it is similar to a logical AND operation.
|
|
220
|
-
* @typeParam U - The type of the value in the other `Option`.
|
|
221
|
-
* @param other - The `Option` to return if `this` is `Some`.
|
|
222
|
-
* @returns `None` if `this` is `None`, otherwise returns `other`.
|
|
223
|
-
*/
|
|
224
|
-
and<U>(other: Option<U>): Option<U>;
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Returns `None` if the Option is `None`, otherwise calls `fn` with the wrapped value and returns the result.
|
|
228
|
-
* This function can be used for control flow based on `Option` values.
|
|
229
|
-
* @typeParam U - The type of the value returned by the function.
|
|
230
|
-
* @param fn - A function that takes the contained value and returns an `Option`.
|
|
231
|
-
* @returns The result of `fn` if `this` is `Some`, otherwise `None`.
|
|
232
|
-
*/
|
|
233
|
-
andThen<U>(fn: (value: T) => Option<U>): Option<U>;
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Returns the Option if it contains a value, otherwise returns `other`.
|
|
237
|
-
* This can be used for providing a fallback `Option`.
|
|
238
|
-
* @param other - The fallback `Option` to use if `this` is `None`.
|
|
239
|
-
* @returns `this` if it is `Some`, otherwise `other`.
|
|
240
|
-
*/
|
|
241
|
-
or(other: Option<T>): Option<T>;
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Returns the Option if it contains a value, otherwise calls `fn` and returns the result.
|
|
245
|
-
* This method can be used for lazy fallbacks, as `fn` is only evaluated if `this` is `None`.
|
|
246
|
-
* @param fn - A function that produces an `Option`.
|
|
247
|
-
* @returns `this` if it is `Some`, otherwise the result of `fn`.
|
|
248
|
-
*/
|
|
249
|
-
orElse(fn: () => Option<T>): Option<T>;
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Returns `Some` if exactly one of `this`, `other` is `Some`, otherwise returns `None`.
|
|
253
|
-
* This can be thought of as an exclusive or operation on `Option` values.
|
|
254
|
-
* @param other - The other `Option` to compare with.
|
|
255
|
-
* @returns `Some` if exactly one of `this` and `other` is `Some`, otherwise `None`.
|
|
256
|
-
*/
|
|
257
|
-
xor(other: Option<T>): Option<T>;
|
|
258
|
-
|
|
259
|
-
// #endregion
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Calls the provided function with the contained value if `this` is `Some`.
|
|
263
|
-
* This is primarily for side effects and does not transform the `Option`.
|
|
264
|
-
* @param fn - A function to call with the contained value.
|
|
265
|
-
* @returns `this`, unmodified, for chaining additional methods.
|
|
266
|
-
*/
|
|
267
|
-
inspect(fn: (value: T) => void): this;
|
|
268
|
-
|
|
269
|
-
// #region Equals comparison
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Tests whether `this` and `other` are both `Some` containing equal values, or both are `None`.
|
|
273
|
-
* This method can be used for comparing `Option` instances in a value-sensitive manner.
|
|
274
|
-
* @param other - The other `Option` to compare with.
|
|
275
|
-
* @returns `true` if `this` and `other` are both `Some` with equal values, or both are `None`, otherwise `false`.
|
|
276
|
-
*/
|
|
277
|
-
eq(other: Option<T>): boolean;
|
|
278
|
-
|
|
279
|
-
// #endregion
|
|
280
|
-
}
|
|
2
|
+
import type { Option, Result } from './core.ts';
|
|
3
|
+
import { isOption, isResult } from './helpers.ts';
|
|
4
|
+
import { OptionKindSymbol, ResultKindSymbol } from './symbols.ts';
|
|
281
5
|
|
|
282
6
|
/**
|
|
283
7
|
* Represents the absence of a value, as a specialized `Option` type.
|
|
@@ -288,7 +12,7 @@ export interface None extends Option<never> {
|
|
|
288
12
|
* When using `None` alone, the following overrides can make type inference more accurate.
|
|
289
13
|
*/
|
|
290
14
|
|
|
291
|
-
readonly [
|
|
15
|
+
readonly [OptionKindSymbol]: 'None';
|
|
292
16
|
|
|
293
17
|
unwrapOr<T>(defaultValue: T): T;
|
|
294
18
|
unwrapOrElse<T>(fn: () => T): T;
|
|
@@ -312,321 +36,6 @@ export interface None extends Option<never> {
|
|
|
312
36
|
eq<T>(other: Option<T>): boolean;
|
|
313
37
|
}
|
|
314
38
|
|
|
315
|
-
/**
|
|
316
|
-
* The `Result` type is used for returning and propagating errors.
|
|
317
|
-
* It is an enum with the variants, `Ok(T)`, representing success and containing a value, and `Err(E)`, representing error and containing an error value.
|
|
318
|
-
* This interface includes methods that act on the `Result` type, similar to Rust's `Result` enum.
|
|
319
|
-
*
|
|
320
|
-
* As Rust Code:
|
|
321
|
-
```rust
|
|
322
|
-
pub enum Result<T, E> {
|
|
323
|
-
Ok(T),
|
|
324
|
-
Err(E),
|
|
325
|
-
}
|
|
326
|
-
```
|
|
327
|
-
* @typeParam T - The type of the value contained in a successful `Result`.
|
|
328
|
-
* @typeParam E - The type of the error contained in an unsuccessful `Result`.
|
|
329
|
-
*/
|
|
330
|
-
export interface Result<T, E> {
|
|
331
|
-
// #region Internal properties
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* Identify `Ok` or `Err`.
|
|
335
|
-
*
|
|
336
|
-
* @private
|
|
337
|
-
*/
|
|
338
|
-
readonly [resultKindSymbol]: 'Ok' | 'Err';
|
|
339
|
-
|
|
340
|
-
// #endregion
|
|
341
|
-
|
|
342
|
-
// #region Querying the variant
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* The `isOk` and `isErr` methods return `true` if the `Result` is `Ok` or `Err`, respectively.
|
|
346
|
-
*/
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* Returns `true` if the result is `Ok`.
|
|
350
|
-
*/
|
|
351
|
-
isOk(): boolean;
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Returns `true` if the result is `Err`.
|
|
355
|
-
*/
|
|
356
|
-
isErr(): boolean;
|
|
357
|
-
|
|
358
|
-
/**
|
|
359
|
-
* Returns `true` if the result is `Ok` and the provided predicate returns `true` for the contained value.
|
|
360
|
-
* @param predicate - A function that takes the `Ok` value and returns a boolean.
|
|
361
|
-
*/
|
|
362
|
-
isOkAnd(predicate: (value: T) => boolean): boolean;
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
* Returns `true` if the result is `Err` and the provided predicate returns `true` for the contained error.
|
|
366
|
-
* @param predicate - A function that takes the `Err` value and returns a boolean.
|
|
367
|
-
*/
|
|
368
|
-
isErrAnd(predicate: (error: E) => boolean): boolean;
|
|
369
|
-
|
|
370
|
-
// #endregion
|
|
371
|
-
|
|
372
|
-
// #region Extracting the contained value
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* These methods extract the contained value in a `Result<T, E>` when it is the `Ok` variant.
|
|
376
|
-
*/
|
|
377
|
-
|
|
378
|
-
/**
|
|
379
|
-
* Returns the contained `Ok` value, with a provided error message if the result is `Err`.
|
|
380
|
-
* @param msg - The error message to provide if the result is an `Err`.
|
|
381
|
-
* @throws {TypeError} Throws an error with the provided message if the result is an `Err`.
|
|
382
|
-
*/
|
|
383
|
-
expect(msg: string): T;
|
|
384
|
-
|
|
385
|
-
/**
|
|
386
|
-
* Returns the contained `Ok` value.
|
|
387
|
-
* @throws {TypeError} Throws an error if the result is an `Err`.
|
|
388
|
-
*/
|
|
389
|
-
unwrap(): T;
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
* Returns the contained `Ok` value or a provided default.
|
|
393
|
-
* @param defaultValue - The value to return if the result is an `Err`.
|
|
394
|
-
*/
|
|
395
|
-
unwrapOr(defaultValue: T): T;
|
|
396
|
-
|
|
397
|
-
/**
|
|
398
|
-
* Returns the contained `Ok` value or computes it from a closure if the result is `Err`.
|
|
399
|
-
* @param fn - A function that takes the `Err` value and returns an `Ok` value.
|
|
400
|
-
*/
|
|
401
|
-
unwrapOrElse(fn: (error: E) => T): T;
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* These methods extract the contained value in a `Result<T, E>` when it is the `Err` variant.
|
|
405
|
-
*/
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* Returns the contained `Err` value, with a provided error message if the result is `Ok`.
|
|
409
|
-
* @param msg - The error message to provide if the result is an `Ok`.
|
|
410
|
-
* @throws {TypeError} Throws an error with the provided message if the result is an `Ok`.
|
|
411
|
-
*/
|
|
412
|
-
expectErr(msg: string): E;
|
|
413
|
-
|
|
414
|
-
/**
|
|
415
|
-
* Returns the contained `Err` value.
|
|
416
|
-
* @throws {TypeError} Throws an error if the result is an `Ok`.
|
|
417
|
-
*/
|
|
418
|
-
unwrapErr(): E;
|
|
419
|
-
|
|
420
|
-
// #endregion
|
|
421
|
-
|
|
422
|
-
// #region Transforming contained values
|
|
423
|
-
|
|
424
|
-
/**
|
|
425
|
-
* These methods transform `Result` to `Option`:
|
|
426
|
-
*/
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* Converts from `Result<T, E>` to `Option<T>`.
|
|
430
|
-
* If the result is `Ok`, returns `Some(T)`.
|
|
431
|
-
* If the result is `Err`, returns `None`.
|
|
432
|
-
*/
|
|
433
|
-
ok(): Option<T>;
|
|
434
|
-
|
|
435
|
-
/**
|
|
436
|
-
* Converts from `Result<T, E>` to `Option<E>`.
|
|
437
|
-
* If the result is `Err`, returns `Some(E)`.
|
|
438
|
-
* If the result is `Ok`, returns `None`.
|
|
439
|
-
*/
|
|
440
|
-
err(): Option<E>;
|
|
441
|
-
|
|
442
|
-
/**
|
|
443
|
-
* Transposes a `Result` of an `Option` into an `Option` of a `Result`.
|
|
444
|
-
* @typeParam T - The type of the success value in the `Ok` variant of the `Option`.
|
|
445
|
-
* @returns `Some` containing `Ok` if the result is `Ok` containing `Some`,
|
|
446
|
-
* `Some` containing `Err` if the result is `Err`,
|
|
447
|
-
* `None` if the result is `Ok` containing `None`.
|
|
448
|
-
*/
|
|
449
|
-
transpose<T>(this: Result<Option<T>, E>): Option<Result<T, E>>;
|
|
450
|
-
|
|
451
|
-
/**
|
|
452
|
-
* This method transforms the contained value of the `Ok` variant:
|
|
453
|
-
*/
|
|
454
|
-
|
|
455
|
-
/**
|
|
456
|
-
* Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value,
|
|
457
|
-
* leaving an `Err` value untouched.
|
|
458
|
-
* @typeParam U - The type of the value returned by the map function.
|
|
459
|
-
* @param fn - A function that takes the `Ok` value and returns a new value.
|
|
460
|
-
*/
|
|
461
|
-
map<U>(fn: (value: T) => U): Result<U, E>;
|
|
462
|
-
|
|
463
|
-
/**
|
|
464
|
-
* This method transforms the contained value of the `Err` variant:
|
|
465
|
-
*/
|
|
466
|
-
|
|
467
|
-
/**
|
|
468
|
-
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value,
|
|
469
|
-
* leaving an `Ok` value untouched.
|
|
470
|
-
* @typeParam F - The type of the error returned by the map function.
|
|
471
|
-
* @param fn - A function that takes the `Err` value and returns a new error value.
|
|
472
|
-
*/
|
|
473
|
-
mapErr<F>(fn: (error: E) => F): Result<T, F>;
|
|
474
|
-
|
|
475
|
-
/**
|
|
476
|
-
* These methods transform a `Result<T, E>` into a value of a possibly different type `U`:
|
|
477
|
-
*/
|
|
478
|
-
|
|
479
|
-
/**
|
|
480
|
-
* Maps a `Result<T, E>` to `U` by applying a function to the contained `Ok` value (if `Ok`), or returns the provided default (if `Err`).
|
|
481
|
-
* @typeParam U - The type of the value returned by the map function or the default value.
|
|
482
|
-
* @param defaultValue - The value to return if the result is `Err`.
|
|
483
|
-
* @param fn - A function that takes the `Ok` value and returns a new value.
|
|
484
|
-
*/
|
|
485
|
-
mapOr<U>(defaultValue: U, fn: (value: T) => U): U;
|
|
486
|
-
|
|
487
|
-
/**
|
|
488
|
-
* Maps a `Result<T, E>` to `U` by applying a function to the contained `Ok` value (if `Ok`), or computes a default (if `Err`).
|
|
489
|
-
* @typeParam U - The type of the value returned by the map function or the default function.
|
|
490
|
-
* @param defaultFn - A function that returns the default value.
|
|
491
|
-
* @param fn - A function that takes the `Ok` value and returns a new value.
|
|
492
|
-
*/
|
|
493
|
-
mapOrElse<U>(defaultFn: (error: E) => U, fn: (value: T) => U): U;
|
|
494
|
-
|
|
495
|
-
/**
|
|
496
|
-
* Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
|
|
497
|
-
* If the result is `Ok(Ok(T))`, returns `Ok(T)`.
|
|
498
|
-
* If the result is `Ok(Err(E))` or `Err(E)`, returns `Err(E)`.
|
|
499
|
-
*/
|
|
500
|
-
flatten<T>(this: Result<Result<T, E>, E>): Result<T, E>;
|
|
501
|
-
|
|
502
|
-
// #endregion
|
|
503
|
-
|
|
504
|
-
// #region Boolean operators
|
|
505
|
-
|
|
506
|
-
/**
|
|
507
|
-
* These methods treat the `Result` as a boolean value, where `Ok` acts like `true` and `Err` acts like `false`.
|
|
508
|
-
*/
|
|
509
|
-
|
|
510
|
-
/**
|
|
511
|
-
* Returns `this` if the result is `Err`, otherwise returns the passed `Result`.
|
|
512
|
-
* @typeParam U - The type of the value in the other `Result`.
|
|
513
|
-
* @param other - The `Result` to return if `this` is `Ok`.
|
|
514
|
-
* @returns The passed `Result` if `this` is `Ok`, otherwise returns `this` (which is `Err`).
|
|
515
|
-
*/
|
|
516
|
-
and<U>(other: Result<U, E>): Result<U, E>;
|
|
517
|
-
|
|
518
|
-
/**
|
|
519
|
-
* Returns `this` if it is `Ok`, otherwise returns the passed `Result`.
|
|
520
|
-
* @typeParam F - The type of the error in the other `Result`.
|
|
521
|
-
* @param other - The `Result` to return if `this` is `Err`.
|
|
522
|
-
* @returns `this` if it is `Ok`, otherwise returns `other`.
|
|
523
|
-
*/
|
|
524
|
-
or<F>(other: Result<T, F>): Result<T, F>;
|
|
525
|
-
|
|
526
|
-
/**
|
|
527
|
-
* Calls the provided function with the contained value if `this` is `Ok`, otherwise returns `this` as `Err`.
|
|
528
|
-
* @typeParam U - The type of the value returned by the function.
|
|
529
|
-
* @param fn - A function that takes the `Ok` value and returns a `Result`.
|
|
530
|
-
* @returns The result of `fn` if `this` is `Ok`, otherwise `this` as `Err`.
|
|
531
|
-
*/
|
|
532
|
-
andThen<U>(fn: (value: T) => Result<U, E>): Result<U, E>;
|
|
533
|
-
|
|
534
|
-
/**
|
|
535
|
-
* Calls the provided function with the contained error if `this` is `Err`, otherwise returns `this` as `Ok`.
|
|
536
|
-
* @typeParam F - The type of the error returned by the function.
|
|
537
|
-
* @param fn - A function that takes the `Err` value and returns a `Result`.
|
|
538
|
-
* @returns The result of `fn` if `this` is `Err`, otherwise `this` as `Ok`.
|
|
539
|
-
*/
|
|
540
|
-
orElse<F>(fn: (error: E) => Result<T, F>): Result<T, F>;
|
|
541
|
-
|
|
542
|
-
// #endregion
|
|
543
|
-
|
|
544
|
-
/**
|
|
545
|
-
* Calls the provided function with the contained value if `this` is `Ok`, for side effects only.
|
|
546
|
-
* Does not modify the `Result`.
|
|
547
|
-
* @param fn - A function to call with the `Ok` value.
|
|
548
|
-
* @returns `this`, unmodified.
|
|
549
|
-
*/
|
|
550
|
-
inspect(fn: (value: T) => void): this;
|
|
551
|
-
|
|
552
|
-
/**
|
|
553
|
-
* Calls the provided function with the contained error if `this` is `Err`, for side effects only.
|
|
554
|
-
* Does not modify the `Result`.
|
|
555
|
-
* @param fn - A function to call with the `Err` value.
|
|
556
|
-
* @returns `this`, unmodified.
|
|
557
|
-
*/
|
|
558
|
-
inspectErr(fn: (error: E) => void): this;
|
|
559
|
-
|
|
560
|
-
// #region Equals comparison
|
|
561
|
-
|
|
562
|
-
/**
|
|
563
|
-
* Tests whether `this` and `other` are both `Ok` containing equal values, or both are `Err` containing equal errors.
|
|
564
|
-
* @param other - The other `Result` to compare with.
|
|
565
|
-
* @returns `true` if `this` and `other` are both `Ok` with equal values, or both are `Err` with equal errors, otherwise `false`.
|
|
566
|
-
*/
|
|
567
|
-
eq(other: Result<T, E>): boolean;
|
|
568
|
-
|
|
569
|
-
// #endregion
|
|
570
|
-
|
|
571
|
-
/**
|
|
572
|
-
* Transforms the current Result into a new Result where the type of the error result is replaced with a new type `F`.
|
|
573
|
-
* The type of the success result remains unchanged.
|
|
574
|
-
* Just same as `result as unknown as Result<T, F>`.
|
|
575
|
-
*
|
|
576
|
-
* @typeParam F - The new type for the error result.
|
|
577
|
-
* @returns `this` but the error result type is `F`.
|
|
578
|
-
*/
|
|
579
|
-
asOk<F>(): Result<T, F>;
|
|
580
|
-
|
|
581
|
-
/**
|
|
582
|
-
* Transforms the current Result into a new Result where the type of the success result is replaced with a new type `U`.
|
|
583
|
-
* The type of the error result remains unchanged.
|
|
584
|
-
* Useful where you need to return an Error chained to another type.
|
|
585
|
-
* Just same as `result as unknown as Result<U, E>`.
|
|
586
|
-
*
|
|
587
|
-
* @typeParam U - The new type for the success result.
|
|
588
|
-
* @returns `this` but the success result type is `U`.
|
|
589
|
-
*/
|
|
590
|
-
asErr<U>(): Result<U, E>;
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
/**
|
|
594
|
-
* Export some commonly used types.
|
|
595
|
-
*/
|
|
596
|
-
|
|
597
|
-
/**
|
|
598
|
-
* Represents an asynchronous operation that yields an `Option<T>`.
|
|
599
|
-
* This is a promise that resolves to either `Some(T)` if the value is present, or `None` if the value is absent.
|
|
600
|
-
*
|
|
601
|
-
* @typeParam T - The type of the value that may be contained within the `Option`.
|
|
602
|
-
*/
|
|
603
|
-
export type AsyncOption<T> = Promise<Option<T>>;
|
|
604
|
-
|
|
605
|
-
/**
|
|
606
|
-
* Represents an asynchronous operation that yields a `Result<T, E>`.
|
|
607
|
-
* This is a promise that resolves to `Ok(T)` if the operation was successful, or `Err(E)` if there was an error.
|
|
608
|
-
*
|
|
609
|
-
* @typeParam T - The type of the value that is produced by a successful operation.
|
|
610
|
-
* @typeParam E - The type of the error that may be produced by a failed operation.
|
|
611
|
-
*/
|
|
612
|
-
export type AsyncResult<T, E> = Promise<Result<T, E>>;
|
|
613
|
-
|
|
614
|
-
/**
|
|
615
|
-
* Represents a synchronous operation that yields a `Result<T, Error>`.
|
|
616
|
-
* This is a result that is either `Ok(T)` if the operation was successful, or `Err(Error)` if there was an error.
|
|
617
|
-
*
|
|
618
|
-
* @typeParam T - The type of the value that is produced by a successful operation.
|
|
619
|
-
*/
|
|
620
|
-
export type IOResult<T> = Result<T, Error>;
|
|
621
|
-
|
|
622
|
-
/**
|
|
623
|
-
* Represents an asynchronous I/O operation that yields a `Result<T, Error>`.
|
|
624
|
-
* This is a promise that resolves to `Ok(T)` if the I/O operation was successful, or `Err(Error)` if there was an error.
|
|
625
|
-
*
|
|
626
|
-
* @typeParam T - The type of the value that is produced by a successful I/O operation.
|
|
627
|
-
*/
|
|
628
|
-
export type AsyncIOResult<T> = Promise<IOResult<T>>;
|
|
629
|
-
|
|
630
39
|
/**
|
|
631
40
|
* Creates an `Option<T>` representing the presence of a value.
|
|
632
41
|
* This function is typically used to construct an `Option` that contains a value, indicating that the operation yielding the value was successful.
|
|
@@ -645,45 +54,72 @@ export type AsyncIOResult<T> = Promise<IOResult<T>>;
|
|
|
645
54
|
*/
|
|
646
55
|
export function Some<T>(value: T): Option<T> {
|
|
647
56
|
const some: Option<T> = {
|
|
648
|
-
[
|
|
57
|
+
[Symbol.toStringTag]: 'Option',
|
|
58
|
+
[OptionKindSymbol]: 'Some',
|
|
649
59
|
|
|
650
|
-
isSome
|
|
651
|
-
|
|
652
|
-
|
|
60
|
+
isSome(): true {
|
|
61
|
+
return true;
|
|
62
|
+
},
|
|
63
|
+
isNone(): false {
|
|
64
|
+
return false;
|
|
65
|
+
},
|
|
66
|
+
isSomeAnd(predicate: (value: T) => boolean): boolean {
|
|
67
|
+
return predicate(value);
|
|
68
|
+
},
|
|
653
69
|
|
|
654
|
-
expect
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
70
|
+
expect(_msg: string): T {
|
|
71
|
+
return value;
|
|
72
|
+
},
|
|
73
|
+
unwrap(): T {
|
|
74
|
+
return value;
|
|
75
|
+
},
|
|
76
|
+
unwrapOr(_defaultValue: T): T {
|
|
77
|
+
return value;
|
|
78
|
+
},
|
|
79
|
+
unwrapOrElse(_fn: () => T): T {
|
|
80
|
+
return value;
|
|
81
|
+
},
|
|
658
82
|
|
|
659
|
-
okOr
|
|
660
|
-
|
|
661
|
-
|
|
83
|
+
okOr<E>(_error: E): Result<T, E> {
|
|
84
|
+
return Ok(value);
|
|
85
|
+
},
|
|
86
|
+
okOrElse<E>(_err: () => E): Result<T, E> {
|
|
87
|
+
return Ok(value);
|
|
88
|
+
},
|
|
89
|
+
transpose<T, E>(): Result<Option<T>, E> {
|
|
662
90
|
const r = value as unknown as Result<T, E>;
|
|
663
91
|
assertResult(r);
|
|
664
92
|
return r.isOk() ? Ok(Some(r.unwrap())) : Err(r.unwrapErr());
|
|
665
93
|
},
|
|
666
94
|
|
|
667
|
-
filter
|
|
668
|
-
|
|
95
|
+
filter(predicate: (value: T) => boolean): Option<T> {
|
|
96
|
+
return predicate(value) ? some : None;
|
|
97
|
+
},
|
|
98
|
+
flatten<T>(): Option<T> {
|
|
669
99
|
const o = value as unknown as Option<T>;
|
|
670
100
|
assertOption(o);
|
|
671
101
|
return o;
|
|
672
102
|
},
|
|
673
|
-
map
|
|
103
|
+
map<U>(fn: (value: T) => U): Option<U> {
|
|
104
|
+
return Some(fn(value));
|
|
105
|
+
},
|
|
674
106
|
|
|
675
|
-
mapOr
|
|
676
|
-
|
|
107
|
+
mapOr<U>(_defaultValue: U, fn: (value: T) => U): U {
|
|
108
|
+
return fn(value);
|
|
109
|
+
},
|
|
110
|
+
mapOrElse<U>(_defaultFn: () => U, fn: (value: T) => U): U {
|
|
111
|
+
return fn(value);
|
|
112
|
+
},
|
|
677
113
|
|
|
678
|
-
zip
|
|
114
|
+
zip<U>(other: Option<U>): Option<[T, U]> {
|
|
679
115
|
assertOption(other);
|
|
680
116
|
return other.isSome() ? Some([value, other.unwrap()]) : None;
|
|
681
117
|
},
|
|
682
|
-
zipWith
|
|
118
|
+
zipWith<U, R>(other: Option<U>, fn: (value: T, otherValue: U) => R): Option<R> {
|
|
683
119
|
assertOption(other);
|
|
684
120
|
return other.isSome() ? Some(fn(value, other.unwrap())) : None;
|
|
685
121
|
},
|
|
686
|
-
unzip
|
|
122
|
+
unzip<T, U>(): [Option<T>, Option<U>] {
|
|
687
123
|
const tuple = value as unknown as [T, U];
|
|
688
124
|
|
|
689
125
|
if (!Array.isArray(tuple) || tuple.length !== 2) {
|
|
@@ -694,27 +130,37 @@ export function Some<T>(value: T): Option<T> {
|
|
|
694
130
|
return [Some(a), Some(b)];
|
|
695
131
|
},
|
|
696
132
|
|
|
697
|
-
and
|
|
133
|
+
and<U>(other: Option<U>): Option<U> {
|
|
698
134
|
assertOption(other);
|
|
699
135
|
return other;
|
|
700
136
|
},
|
|
701
|
-
andThen
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
137
|
+
andThen<U>(fn: (value: T) => Option<U>): Option<U> {
|
|
138
|
+
return fn(value);
|
|
139
|
+
},
|
|
140
|
+
or(_other: Option<T>): Option<T> {
|
|
141
|
+
return some;
|
|
142
|
+
},
|
|
143
|
+
orElse(_fn: () => Option<T>): Option<T> {
|
|
144
|
+
return some;
|
|
145
|
+
},
|
|
146
|
+
xor(other: Option<T>): Option<T> {
|
|
705
147
|
assertOption(other);
|
|
706
148
|
return other.isSome() ? None : some;
|
|
707
149
|
},
|
|
708
150
|
|
|
709
|
-
inspect
|
|
151
|
+
inspect(fn: (value: T) => void): Option<T> {
|
|
710
152
|
fn(value);
|
|
711
153
|
return some;
|
|
712
154
|
},
|
|
713
155
|
|
|
714
|
-
eq
|
|
156
|
+
eq(other: Option<T>): boolean {
|
|
715
157
|
assertOption(other);
|
|
716
158
|
return other.isSome() && other.unwrap() === value;
|
|
717
159
|
},
|
|
160
|
+
|
|
161
|
+
toString(): string {
|
|
162
|
+
return `Some(${ value })`;
|
|
163
|
+
},
|
|
718
164
|
} as const;
|
|
719
165
|
|
|
720
166
|
return some;
|
|
@@ -725,54 +171,99 @@ export function Some<T>(value: T): Option<T> {
|
|
|
725
171
|
* This constant is frozen to ensure it is immutable and cannot be altered, preserving the integrity of `None` throughout the application.
|
|
726
172
|
*/
|
|
727
173
|
export const None = Object.freeze<None>({
|
|
728
|
-
[
|
|
174
|
+
[Symbol.toStringTag]: 'Option',
|
|
175
|
+
[OptionKindSymbol]: 'None',
|
|
729
176
|
|
|
730
|
-
isSome
|
|
731
|
-
|
|
732
|
-
|
|
177
|
+
isSome(): false {
|
|
178
|
+
return false;
|
|
179
|
+
},
|
|
180
|
+
isNone(): true {
|
|
181
|
+
return true;
|
|
182
|
+
},
|
|
183
|
+
isSomeAnd(_predicate: (value: never) => boolean): false {
|
|
184
|
+
return false;
|
|
185
|
+
},
|
|
733
186
|
|
|
734
|
-
expect
|
|
187
|
+
expect(msg: string): never {
|
|
735
188
|
throw new TypeError(msg);
|
|
736
189
|
},
|
|
737
|
-
unwrap
|
|
190
|
+
unwrap(): never {
|
|
738
191
|
throw new TypeError('Called `Option::unwrap()` on a `None` value');
|
|
739
192
|
},
|
|
740
|
-
unwrapOr
|
|
741
|
-
|
|
193
|
+
unwrapOr<T>(defaultValue: T): T {
|
|
194
|
+
return defaultValue;
|
|
195
|
+
},
|
|
196
|
+
unwrapOrElse<T>(fn: () => T): T {
|
|
197
|
+
return fn();
|
|
198
|
+
},
|
|
742
199
|
|
|
743
|
-
okOr
|
|
744
|
-
|
|
745
|
-
|
|
200
|
+
okOr<E>(error: E): Result<never, E> {
|
|
201
|
+
return Err(error);
|
|
202
|
+
},
|
|
203
|
+
okOrElse<E>(err: () => E): Result<never, E> {
|
|
204
|
+
return Err(err());
|
|
205
|
+
},
|
|
206
|
+
transpose(): Result<None, never> {
|
|
207
|
+
return Ok(None);
|
|
208
|
+
},
|
|
746
209
|
|
|
747
|
-
filter
|
|
748
|
-
|
|
749
|
-
|
|
210
|
+
filter(_predicate: (value: never) => boolean): None {
|
|
211
|
+
return None;
|
|
212
|
+
},
|
|
213
|
+
flatten(): None {
|
|
214
|
+
return None;
|
|
215
|
+
},
|
|
216
|
+
map<U>(_fn: (value: never) => U): None {
|
|
217
|
+
return None;
|
|
218
|
+
},
|
|
750
219
|
|
|
751
|
-
mapOr
|
|
752
|
-
|
|
220
|
+
mapOr<U>(defaultValue: U, _fn: (value: never) => U): U {
|
|
221
|
+
return defaultValue;
|
|
222
|
+
},
|
|
223
|
+
mapOrElse<U>(defaultFn: () => U, _fn: (value: never) => U): U {
|
|
224
|
+
return defaultFn();
|
|
225
|
+
},
|
|
753
226
|
|
|
754
|
-
zip
|
|
755
|
-
|
|
756
|
-
|
|
227
|
+
zip<U>(_other: Option<U>): None {
|
|
228
|
+
return None;
|
|
229
|
+
},
|
|
230
|
+
zipWith<U, R>(_other: Option<U>, _fn: (value: never, otherValue: U) => R): None {
|
|
231
|
+
return None;
|
|
232
|
+
},
|
|
233
|
+
unzip(): [None, None] {
|
|
234
|
+
return [None, None];
|
|
235
|
+
},
|
|
757
236
|
|
|
758
|
-
and
|
|
759
|
-
|
|
760
|
-
|
|
237
|
+
and<U>(_other: Option<U>): None {
|
|
238
|
+
return None;
|
|
239
|
+
},
|
|
240
|
+
andThen<U>(_fn: (value: never) => Option<U>): None {
|
|
241
|
+
return None;
|
|
242
|
+
},
|
|
243
|
+
or<T>(other: Option<T>): Option<T> {
|
|
761
244
|
assertOption(other);
|
|
762
245
|
return other;
|
|
763
246
|
},
|
|
764
|
-
orElse
|
|
765
|
-
|
|
247
|
+
orElse<T>(fn: () => Option<T>): Option<T> {
|
|
248
|
+
return fn();
|
|
249
|
+
},
|
|
250
|
+
xor<T>(other: Option<T>): Option<T> {
|
|
766
251
|
assertOption(other);
|
|
767
252
|
return other.isSome() ? other : None;
|
|
768
253
|
},
|
|
769
254
|
|
|
770
|
-
inspect
|
|
255
|
+
inspect(_fn: (value: never) => void): None {
|
|
256
|
+
return None;
|
|
257
|
+
},
|
|
771
258
|
|
|
772
|
-
eq
|
|
259
|
+
eq<T>(other: Option<T>): boolean {
|
|
773
260
|
assertOption(other);
|
|
774
261
|
return other === None;
|
|
775
262
|
},
|
|
263
|
+
|
|
264
|
+
toString(): string {
|
|
265
|
+
return 'None';
|
|
266
|
+
},
|
|
776
267
|
}) as None;
|
|
777
268
|
|
|
778
269
|
/**
|
|
@@ -794,58 +285,95 @@ export const None = Object.freeze<None>({
|
|
|
794
285
|
*/
|
|
795
286
|
export function Ok<T, E>(value: T): Result<T, E> {
|
|
796
287
|
const ok: Result<T, E> = {
|
|
797
|
-
[
|
|
288
|
+
[Symbol.toStringTag]: 'Result',
|
|
289
|
+
[ResultKindSymbol]: 'Ok',
|
|
798
290
|
|
|
799
|
-
isOk
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
291
|
+
isOk(): true {
|
|
292
|
+
return true;
|
|
293
|
+
},
|
|
294
|
+
isErr(): false {
|
|
295
|
+
return false;
|
|
296
|
+
},
|
|
297
|
+
isOkAnd(predicate: (value: T) => boolean): boolean {
|
|
298
|
+
return predicate(value);
|
|
299
|
+
},
|
|
300
|
+
isErrAnd(_predicate: (error: E) => boolean): false {
|
|
301
|
+
return false;
|
|
302
|
+
},
|
|
803
303
|
|
|
804
|
-
expect
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
304
|
+
expect(_msg: string): T {
|
|
305
|
+
return value;
|
|
306
|
+
},
|
|
307
|
+
unwrap(): T {
|
|
308
|
+
return value;
|
|
309
|
+
},
|
|
310
|
+
unwrapOr(_defaultValue: T): T {
|
|
311
|
+
return value;
|
|
312
|
+
},
|
|
313
|
+
unwrapOrElse(_fn: (error: E) => T): T {
|
|
314
|
+
return value;
|
|
315
|
+
},
|
|
808
316
|
|
|
809
|
-
expectErr
|
|
317
|
+
expectErr(msg: string): E {
|
|
810
318
|
throw new TypeError(`${ msg }: ${ value }`);
|
|
811
319
|
},
|
|
812
|
-
unwrapErr
|
|
320
|
+
unwrapErr(): E {
|
|
813
321
|
throw new TypeError('Called `Result::unwrapErr()` on an `Ok` value');
|
|
814
322
|
},
|
|
815
323
|
|
|
816
|
-
ok
|
|
817
|
-
|
|
818
|
-
|
|
324
|
+
ok(): Option<T> {
|
|
325
|
+
return Some(value);
|
|
326
|
+
},
|
|
327
|
+
err(): None {
|
|
328
|
+
return None;
|
|
329
|
+
},
|
|
330
|
+
transpose<T>(): Option<Result<T, E>> {
|
|
819
331
|
const o = value as Option<T>;
|
|
820
332
|
assertOption(o);
|
|
821
333
|
return o.isSome() ? Some(Ok(o.unwrap())) : None;
|
|
822
334
|
},
|
|
823
335
|
|
|
824
|
-
map
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
336
|
+
map<U>(fn: (value: T) => U): Result<U, E> {
|
|
337
|
+
return Ok(fn(value));
|
|
338
|
+
},
|
|
339
|
+
mapErr<F>(_fn: (error: E) => F): Result<T, F> {
|
|
340
|
+
return Ok(value);
|
|
341
|
+
},
|
|
342
|
+
mapOr<U>(_defaultValue: U, fn: (value: T) => U): U {
|
|
343
|
+
return fn(value);
|
|
344
|
+
},
|
|
345
|
+
mapOrElse<U>(_defaultFn: (error: E) => U, fn: (value: T) => U): U {
|
|
346
|
+
return fn(value);
|
|
347
|
+
},
|
|
348
|
+
flatten<T>(): Result<T, E> {
|
|
829
349
|
const r = value as Result<T, E>;
|
|
830
350
|
assertResult(r);
|
|
831
351
|
return r;
|
|
832
352
|
},
|
|
833
353
|
|
|
834
|
-
and
|
|
354
|
+
and<U>(other: Result<U, E>): Result<U, E> {
|
|
835
355
|
assertResult(other);
|
|
836
356
|
return other;
|
|
837
357
|
},
|
|
838
|
-
or
|
|
839
|
-
|
|
840
|
-
|
|
358
|
+
or<F>(_other: Result<T, F>): Result<T, F> {
|
|
359
|
+
return ok as unknown as Result<T, F>;
|
|
360
|
+
},
|
|
361
|
+
andThen<U>(fn: (value: T) => Result<U, E>): Result<U, E> {
|
|
362
|
+
return fn(value);
|
|
363
|
+
},
|
|
364
|
+
orElse<F>(_fn: (error: E) => Result<T, F>): Result<T, F> {
|
|
365
|
+
return ok as unknown as Result<T, F>;
|
|
366
|
+
},
|
|
841
367
|
|
|
842
|
-
inspect
|
|
368
|
+
inspect(fn: (value: T) => void): Result<T, E> {
|
|
843
369
|
fn(value);
|
|
844
370
|
return ok;
|
|
845
371
|
},
|
|
846
|
-
inspectErr
|
|
372
|
+
inspectErr(_fn: (error: E) => void): Result<T, E> {
|
|
373
|
+
return ok;
|
|
374
|
+
},
|
|
847
375
|
|
|
848
|
-
eq
|
|
376
|
+
eq(other: Result<T, E>): boolean {
|
|
849
377
|
assertResult(other);
|
|
850
378
|
return other.isOk() && other.unwrap() === value;
|
|
851
379
|
},
|
|
@@ -856,6 +384,10 @@ export function Ok<T, E>(value: T): Result<T, E> {
|
|
|
856
384
|
asErr(): never {
|
|
857
385
|
throw new TypeError('Called `Result::asErr()` on an `Ok` value');
|
|
858
386
|
},
|
|
387
|
+
|
|
388
|
+
toString(): string {
|
|
389
|
+
return `Ok(${ value })`;
|
|
390
|
+
},
|
|
859
391
|
} as const;
|
|
860
392
|
|
|
861
393
|
return ok;
|
|
@@ -880,50 +412,91 @@ export function Ok<T, E>(value: T): Result<T, E> {
|
|
|
880
412
|
*/
|
|
881
413
|
export function Err<T, E>(error: E): Result<T, E> {
|
|
882
414
|
const err: Result<T, E> = {
|
|
883
|
-
[
|
|
415
|
+
[Symbol.toStringTag]: 'Result',
|
|
416
|
+
[ResultKindSymbol]: 'Err',
|
|
884
417
|
|
|
885
|
-
isOk
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
418
|
+
isOk(): false {
|
|
419
|
+
return false;
|
|
420
|
+
},
|
|
421
|
+
isErr(): true {
|
|
422
|
+
return true;
|
|
423
|
+
},
|
|
424
|
+
isOkAnd(_predicate: (value: T) => boolean): false {
|
|
425
|
+
return false;
|
|
426
|
+
},
|
|
427
|
+
isErrAnd(predicate: (error: E) => boolean): boolean {
|
|
428
|
+
return predicate(error);
|
|
429
|
+
},
|
|
889
430
|
|
|
890
|
-
expect
|
|
431
|
+
expect(msg: string): T {
|
|
891
432
|
throw new TypeError(`${ msg }: ${ error }`);
|
|
892
433
|
},
|
|
893
|
-
unwrap
|
|
434
|
+
unwrap(): T {
|
|
894
435
|
throw new TypeError('Called `Result::unwrap()` on an `Err` value');
|
|
895
436
|
},
|
|
896
|
-
unwrapOr
|
|
897
|
-
|
|
437
|
+
unwrapOr(defaultValue: T): T {
|
|
438
|
+
return defaultValue;
|
|
439
|
+
},
|
|
440
|
+
unwrapOrElse(fn: (error: E) => T): T {
|
|
441
|
+
return fn(error);
|
|
442
|
+
},
|
|
898
443
|
|
|
899
|
-
expectErr
|
|
900
|
-
|
|
444
|
+
expectErr(_msg: string): E {
|
|
445
|
+
return error;
|
|
446
|
+
},
|
|
447
|
+
unwrapErr(): E {
|
|
448
|
+
return error;
|
|
449
|
+
},
|
|
901
450
|
|
|
902
|
-
ok
|
|
903
|
-
|
|
904
|
-
|
|
451
|
+
ok(): None {
|
|
452
|
+
return None;
|
|
453
|
+
},
|
|
454
|
+
err(): Option<E> {
|
|
455
|
+
return Some(error);
|
|
456
|
+
},
|
|
457
|
+
transpose<T>(): Option<Result<T, E>> {
|
|
458
|
+
return Some(err as unknown as Result<T, E>);
|
|
459
|
+
},
|
|
905
460
|
|
|
906
|
-
map
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
461
|
+
map<U>(_fn: (value: T) => U): Result<U, E> {
|
|
462
|
+
return err as unknown as Result<U, E>;
|
|
463
|
+
},
|
|
464
|
+
mapErr<F>(fn: (error: E) => F): Result<T, F> {
|
|
465
|
+
return Err(fn(error));
|
|
466
|
+
},
|
|
467
|
+
mapOr<U>(defaultValue: U, _fn: (value: T) => U): U {
|
|
468
|
+
return defaultValue;
|
|
469
|
+
},
|
|
470
|
+
mapOrElse<U>(defaultFn: (error: E) => U, _fn: (value: T) => U): U {
|
|
471
|
+
return defaultFn(error);
|
|
472
|
+
},
|
|
473
|
+
flatten<T>(): Result<T, E> {
|
|
474
|
+
return err as unknown as Result<T, E>;
|
|
475
|
+
},
|
|
911
476
|
|
|
912
|
-
and
|
|
913
|
-
|
|
477
|
+
and<U>(_other: Result<U, E>): Result<U, E> {
|
|
478
|
+
return err as unknown as Result<U, E>;
|
|
479
|
+
},
|
|
480
|
+
or<F>(other: Result<T, F>): Result<T, F> {
|
|
914
481
|
assertResult(other);
|
|
915
482
|
return other;
|
|
916
483
|
},
|
|
917
|
-
andThen
|
|
918
|
-
|
|
484
|
+
andThen<U>(_fn: (value: T) => Result<U, E>): Result<U, E> {
|
|
485
|
+
return err as unknown as Result<U, E>;
|
|
486
|
+
},
|
|
487
|
+
orElse<F>(fn: (error: E) => Result<T, F>): Result<T, F> {
|
|
488
|
+
return fn(error);
|
|
489
|
+
},
|
|
919
490
|
|
|
920
|
-
inspect
|
|
921
|
-
|
|
491
|
+
inspect(_fn: (value: T) => void): Result<T, E> {
|
|
492
|
+
return err;
|
|
493
|
+
},
|
|
494
|
+
inspectErr(fn: (error: E) => void): Result<T, E> {
|
|
922
495
|
fn(error);
|
|
923
496
|
return err;
|
|
924
497
|
},
|
|
925
498
|
|
|
926
|
-
eq
|
|
499
|
+
eq(other: Result<T, E>): boolean {
|
|
927
500
|
assertResult(other);
|
|
928
501
|
return other.isErr() && other.unwrapErr() === error;
|
|
929
502
|
},
|
|
@@ -934,6 +507,10 @@ export function Err<T, E>(error: E): Result<T, E> {
|
|
|
934
507
|
asErr<U>(): Result<U, E> {
|
|
935
508
|
return err as unknown as Result<U, E>;
|
|
936
509
|
},
|
|
510
|
+
|
|
511
|
+
toString(): string {
|
|
512
|
+
return `Err(${ error })`;
|
|
513
|
+
},
|
|
937
514
|
} as const;
|
|
938
515
|
|
|
939
516
|
return err;
|
|
@@ -947,8 +524,7 @@ export function Err<T, E>(error: E): Result<T, E> {
|
|
|
947
524
|
* @throws {TypeError} If the value is not an `Option`.
|
|
948
525
|
*/
|
|
949
526
|
function assertOption<T>(o: Option<T>): void {
|
|
950
|
-
|
|
951
|
-
if (o == null || typeof o !== 'object' || !(optionKindSymbol in o)) {
|
|
527
|
+
if (!isOption(o)) {
|
|
952
528
|
throw new TypeError(`This(${ o }) is not an Option`);
|
|
953
529
|
}
|
|
954
530
|
}
|
|
@@ -962,37 +538,7 @@ function assertOption<T>(o: Option<T>): void {
|
|
|
962
538
|
* @throws {TypeError} If the value is not a `Result`.
|
|
963
539
|
*/
|
|
964
540
|
function assertResult<T, E>(r: Result<T, E>): void {
|
|
965
|
-
|
|
966
|
-
if (r == null || typeof r !== 'object' || !(resultKindSymbol in r)) {
|
|
541
|
+
if (!isResult(r)) {
|
|
967
542
|
throw new TypeError(`This(${ r }) is not a Result`);
|
|
968
543
|
}
|
|
969
|
-
}
|
|
970
|
-
|
|
971
|
-
/**
|
|
972
|
-
* Converts a Promise to a Result type, capturing the resolved value in an `Ok`, or the error in an `Err`.
|
|
973
|
-
* This allows for promise-based asynchronous operations to be handled in a way that is more in line with the Result pattern.
|
|
974
|
-
*
|
|
975
|
-
* @typeParam T - The type of the value that the promise resolves to.
|
|
976
|
-
* @typeParam E - The type of the error that the promise may reject with, defaults to `Error`.
|
|
977
|
-
* @param p - The promise to convert into a `Result` type.
|
|
978
|
-
* @returns A promise that resolves to a `Result<T, E>`. If the input promise `p` resolves, the resulting promise will resolve with `Ok<T>`. If the input promise `p` rejects, the resulting promise will resolve with `Err<E>`.
|
|
979
|
-
*
|
|
980
|
-
* @example
|
|
981
|
-
* ```ts
|
|
982
|
-
* async function example() {
|
|
983
|
-
* const result = await promiseToResult(fetchData());
|
|
984
|
-
* if (result.isOk()) {
|
|
985
|
-
* console.log('Data:', result.unwrap());
|
|
986
|
-
* } else {
|
|
987
|
-
* console.error('Error:', result.unwrapErr());
|
|
988
|
-
* }
|
|
989
|
-
* }
|
|
990
|
-
* ```
|
|
991
|
-
*/
|
|
992
|
-
export function promiseToResult<T, E = Error>(p: Promise<T>): Promise<Result<T, E>> {
|
|
993
|
-
return p.then((x): Result<T, E> => {
|
|
994
|
-
return Ok(x);
|
|
995
|
-
}).catch((err: E): Result<T, E> => {
|
|
996
|
-
return Err(err);
|
|
997
|
-
});
|
|
998
544
|
}
|