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/core.ts
ADDED
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview
|
|
3
|
+
* A Rust-inspired [Option](https://doc.rust-lang.org/core/option/index.html) enum type, used as an alternative to the use of null and undefined.
|
|
4
|
+
*
|
|
5
|
+
* And [Result](https://doc.rust-lang.org/core/result/index.html) enum type, used for better error handling.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { OptionKindSymbol, ResultKindSymbol } from './symbols.ts';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Type `Option` represents an optional value: every `Option` is either `Some` and contains a value, or `None`, and does not.
|
|
13
|
+
* This interface includes methods that act on the `Option` type, similar to Rust's `Option` enum.
|
|
14
|
+
*
|
|
15
|
+
* As Rust Code:
|
|
16
|
+
```rust
|
|
17
|
+
pub enum Option<T> {
|
|
18
|
+
None,
|
|
19
|
+
Some(T),
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
* @typeParam T - The type of the value contained in the `Some` variant.
|
|
23
|
+
*/
|
|
24
|
+
export interface Option<T> {
|
|
25
|
+
// #region Internal properties
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* [object Option].
|
|
29
|
+
*/
|
|
30
|
+
[Symbol.toStringTag]: 'Option',
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Identify `Some` or `None`.
|
|
34
|
+
*
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
37
|
+
readonly [OptionKindSymbol]: 'Some' | 'None';
|
|
38
|
+
|
|
39
|
+
// #endregion
|
|
40
|
+
|
|
41
|
+
// #region Querying the variant
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The `isSome` and `isNone` methods return `true` if the `Option` is `Some` or `None`, respectively.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Returns `true` if the Option is a `Some` value.
|
|
49
|
+
*/
|
|
50
|
+
isSome(): boolean;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Returns `true` if the Option is a `None` value.
|
|
54
|
+
*/
|
|
55
|
+
isNone(): boolean;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Returns `true` if the Option is a `Some` value and the predicate returns `true` for the contained value.
|
|
59
|
+
* @param predicate - A function that takes the contained value and returns a boolean.
|
|
60
|
+
*/
|
|
61
|
+
isSomeAnd(predicate: (value: T) => boolean): boolean;
|
|
62
|
+
|
|
63
|
+
// #endregion
|
|
64
|
+
|
|
65
|
+
// #region Extracting the contained value
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* These methods extract the contained value in an `Option<T>` when it is the `Some` variant:
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Returns the contained `Some` value, with a provided error message if the value is a `None`.
|
|
73
|
+
* @param msg - The error message to provide if the value is a `None`.
|
|
74
|
+
* @throws {TypeError} Throws an error with the provided message if the Option is a `None`.
|
|
75
|
+
*/
|
|
76
|
+
expect(msg: string): T;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Returns the contained `Some` value.
|
|
80
|
+
* @throws {TypeError} Throws an error if the value is a `None`.
|
|
81
|
+
*/
|
|
82
|
+
unwrap(): T;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Returns the contained `Some` value or a provided default.
|
|
86
|
+
* @param defaultValue - The value to return if the Option is a `None`.
|
|
87
|
+
*/
|
|
88
|
+
unwrapOr(defaultValue: T): T;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Returns the contained `Some` value or computes it from a closure.
|
|
92
|
+
* @param fn - A function that returns the default value.
|
|
93
|
+
*/
|
|
94
|
+
unwrapOrElse(fn: () => T): T;
|
|
95
|
+
|
|
96
|
+
// #endregion
|
|
97
|
+
|
|
98
|
+
// #region Transforming contained values
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* These methods transform `Option` to `Result`:
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
|
|
106
|
+
* @typeParam E - The type of the error value in the `Err` variant of the resulting `Result`.
|
|
107
|
+
* @param error - The error value to use if the Option is a `None`.
|
|
108
|
+
*/
|
|
109
|
+
okOr<E>(error: E): Result<T, E>;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
|
|
113
|
+
* @typeParam E - The type of the error value in the `Err` variant of the resulting `Result`.
|
|
114
|
+
* @param err - A function that returns the error value.
|
|
115
|
+
*/
|
|
116
|
+
okOrElse<E>(err: () => E): Result<T, E>;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Transposes an `Option` of a `Result` into a `Result` of an `Option`.
|
|
120
|
+
* @typeParam T - The type of the success value in the `Ok` variant of the `Result`.
|
|
121
|
+
* @typeParam E - The type of the error value in the `Err` variant of the `Result`.
|
|
122
|
+
* @returns `Ok` containing `Some` if the Option is a `Some` containing `Ok`,
|
|
123
|
+
* `Err` containing the error if the Option is a `Some` containing `Err`,
|
|
124
|
+
* `Ok` containing `None` if the Option is `None`.
|
|
125
|
+
*/
|
|
126
|
+
transpose<T, E>(this: Option<Result<T, E>>): Result<Option<T>, E>;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* These methods transform the `Some` variant:
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Returns `None` if the Option is `None`, otherwise calls predicate with the wrapped value and returns:
|
|
134
|
+
* - `Some(t)` if predicate returns `true` (where `t` is the wrapped value), and
|
|
135
|
+
* - `None` if predicate returns `false`.
|
|
136
|
+
* @param predicate - A function that takes the contained value and returns a boolean.
|
|
137
|
+
*/
|
|
138
|
+
filter(predicate: (value: T) => boolean): Option<T>;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Converts from `Option<Option<T>>` to `Option<T>`.
|
|
142
|
+
* @returns `None` if the Option is `None`, otherwise returns the contained `Option`.
|
|
143
|
+
*/
|
|
144
|
+
flatten<T>(this: Option<Option<T>>): Option<T>;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
|
|
148
|
+
* @typeParam U - The type of the value returned by the map function.
|
|
149
|
+
* @param fn - A function that takes the contained value and returns a new value.
|
|
150
|
+
*/
|
|
151
|
+
map<U>(fn: (value: T) => U): Option<U>;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Maps an `Option<T>` to `U` by applying a function to the contained value (if any), or returns the provided default (if not).
|
|
155
|
+
* @typeParam U - The type of the value returned by the map function or the default value.
|
|
156
|
+
* @param defaultValue - The value to return if the Option is `None`.
|
|
157
|
+
* @param fn - A function that takes the contained value and returns a new value.
|
|
158
|
+
*/
|
|
159
|
+
mapOr<U>(defaultValue: U, fn: (value: T) => U): U;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Maps an `Option<T>` to `U` by applying a function to a contained value (if any), or computes a default (if not).
|
|
163
|
+
* @typeParam U - The type of the value returned by the map function or the default function.
|
|
164
|
+
* @param defaultFn - A function that returns the default value.
|
|
165
|
+
* @param fn - A function that takes the contained value and returns a new value.
|
|
166
|
+
*/
|
|
167
|
+
mapOrElse<U>(defaultFn: () => U, fn: (value: T) => U): U;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* These methods combine the `Some` variants of two `Option` values:
|
|
171
|
+
*/
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Combines `this` with another `Option` by zipping their contained values.
|
|
175
|
+
* If `this` is `Some(s)` and `other` is `Some(o)`, returns `Some([s, o])`.
|
|
176
|
+
* If either `this` or `other` is `None`, returns `None`.
|
|
177
|
+
* @typeParam U - The type of the value in the other `Option`.
|
|
178
|
+
* @param other - The other `Option` to zip with.
|
|
179
|
+
* @returns An `Option` containing a tuple of the values if both are `Some`, otherwise `None`.
|
|
180
|
+
*/
|
|
181
|
+
zip<U>(other: Option<U>): Option<[T, U]>;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Zips `this` with another `Option` using a provided function to combine their contained values.
|
|
185
|
+
* If `this` is `Some(s)` and `other` is `Some(o)`, returns `Some(fn(s, o))`.
|
|
186
|
+
* If either `this` or `other` is `None`, returns `None`.
|
|
187
|
+
* @typeParam U - The type of the value in the other `Option`.
|
|
188
|
+
* @typeParam R - The return type of the combining function.
|
|
189
|
+
* @param other - The other `Option` to zip with.
|
|
190
|
+
* @param fn - The function to combine the values from both `Options`.
|
|
191
|
+
* @returns An `Option` containing the result of `fn` if both `Options` are `Some`, otherwise `None`.
|
|
192
|
+
*/
|
|
193
|
+
zipWith<U, R>(other: Option<U>, fn: (value: T, otherValue: U) => R): Option<R>;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Converts from `Option<[T, U]>` to `[Option<T>, Option<U>]`.
|
|
197
|
+
* If `this` is `Some([a, b])`, returns `[Some(a), Some(b)]`.
|
|
198
|
+
* If `this` is `None`, returns `[None, None]`.
|
|
199
|
+
* @typeParam T - The type of the first value in the tuple.
|
|
200
|
+
* @typeParam U - The type of the second value in the tuple.
|
|
201
|
+
* @returns A tuple of `Options`, one for each element in the original `Option` of a tuple.
|
|
202
|
+
*/
|
|
203
|
+
unzip<T, U>(this: Option<[T, U]>): [Option<T>, Option<U>];
|
|
204
|
+
|
|
205
|
+
// #endregion
|
|
206
|
+
|
|
207
|
+
// #region Boolean operators
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* These methods treat the `Option` as a boolean value, where `Some` acts like `true` and `None` acts like `false`.
|
|
211
|
+
*/
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Returns `None` if the Option is `None`, otherwise returns `other`.
|
|
215
|
+
* This is sometimes called "and then" because it is similar to a logical AND operation.
|
|
216
|
+
* @typeParam U - The type of the value in the other `Option`.
|
|
217
|
+
* @param other - The `Option` to return if `this` is `Some`.
|
|
218
|
+
* @returns `None` if `this` is `None`, otherwise returns `other`.
|
|
219
|
+
*/
|
|
220
|
+
and<U>(other: Option<U>): Option<U>;
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Returns `None` if the Option is `None`, otherwise calls `fn` with the wrapped value and returns the result.
|
|
224
|
+
* This function can be used for control flow based on `Option` values.
|
|
225
|
+
* @typeParam U - The type of the value returned by the function.
|
|
226
|
+
* @param fn - A function that takes the contained value and returns an `Option`.
|
|
227
|
+
* @returns The result of `fn` if `this` is `Some`, otherwise `None`.
|
|
228
|
+
*/
|
|
229
|
+
andThen<U>(fn: (value: T) => Option<U>): Option<U>;
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Returns the Option if it contains a value, otherwise returns `other`.
|
|
233
|
+
* This can be used for providing a fallback `Option`.
|
|
234
|
+
* @param other - The fallback `Option` to use if `this` is `None`.
|
|
235
|
+
* @returns `this` if it is `Some`, otherwise `other`.
|
|
236
|
+
*/
|
|
237
|
+
or(other: Option<T>): Option<T>;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Returns the Option if it contains a value, otherwise calls `fn` and returns the result.
|
|
241
|
+
* This method can be used for lazy fallbacks, as `fn` is only evaluated if `this` is `None`.
|
|
242
|
+
* @param fn - A function that produces an `Option`.
|
|
243
|
+
* @returns `this` if it is `Some`, otherwise the result of `fn`.
|
|
244
|
+
*/
|
|
245
|
+
orElse(fn: () => Option<T>): Option<T>;
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Returns `Some` if exactly one of `this`, `other` is `Some`, otherwise returns `None`.
|
|
249
|
+
* This can be thought of as an exclusive or operation on `Option` values.
|
|
250
|
+
* @param other - The other `Option` to compare with.
|
|
251
|
+
* @returns `Some` if exactly one of `this` and `other` is `Some`, otherwise `None`.
|
|
252
|
+
*/
|
|
253
|
+
xor(other: Option<T>): Option<T>;
|
|
254
|
+
|
|
255
|
+
// #endregion
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Calls the provided function with the contained value if `this` is `Some`.
|
|
259
|
+
* This is primarily for side effects and does not transform the `Option`.
|
|
260
|
+
* @param fn - A function to call with the contained value.
|
|
261
|
+
* @returns `this`, unmodified, for chaining additional methods.
|
|
262
|
+
*/
|
|
263
|
+
inspect(fn: (value: T) => void): this;
|
|
264
|
+
|
|
265
|
+
// #region Equals comparison
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Tests whether `this` and `other` are both `Some` containing equal values, or both are `None`.
|
|
269
|
+
* This method can be used for comparing `Option` instances in a value-sensitive manner.
|
|
270
|
+
* @param other - The other `Option` to compare with.
|
|
271
|
+
* @returns `true` if `this` and `other` are both `Some` with equal values, or both are `None`, otherwise `false`.
|
|
272
|
+
*/
|
|
273
|
+
eq(other: Option<T>): boolean;
|
|
274
|
+
|
|
275
|
+
// #endregion
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Custom `toString` implementation that uses the `Option`'s contained value.
|
|
279
|
+
*/
|
|
280
|
+
toString(): string;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The `Result` type is used for returning and propagating errors.
|
|
285
|
+
* 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.
|
|
286
|
+
* This interface includes methods that act on the `Result` type, similar to Rust's `Result` enum.
|
|
287
|
+
*
|
|
288
|
+
* As Rust Code:
|
|
289
|
+
```rust
|
|
290
|
+
pub enum Result<T, E> {
|
|
291
|
+
Ok(T),
|
|
292
|
+
Err(E),
|
|
293
|
+
}
|
|
294
|
+
```
|
|
295
|
+
* @typeParam T - The type of the value contained in a successful `Result`.
|
|
296
|
+
* @typeParam E - The type of the error contained in an unsuccessful `Result`.
|
|
297
|
+
*/
|
|
298
|
+
export interface Result<T, E> {
|
|
299
|
+
// #region Internal properties
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* [object Result].
|
|
303
|
+
*/
|
|
304
|
+
[Symbol.toStringTag]: 'Result',
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Identify `Ok` or `Err`.
|
|
308
|
+
*
|
|
309
|
+
* @private
|
|
310
|
+
*/
|
|
311
|
+
readonly [ResultKindSymbol]: 'Ok' | 'Err';
|
|
312
|
+
|
|
313
|
+
// #endregion
|
|
314
|
+
|
|
315
|
+
// #region Querying the variant
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* The `isOk` and `isErr` methods return `true` if the `Result` is `Ok` or `Err`, respectively.
|
|
319
|
+
*/
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Returns `true` if the result is `Ok`.
|
|
323
|
+
*/
|
|
324
|
+
isOk(): boolean;
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Returns `true` if the result is `Err`.
|
|
328
|
+
*/
|
|
329
|
+
isErr(): boolean;
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Returns `true` if the result is `Ok` and the provided predicate returns `true` for the contained value.
|
|
333
|
+
* @param predicate - A function that takes the `Ok` value and returns a boolean.
|
|
334
|
+
*/
|
|
335
|
+
isOkAnd(predicate: (value: T) => boolean): boolean;
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Returns `true` if the result is `Err` and the provided predicate returns `true` for the contained error.
|
|
339
|
+
* @param predicate - A function that takes the `Err` value and returns a boolean.
|
|
340
|
+
*/
|
|
341
|
+
isErrAnd(predicate: (error: E) => boolean): boolean;
|
|
342
|
+
|
|
343
|
+
// #endregion
|
|
344
|
+
|
|
345
|
+
// #region Extracting the contained value
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* These methods extract the contained value in a `Result<T, E>` when it is the `Ok` variant.
|
|
349
|
+
*/
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Returns the contained `Ok` value, with a provided error message if the result is `Err`.
|
|
353
|
+
* @param msg - The error message to provide if the result is an `Err`.
|
|
354
|
+
* @throws {TypeError} Throws an error with the provided message if the result is an `Err`.
|
|
355
|
+
*/
|
|
356
|
+
expect(msg: string): T;
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Returns the contained `Ok` value.
|
|
360
|
+
* @throws {TypeError} Throws an error if the result is an `Err`.
|
|
361
|
+
*/
|
|
362
|
+
unwrap(): T;
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Returns the contained `Ok` value or a provided default.
|
|
366
|
+
* @param defaultValue - The value to return if the result is an `Err`.
|
|
367
|
+
*/
|
|
368
|
+
unwrapOr(defaultValue: T): T;
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Returns the contained `Ok` value or computes it from a closure if the result is `Err`.
|
|
372
|
+
* @param fn - A function that takes the `Err` value and returns an `Ok` value.
|
|
373
|
+
*/
|
|
374
|
+
unwrapOrElse(fn: (error: E) => T): T;
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* These methods extract the contained value in a `Result<T, E>` when it is the `Err` variant.
|
|
378
|
+
*/
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Returns the contained `Err` value, with a provided error message if the result is `Ok`.
|
|
382
|
+
* @param msg - The error message to provide if the result is an `Ok`.
|
|
383
|
+
* @throws {TypeError} Throws an error with the provided message if the result is an `Ok`.
|
|
384
|
+
*/
|
|
385
|
+
expectErr(msg: string): E;
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Returns the contained `Err` value.
|
|
389
|
+
* @throws {TypeError} Throws an error if the result is an `Ok`.
|
|
390
|
+
*/
|
|
391
|
+
unwrapErr(): E;
|
|
392
|
+
|
|
393
|
+
// #endregion
|
|
394
|
+
|
|
395
|
+
// #region Transforming contained values
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* These methods transform `Result` to `Option`:
|
|
399
|
+
*/
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Converts from `Result<T, E>` to `Option<T>`.
|
|
403
|
+
* If the result is `Ok`, returns `Some(T)`.
|
|
404
|
+
* If the result is `Err`, returns `None`.
|
|
405
|
+
*/
|
|
406
|
+
ok(): Option<T>;
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Converts from `Result<T, E>` to `Option<E>`.
|
|
410
|
+
* If the result is `Err`, returns `Some(E)`.
|
|
411
|
+
* If the result is `Ok`, returns `None`.
|
|
412
|
+
*/
|
|
413
|
+
err(): Option<E>;
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Transposes a `Result` of an `Option` into an `Option` of a `Result`.
|
|
417
|
+
* @typeParam T - The type of the success value in the `Ok` variant of the `Option`.
|
|
418
|
+
* @returns `Some` containing `Ok` if the result is `Ok` containing `Some`,
|
|
419
|
+
* `Some` containing `Err` if the result is `Err`,
|
|
420
|
+
* `None` if the result is `Ok` containing `None`.
|
|
421
|
+
*/
|
|
422
|
+
transpose<T>(this: Result<Option<T>, E>): Option<Result<T, E>>;
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* This method transforms the contained value of the `Ok` variant:
|
|
426
|
+
*/
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value,
|
|
430
|
+
* leaving an `Err` value untouched.
|
|
431
|
+
* @typeParam U - The type of the value returned by the map function.
|
|
432
|
+
* @param fn - A function that takes the `Ok` value and returns a new value.
|
|
433
|
+
*/
|
|
434
|
+
map<U>(fn: (value: T) => U): Result<U, E>;
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* This method transforms the contained value of the `Err` variant:
|
|
438
|
+
*/
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value,
|
|
442
|
+
* leaving an `Ok` value untouched.
|
|
443
|
+
* @typeParam F - The type of the error returned by the map function.
|
|
444
|
+
* @param fn - A function that takes the `Err` value and returns a new error value.
|
|
445
|
+
*/
|
|
446
|
+
mapErr<F>(fn: (error: E) => F): Result<T, F>;
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* These methods transform a `Result<T, E>` into a value of a possibly different type `U`:
|
|
450
|
+
*/
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* 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`).
|
|
454
|
+
* @typeParam U - The type of the value returned by the map function or the default value.
|
|
455
|
+
* @param defaultValue - The value to return if the result is `Err`.
|
|
456
|
+
* @param fn - A function that takes the `Ok` value and returns a new value.
|
|
457
|
+
*/
|
|
458
|
+
mapOr<U>(defaultValue: U, fn: (value: T) => U): U;
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Maps a `Result<T, E>` to `U` by applying a function to the contained `Ok` value (if `Ok`), or computes a default (if `Err`).
|
|
462
|
+
* @typeParam U - The type of the value returned by the map function or the default function.
|
|
463
|
+
* @param defaultFn - A function that returns the default value.
|
|
464
|
+
* @param fn - A function that takes the `Ok` value and returns a new value.
|
|
465
|
+
*/
|
|
466
|
+
mapOrElse<U>(defaultFn: (error: E) => U, fn: (value: T) => U): U;
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
|
|
470
|
+
* If the result is `Ok(Ok(T))`, returns `Ok(T)`.
|
|
471
|
+
* If the result is `Ok(Err(E))` or `Err(E)`, returns `Err(E)`.
|
|
472
|
+
*/
|
|
473
|
+
flatten<T>(this: Result<Result<T, E>, E>): Result<T, E>;
|
|
474
|
+
|
|
475
|
+
// #endregion
|
|
476
|
+
|
|
477
|
+
// #region Boolean operators
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* These methods treat the `Result` as a boolean value, where `Ok` acts like `true` and `Err` acts like `false`.
|
|
481
|
+
*/
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Returns `this` if the result is `Err`, otherwise returns the passed `Result`.
|
|
485
|
+
* @typeParam U - The type of the value in the other `Result`.
|
|
486
|
+
* @param other - The `Result` to return if `this` is `Ok`.
|
|
487
|
+
* @returns The passed `Result` if `this` is `Ok`, otherwise returns `this` (which is `Err`).
|
|
488
|
+
*/
|
|
489
|
+
and<U>(other: Result<U, E>): Result<U, E>;
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Returns `this` if it is `Ok`, otherwise returns the passed `Result`.
|
|
493
|
+
* @typeParam F - The type of the error in the other `Result`.
|
|
494
|
+
* @param other - The `Result` to return if `this` is `Err`.
|
|
495
|
+
* @returns `this` if it is `Ok`, otherwise returns `other`.
|
|
496
|
+
*/
|
|
497
|
+
or<F>(other: Result<T, F>): Result<T, F>;
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Calls the provided function with the contained value if `this` is `Ok`, otherwise returns `this` as `Err`.
|
|
501
|
+
* @typeParam U - The type of the value returned by the function.
|
|
502
|
+
* @param fn - A function that takes the `Ok` value and returns a `Result`.
|
|
503
|
+
* @returns The result of `fn` if `this` is `Ok`, otherwise `this` as `Err`.
|
|
504
|
+
*/
|
|
505
|
+
andThen<U>(fn: (value: T) => Result<U, E>): Result<U, E>;
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Calls the provided function with the contained error if `this` is `Err`, otherwise returns `this` as `Ok`.
|
|
509
|
+
* @typeParam F - The type of the error returned by the function.
|
|
510
|
+
* @param fn - A function that takes the `Err` value and returns a `Result`.
|
|
511
|
+
* @returns The result of `fn` if `this` is `Err`, otherwise `this` as `Ok`.
|
|
512
|
+
*/
|
|
513
|
+
orElse<F>(fn: (error: E) => Result<T, F>): Result<T, F>;
|
|
514
|
+
|
|
515
|
+
// #endregion
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Calls the provided function with the contained value if `this` is `Ok`, for side effects only.
|
|
519
|
+
* Does not modify the `Result`.
|
|
520
|
+
* @param fn - A function to call with the `Ok` value.
|
|
521
|
+
* @returns `this`, unmodified.
|
|
522
|
+
*/
|
|
523
|
+
inspect(fn: (value: T) => void): this;
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Calls the provided function with the contained error if `this` is `Err`, for side effects only.
|
|
527
|
+
* Does not modify the `Result`.
|
|
528
|
+
* @param fn - A function to call with the `Err` value.
|
|
529
|
+
* @returns `this`, unmodified.
|
|
530
|
+
*/
|
|
531
|
+
inspectErr(fn: (error: E) => void): this;
|
|
532
|
+
|
|
533
|
+
// #region Equals comparison
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Tests whether `this` and `other` are both `Ok` containing equal values, or both are `Err` containing equal errors.
|
|
537
|
+
* @param other - The other `Result` to compare with.
|
|
538
|
+
* @returns `true` if `this` and `other` are both `Ok` with equal values, or both are `Err` with equal errors, otherwise `false`.
|
|
539
|
+
*/
|
|
540
|
+
eq(other: Result<T, E>): boolean;
|
|
541
|
+
|
|
542
|
+
// #endregion
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Transforms the current Result into a new Result where the type of the error result is replaced with a new type `F`.
|
|
546
|
+
* The type of the success result remains unchanged.
|
|
547
|
+
* Just same as `result as unknown as Result<T, F>`.
|
|
548
|
+
*
|
|
549
|
+
* @typeParam F - The new type for the error result.
|
|
550
|
+
* @returns `this` but the error result type is `F`.
|
|
551
|
+
*/
|
|
552
|
+
asOk<F>(): Result<T, F>;
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Transforms the current Result into a new Result where the type of the success result is replaced with a new type `U`.
|
|
556
|
+
* The type of the error result remains unchanged.
|
|
557
|
+
* Useful where you need to return an Error chained to another type.
|
|
558
|
+
* Just same as `result as unknown as Result<U, E>`.
|
|
559
|
+
*
|
|
560
|
+
* @typeParam U - The new type for the success result.
|
|
561
|
+
* @returns `this` but the success result type is `U`.
|
|
562
|
+
*/
|
|
563
|
+
asErr<U>(): Result<U, E>;
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Custom `toString` implementation that uses the `Result`'s contained value.
|
|
567
|
+
*/
|
|
568
|
+
toString(): string;
|
|
569
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exports some commonly used types.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { Option, Result } from './core.ts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Represents an asynchronous operation that yields an `Option<T>`.
|
|
9
|
+
* This is a promise that resolves to either `Some(T)` if the value is present, or `None` if the value is absent.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T - The type of the value that may be contained within the `Option`.
|
|
12
|
+
*/
|
|
13
|
+
export type AsyncOption<T> = Promise<Option<T>>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Represents an asynchronous operation that yields a `Result<T, E>`.
|
|
17
|
+
* This is a promise that resolves to `Ok(T)` if the operation was successful, or `Err(E)` if there was an error.
|
|
18
|
+
*
|
|
19
|
+
* @typeParam T - The type of the value that is produced by a successful operation.
|
|
20
|
+
* @typeParam E - The type of the error that may be produced by a failed operation.
|
|
21
|
+
*/
|
|
22
|
+
export type AsyncResult<T, E> = Promise<Result<T, E>>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Represents a synchronous operation that yields a `Result<T, Error>`.
|
|
26
|
+
* This is a result that is either `Ok(T)` if the operation was successful, or `Err(Error)` if there was an error.
|
|
27
|
+
*
|
|
28
|
+
* @typeParam T - The type of the value that is produced by a successful operation.
|
|
29
|
+
*/
|
|
30
|
+
export type IOResult<T> = Result<T, Error>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Represents an asynchronous I/O operation that yields a `Result<T, Error>`.
|
|
34
|
+
* This is a promise that resolves to `Ok(T)` if the I/O operation was successful, or `Err(Error)` if there was an error.
|
|
35
|
+
*
|
|
36
|
+
* @typeParam T - The type of the value that is produced by a successful I/O operation.
|
|
37
|
+
*/
|
|
38
|
+
export type AsyncIOResult<T> = AsyncResult<T, Error>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Result } from './core.ts';
|
|
2
|
+
import { Err, Ok } from './prelude.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts a Promise to a Result type, capturing the resolved value in an `Ok`, or the error in an `Err`.
|
|
6
|
+
* This allows for promise-based asynchronous operations to be handled in a way that is more in line with the Result pattern.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam T - The type of the value that the promise resolves to.
|
|
9
|
+
* @typeParam E - The type of the error that the promise may reject with, defaults to `Error`.
|
|
10
|
+
* @param p - The promise to convert into a `Result` type.
|
|
11
|
+
* @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>`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* async function example() {
|
|
16
|
+
* const result = await promiseToAsyncResult(fetchData());
|
|
17
|
+
* if (result.isOk()) {
|
|
18
|
+
* console.log('Data:', result.unwrap());
|
|
19
|
+
* } else {
|
|
20
|
+
* console.error('Error:', result.unwrapErr());
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function promiseToAsyncResult<T, E = Error>(p: Promise<T>): Promise<Result<T, E>> {
|
|
26
|
+
return p.then((x): Result<T, E> => {
|
|
27
|
+
return Ok(x);
|
|
28
|
+
}).catch((err: E): Result<T, E> => {
|
|
29
|
+
return Err(err);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Option, Result } from './core.ts';
|
|
2
|
+
import { OptionKindSymbol, ResultKindSymbol } from './symbols.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checks if a value is an `Option`.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - The expected type of the value contained within the `Option`.
|
|
8
|
+
* @param o - The value to be checked as an `Option`.
|
|
9
|
+
* @returns `true` if the value is an `Option`, otherwise `false`.
|
|
10
|
+
*/
|
|
11
|
+
export function isOption<T>(o: unknown): o is Option<T> {
|
|
12
|
+
// `Some` and `None` must be an object.
|
|
13
|
+
return o != null && typeof o === 'object' && OptionKindSymbol in o;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Checks if a value is a `Result`.
|
|
18
|
+
*
|
|
19
|
+
* @typeParam T - The expected type of the success value contained within the `Result`.
|
|
20
|
+
* @typeParam E - The expected type of the error value contained within the `Result`.
|
|
21
|
+
* @param r - The value to be checked as a `Result`.
|
|
22
|
+
* @returns `true` if the value is a `Result`, otherwise `false`.
|
|
23
|
+
*/
|
|
24
|
+
export function isResult<T, E>(r: unknown): r is Result<T, E> {
|
|
25
|
+
// `Ok` and `Err` must be an object.
|
|
26
|
+
return r != null && typeof r === 'object' && ResultKindSymbol in r;
|
|
27
|
+
}
|