happy-rusty 1.5.0 → 1.6.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/CHANGELOG.md +206 -0
- package/README.cn.md +265 -19
- package/README.md +261 -21
- package/dist/main.cjs +382 -32
- package/dist/main.cjs.map +1 -1
- package/dist/main.mjs +374 -33
- package/dist/main.mjs.map +1 -1
- package/dist/types.d.ts +2002 -52
- package/package.json +37 -24
- package/dist/types.d.ts.map +0 -1
- package/docs/README.md +0 -47
- package/docs/functions/Err.md +0 -46
- package/docs/functions/Ok.md +0 -70
- package/docs/functions/Some.md +0 -45
- package/docs/functions/isOption.md +0 -35
- package/docs/functions/isResult.md +0 -36
- package/docs/functions/promiseToAsyncResult.md +0 -50
- package/docs/interfaces/None.md +0 -979
- package/docs/interfaces/Option.md +0 -857
- package/docs/interfaces/Result.md +0 -903
- package/docs/type-aliases/AsyncIOResult.md +0 -24
- package/docs/type-aliases/AsyncOption.md +0 -24
- package/docs/type-aliases/AsyncResult.md +0 -25
- package/docs/type-aliases/AsyncVoidIOResult.md +0 -17
- package/docs/type-aliases/AsyncVoidResult.md +0 -23
- package/docs/type-aliases/IOResult.md +0 -24
- package/docs/type-aliases/VoidIOResult.md +0 -17
- package/docs/type-aliases/VoidResult.md +0 -23
- package/docs/variables/None.md +0 -18
- package/docs/variables/RESULT_FALSE.md +0 -18
- package/docs/variables/RESULT_TRUE.md +0 -18
- package/docs/variables/RESULT_VOID.md +0 -17
- package/docs/variables/RESULT_ZERO.md +0 -18
- package/src/enum/constants.ts +0 -30
- package/src/enum/core.ts +0 -635
- package/src/enum/defines.ts +0 -45
- package/src/enum/extensions.ts +0 -31
- package/src/enum/mod.ts +0 -6
- package/src/enum/prelude.ts +0 -619
- package/src/enum/symbols.ts +0 -9
- package/src/enum/utils.ts +0 -27
- package/src/mod.ts +0 -1
package/src/enum/extensions.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
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
|
-
* result.inspect(x => {
|
|
18
|
-
* console.log('Data:', x);
|
|
19
|
-
* }).inspectErr(err => {
|
|
20
|
-
* console.error('Error:', err);
|
|
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
|
-
}
|
package/src/enum/mod.ts
DELETED
package/src/enum/prelude.ts
DELETED
|
@@ -1,619 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
import type { AsyncOption, AsyncResult, Option, Result } from './core.ts';
|
|
3
|
-
import { OptionKindSymbol, ResultKindSymbol } from './symbols.ts';
|
|
4
|
-
import { isOption, isResult } from './utils.ts';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Represents the absence of a value, as a specialized `Option` type.
|
|
8
|
-
* The type parameter is set to `never` because `None` does not hold a value.
|
|
9
|
-
*/
|
|
10
|
-
export interface None extends Option<never> {
|
|
11
|
-
/**
|
|
12
|
-
* When using `None` alone, the following overrides can make type inference more accurate.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
readonly [OptionKindSymbol]: 'None';
|
|
16
|
-
|
|
17
|
-
isSome(): false;
|
|
18
|
-
isNone(): true;
|
|
19
|
-
isSomeAnd(predicate: (value: never) => boolean): false;
|
|
20
|
-
isSomeAndAsync(predicate: (value: never) => Promise<boolean>): Promise<false>;
|
|
21
|
-
|
|
22
|
-
expect(msg: string): never;
|
|
23
|
-
unwrap(): never;
|
|
24
|
-
unwrapOr<T>(defaultValue: T): T;
|
|
25
|
-
unwrapOrElse<T>(fn: () => T): T;
|
|
26
|
-
unwrapOrElseAsync<T>(fn: () => Promise<T>): Promise<T>;
|
|
27
|
-
|
|
28
|
-
okOr<E>(error: E): Result<never, E>;
|
|
29
|
-
okOrElse<E>(err: () => E): Result<never, E>;
|
|
30
|
-
transpose(): Result<None, never>;
|
|
31
|
-
|
|
32
|
-
filter(predicate: (value: never) => boolean): None;
|
|
33
|
-
flatten(): None;
|
|
34
|
-
map<U>(fn: (value: never) => U): None;
|
|
35
|
-
mapOr<U>(defaultValue: U, fn: (value: never) => U): U;
|
|
36
|
-
mapOrElse<U>(defaultFn: () => U, fn: (value: never) => U): U;
|
|
37
|
-
|
|
38
|
-
zip<U>(other: Option<U>): None;
|
|
39
|
-
zipWith<U, R>(other: Option<U>, fn: (value: never, otherValue: U) => R): None;
|
|
40
|
-
unzip(): [None, None];
|
|
41
|
-
|
|
42
|
-
and<U>(other: Option<U>): None;
|
|
43
|
-
andThen<U>(fn: (value: never) => Option<U>): None;
|
|
44
|
-
andThenAsync<U>(fn: (value: never) => AsyncOption<U>): Promise<None>;
|
|
45
|
-
or<T>(other: Option<T>): Option<T>;
|
|
46
|
-
orElse<T>(fn: () => Option<T>): Option<T>;
|
|
47
|
-
orElseAsync<T>(fn: () => AsyncOption<T>): AsyncOption<T>;
|
|
48
|
-
xor<T>(other: Option<T>): Option<T>;
|
|
49
|
-
|
|
50
|
-
inspect(fn: (value: never) => void): this;
|
|
51
|
-
|
|
52
|
-
eq<T>(other: Option<T>): boolean;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Creates an `Option<T>` representing the presence of a value.
|
|
57
|
-
* This function is typically used to construct an `Option` that contains a value, indicating that the operation yielding the value was successful.
|
|
58
|
-
*
|
|
59
|
-
* @typeParam T - The type of the value to be wrapped in a `Some`.
|
|
60
|
-
* @param value - The value to wrap as a `Some` option.
|
|
61
|
-
* @returns An `Option<T>` that contains the provided value, representing the `Some` case.
|
|
62
|
-
*
|
|
63
|
-
* @example
|
|
64
|
-
* ```ts
|
|
65
|
-
* const maybeValue = Some(1); // Option<number> with a value
|
|
66
|
-
* if (maybeValue.isSome()) {
|
|
67
|
-
* console.log(maybeValue.unwrap()); // Outputs: 1
|
|
68
|
-
* }
|
|
69
|
-
* ```
|
|
70
|
-
*/
|
|
71
|
-
export function Some<T>(value: T): Option<T> {
|
|
72
|
-
const some: Option<T> = {
|
|
73
|
-
[Symbol.toStringTag]: 'Option',
|
|
74
|
-
[OptionKindSymbol]: 'Some',
|
|
75
|
-
|
|
76
|
-
isSome(): true {
|
|
77
|
-
return true;
|
|
78
|
-
},
|
|
79
|
-
isNone(): false {
|
|
80
|
-
return false;
|
|
81
|
-
},
|
|
82
|
-
isSomeAnd(predicate: (value: T) => boolean): boolean {
|
|
83
|
-
return predicate(value);
|
|
84
|
-
},
|
|
85
|
-
isSomeAndAsync(predicate: (value: T) => Promise<boolean>): Promise<boolean> {
|
|
86
|
-
return predicate(value);
|
|
87
|
-
},
|
|
88
|
-
|
|
89
|
-
expect(_msg: string): T {
|
|
90
|
-
return value;
|
|
91
|
-
},
|
|
92
|
-
unwrap(): T {
|
|
93
|
-
return value;
|
|
94
|
-
},
|
|
95
|
-
unwrapOr(_defaultValue: T): T {
|
|
96
|
-
return value;
|
|
97
|
-
},
|
|
98
|
-
unwrapOrElse(_fn: () => T): T {
|
|
99
|
-
return value;
|
|
100
|
-
},
|
|
101
|
-
unwrapOrElseAsync(_fn: () => Promise<T>): Promise<T> {
|
|
102
|
-
return Promise.resolve(value);
|
|
103
|
-
},
|
|
104
|
-
|
|
105
|
-
okOr<E>(_error: E): Result<T, E> {
|
|
106
|
-
return Ok(value);
|
|
107
|
-
},
|
|
108
|
-
okOrElse<E>(_err: () => E): Result<T, E> {
|
|
109
|
-
return Ok(value);
|
|
110
|
-
},
|
|
111
|
-
transpose<T, E>(): Result<Option<T>, E> {
|
|
112
|
-
const r = value as unknown as Result<T, E>;
|
|
113
|
-
assertResult(r);
|
|
114
|
-
return r.isOk() ? Ok(Some(r.unwrap())) : Err(r.unwrapErr());
|
|
115
|
-
},
|
|
116
|
-
|
|
117
|
-
filter(predicate: (value: T) => boolean): Option<T> {
|
|
118
|
-
return predicate(value) ? some : None;
|
|
119
|
-
},
|
|
120
|
-
flatten<T>(): Option<T> {
|
|
121
|
-
const o = value as unknown as Option<T>;
|
|
122
|
-
assertOption(o);
|
|
123
|
-
return o;
|
|
124
|
-
},
|
|
125
|
-
map<U>(fn: (value: T) => U): Option<U> {
|
|
126
|
-
return Some(fn(value));
|
|
127
|
-
},
|
|
128
|
-
|
|
129
|
-
mapOr<U>(_defaultValue: U, fn: (value: T) => U): U {
|
|
130
|
-
return fn(value);
|
|
131
|
-
},
|
|
132
|
-
mapOrElse<U>(_defaultFn: () => U, fn: (value: T) => U): U {
|
|
133
|
-
return fn(value);
|
|
134
|
-
},
|
|
135
|
-
|
|
136
|
-
zip<U>(other: Option<U>): Option<[T, U]> {
|
|
137
|
-
assertOption(other);
|
|
138
|
-
return other.isSome() ? Some([value, other.unwrap()]) : None;
|
|
139
|
-
},
|
|
140
|
-
zipWith<U, R>(other: Option<U>, fn: (value: T, otherValue: U) => R): Option<R> {
|
|
141
|
-
assertOption(other);
|
|
142
|
-
return other.isSome() ? Some(fn(value, other.unwrap())) : None;
|
|
143
|
-
},
|
|
144
|
-
unzip<T, U>(): [Option<T>, Option<U>] {
|
|
145
|
-
const tuple = value as unknown as [T, U];
|
|
146
|
-
|
|
147
|
-
if (!Array.isArray(tuple) || tuple.length !== 2) {
|
|
148
|
-
throw new TypeError('Unzip format is incorrect.');
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const [a, b] = tuple;
|
|
152
|
-
return [Some(a), Some(b)];
|
|
153
|
-
},
|
|
154
|
-
|
|
155
|
-
and<U>(other: Option<U>): Option<U> {
|
|
156
|
-
assertOption(other);
|
|
157
|
-
return other;
|
|
158
|
-
},
|
|
159
|
-
andThen<U>(fn: (value: T) => Option<U>): Option<U> {
|
|
160
|
-
return fn(value);
|
|
161
|
-
},
|
|
162
|
-
andThenAsync<U>(fn: (value: T) => AsyncOption<U>): AsyncOption<U> {
|
|
163
|
-
return fn(value);
|
|
164
|
-
},
|
|
165
|
-
or(_other: Option<T>): Option<T> {
|
|
166
|
-
return some;
|
|
167
|
-
},
|
|
168
|
-
orElse(_fn: () => Option<T>): Option<T> {
|
|
169
|
-
return some;
|
|
170
|
-
},
|
|
171
|
-
orElseAsync(_fn: () => AsyncOption<T>): AsyncOption<T> {
|
|
172
|
-
return Promise.resolve(some);
|
|
173
|
-
},
|
|
174
|
-
xor(other: Option<T>): Option<T> {
|
|
175
|
-
assertOption(other);
|
|
176
|
-
return other.isSome() ? None : some;
|
|
177
|
-
},
|
|
178
|
-
|
|
179
|
-
inspect(fn: (value: T) => void): Option<T> {
|
|
180
|
-
fn(value);
|
|
181
|
-
return some;
|
|
182
|
-
},
|
|
183
|
-
|
|
184
|
-
eq(other: Option<T>): boolean {
|
|
185
|
-
assertOption(other);
|
|
186
|
-
return other.isSome() && other.unwrap() === value;
|
|
187
|
-
},
|
|
188
|
-
|
|
189
|
-
toString(): string {
|
|
190
|
-
return `Some(${ value })`;
|
|
191
|
-
},
|
|
192
|
-
} as const;
|
|
193
|
-
|
|
194
|
-
return some;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* A constant representing the `None` case of an `Option`, indicating the absence of a value.
|
|
199
|
-
* This constant is frozen to ensure it is immutable and cannot be altered, preserving the integrity of `None` throughout the application.
|
|
200
|
-
*/
|
|
201
|
-
export const None = Object.freeze<None>({
|
|
202
|
-
[Symbol.toStringTag]: 'Option',
|
|
203
|
-
[OptionKindSymbol]: 'None',
|
|
204
|
-
|
|
205
|
-
isSome(): false {
|
|
206
|
-
return false;
|
|
207
|
-
},
|
|
208
|
-
isNone(): true {
|
|
209
|
-
return true;
|
|
210
|
-
},
|
|
211
|
-
isSomeAnd(_predicate: (value: never) => boolean): false {
|
|
212
|
-
return false;
|
|
213
|
-
},
|
|
214
|
-
isSomeAndAsync(_predicate: (value: never) => Promise<boolean>): Promise<false> {
|
|
215
|
-
return Promise.resolve(false);
|
|
216
|
-
},
|
|
217
|
-
|
|
218
|
-
expect(msg: string): never {
|
|
219
|
-
throw new TypeError(msg);
|
|
220
|
-
},
|
|
221
|
-
unwrap(): never {
|
|
222
|
-
throw new TypeError('Called `Option::unwrap()` on a `None` value');
|
|
223
|
-
},
|
|
224
|
-
unwrapOr<T>(defaultValue: T): T {
|
|
225
|
-
return defaultValue;
|
|
226
|
-
},
|
|
227
|
-
unwrapOrElse<T>(fn: () => T): T {
|
|
228
|
-
return fn();
|
|
229
|
-
},
|
|
230
|
-
unwrapOrElseAsync<T>(fn: () => Promise<T>): Promise<T> {
|
|
231
|
-
return fn();
|
|
232
|
-
},
|
|
233
|
-
|
|
234
|
-
okOr<E>(error: E): Result<never, E> {
|
|
235
|
-
return Err(error);
|
|
236
|
-
},
|
|
237
|
-
okOrElse<E>(err: () => E): Result<never, E> {
|
|
238
|
-
return Err(err());
|
|
239
|
-
},
|
|
240
|
-
transpose(): Result<None, never> {
|
|
241
|
-
return Ok(None);
|
|
242
|
-
},
|
|
243
|
-
|
|
244
|
-
filter(_predicate: (value: never) => boolean): None {
|
|
245
|
-
return None;
|
|
246
|
-
},
|
|
247
|
-
flatten(): None {
|
|
248
|
-
return None;
|
|
249
|
-
},
|
|
250
|
-
map<U>(_fn: (value: never) => U): None {
|
|
251
|
-
return None;
|
|
252
|
-
},
|
|
253
|
-
|
|
254
|
-
mapOr<U>(defaultValue: U, _fn: (value: never) => U): U {
|
|
255
|
-
return defaultValue;
|
|
256
|
-
},
|
|
257
|
-
mapOrElse<U>(defaultFn: () => U, _fn: (value: never) => U): U {
|
|
258
|
-
return defaultFn();
|
|
259
|
-
},
|
|
260
|
-
|
|
261
|
-
zip<U>(_other: Option<U>): None {
|
|
262
|
-
return None;
|
|
263
|
-
},
|
|
264
|
-
zipWith<U, R>(_other: Option<U>, _fn: (value: never, otherValue: U) => R): None {
|
|
265
|
-
return None;
|
|
266
|
-
},
|
|
267
|
-
unzip(): [None, None] {
|
|
268
|
-
return [None, None];
|
|
269
|
-
},
|
|
270
|
-
|
|
271
|
-
and<U>(_other: Option<U>): None {
|
|
272
|
-
return None;
|
|
273
|
-
},
|
|
274
|
-
andThen<U>(_fn: (value: never) => Option<U>): None {
|
|
275
|
-
return None;
|
|
276
|
-
},
|
|
277
|
-
andThenAsync<U>(_fn: (value: never) => AsyncOption<U>): Promise<None> {
|
|
278
|
-
return Promise.resolve(None);
|
|
279
|
-
},
|
|
280
|
-
or<T>(other: Option<T>): Option<T> {
|
|
281
|
-
assertOption(other);
|
|
282
|
-
return other;
|
|
283
|
-
},
|
|
284
|
-
orElse<T>(fn: () => Option<T>): Option<T> {
|
|
285
|
-
return fn();
|
|
286
|
-
},
|
|
287
|
-
orElseAsync<T>(fn: () => AsyncOption<T>): AsyncOption<T> {
|
|
288
|
-
return fn();
|
|
289
|
-
},
|
|
290
|
-
xor<T>(other: Option<T>): Option<T> {
|
|
291
|
-
assertOption(other);
|
|
292
|
-
return other.isSome() ? other : None;
|
|
293
|
-
},
|
|
294
|
-
|
|
295
|
-
inspect(_fn: (value: never) => void): None {
|
|
296
|
-
return None;
|
|
297
|
-
},
|
|
298
|
-
|
|
299
|
-
eq<T>(other: Option<T>): boolean {
|
|
300
|
-
assertOption(other);
|
|
301
|
-
return other === None;
|
|
302
|
-
},
|
|
303
|
-
|
|
304
|
-
toString(): string {
|
|
305
|
-
return 'None';
|
|
306
|
-
},
|
|
307
|
-
}) as None;
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* Creates a `Result<T, E>` representing a successful outcome containing a value.
|
|
311
|
-
* This function is used to construct a `Result` that signifies the operation was successful by containing the value `T`.
|
|
312
|
-
*
|
|
313
|
-
* @typeParam T - The type of the value to be contained in the `Ok` result.
|
|
314
|
-
* @typeParam E - The type of the error that the result could potentially contain (not used in this case).
|
|
315
|
-
* @param value - The value to wrap as an `Ok` result.
|
|
316
|
-
* @returns A `Result<T, E>` that contains the provided value, representing the `Ok` case.
|
|
317
|
-
*
|
|
318
|
-
* @example
|
|
319
|
-
* ```ts
|
|
320
|
-
* const goodResult = Ok<number, Error>(1); // Result<number, Error> with a value
|
|
321
|
-
* if (goodResult.isOk()) {
|
|
322
|
-
* console.log(goodResult.unwrap()); // Outputs: 1
|
|
323
|
-
* }
|
|
324
|
-
* ```
|
|
325
|
-
*/
|
|
326
|
-
export function Ok<T, E>(value: T): Result<T, E>;
|
|
327
|
-
/**
|
|
328
|
-
* Because javascript does not have a `()` type, use `void` instead.
|
|
329
|
-
*/
|
|
330
|
-
export function Ok<E>(): Result<void, E>;
|
|
331
|
-
export function Ok<T, E>(value?: T): Result<T, E> {
|
|
332
|
-
const ok: Result<T, E> = {
|
|
333
|
-
[Symbol.toStringTag]: 'Result',
|
|
334
|
-
[ResultKindSymbol]: 'Ok',
|
|
335
|
-
|
|
336
|
-
isOk(): true {
|
|
337
|
-
return true;
|
|
338
|
-
},
|
|
339
|
-
isErr(): false {
|
|
340
|
-
return false;
|
|
341
|
-
},
|
|
342
|
-
isOkAnd(predicate: (value: T) => boolean): boolean {
|
|
343
|
-
return predicate(value as T);
|
|
344
|
-
},
|
|
345
|
-
isOkAndAsync(predicate: (value: T) => Promise<boolean>): Promise<boolean> {
|
|
346
|
-
return predicate(value as T);
|
|
347
|
-
},
|
|
348
|
-
isErrAnd(_predicate: (error: E) => boolean): false {
|
|
349
|
-
return false;
|
|
350
|
-
},
|
|
351
|
-
isErrAndAsync(_predicate: (error: E) => Promise<boolean>): Promise<false> {
|
|
352
|
-
return Promise.resolve(false);
|
|
353
|
-
},
|
|
354
|
-
|
|
355
|
-
expect(_msg: string): T {
|
|
356
|
-
return value as T;
|
|
357
|
-
},
|
|
358
|
-
unwrap(): T {
|
|
359
|
-
return value as T;
|
|
360
|
-
},
|
|
361
|
-
unwrapOr(_defaultValue: T): T {
|
|
362
|
-
return value as T;
|
|
363
|
-
},
|
|
364
|
-
unwrapOrElse(_fn: (error: E) => T): T {
|
|
365
|
-
return value as T;
|
|
366
|
-
},
|
|
367
|
-
unwrapOrElseAsync(_fn: (error: E) => Promise<T>): Promise<T> {
|
|
368
|
-
return Promise.resolve(value as T);
|
|
369
|
-
},
|
|
370
|
-
|
|
371
|
-
expectErr(msg: string): E {
|
|
372
|
-
throw new TypeError(`${ msg }: ${ value }`);
|
|
373
|
-
},
|
|
374
|
-
unwrapErr(): E {
|
|
375
|
-
throw new TypeError('Called `Result::unwrapErr()` on an `Ok` value');
|
|
376
|
-
},
|
|
377
|
-
|
|
378
|
-
ok(): Option<T> {
|
|
379
|
-
return Some(value as T);
|
|
380
|
-
},
|
|
381
|
-
err(): None {
|
|
382
|
-
return None;
|
|
383
|
-
},
|
|
384
|
-
transpose<T>(): Option<Result<T, E>> {
|
|
385
|
-
const o = value as Option<T>;
|
|
386
|
-
assertOption(o);
|
|
387
|
-
return o.isSome() ? Some(Ok(o.unwrap())) : None;
|
|
388
|
-
},
|
|
389
|
-
|
|
390
|
-
map<U>(fn: (value: T) => U): Result<U, E> {
|
|
391
|
-
return Ok(fn(value as T));
|
|
392
|
-
},
|
|
393
|
-
mapErr<F>(_fn: (error: E) => F): Result<T, F> {
|
|
394
|
-
return Ok(value as T);
|
|
395
|
-
},
|
|
396
|
-
mapOr<U>(_defaultValue: U, fn: (value: T) => U): U {
|
|
397
|
-
return fn(value as T);
|
|
398
|
-
},
|
|
399
|
-
mapOrElse<U>(_defaultFn: (error: E) => U, fn: (value: T) => U): U {
|
|
400
|
-
return fn(value as T);
|
|
401
|
-
},
|
|
402
|
-
flatten<T>(): Result<T, E> {
|
|
403
|
-
const r = value as Result<T, E>;
|
|
404
|
-
assertResult(r);
|
|
405
|
-
return r;
|
|
406
|
-
},
|
|
407
|
-
|
|
408
|
-
and<U>(other: Result<U, E>): Result<U, E> {
|
|
409
|
-
assertResult(other);
|
|
410
|
-
return other;
|
|
411
|
-
},
|
|
412
|
-
or<F>(_other: Result<T, F>): Result<T, F> {
|
|
413
|
-
return ok as unknown as Result<T, F>;
|
|
414
|
-
},
|
|
415
|
-
andThen<U>(fn: (value: T) => Result<U, E>): Result<U, E> {
|
|
416
|
-
return fn(value as T);
|
|
417
|
-
},
|
|
418
|
-
andThenAsync<U>(fn: (value: T) => AsyncResult<U, E>): AsyncResult<U, E> {
|
|
419
|
-
return fn(value as T);
|
|
420
|
-
},
|
|
421
|
-
orElse<F>(_fn: (error: E) => Result<T, F>): Result<T, F> {
|
|
422
|
-
return ok as unknown as Result<T, F>;
|
|
423
|
-
},
|
|
424
|
-
orElseAsync<F>(_fn: (error: E) => AsyncResult<T, F>): AsyncResult<T, F> {
|
|
425
|
-
return Promise.resolve(ok as unknown as Result<T, F>);
|
|
426
|
-
},
|
|
427
|
-
|
|
428
|
-
inspect(fn: (value: T) => void): Result<T, E> {
|
|
429
|
-
fn(value as T);
|
|
430
|
-
return ok;
|
|
431
|
-
},
|
|
432
|
-
inspectErr(_fn: (error: E) => void): Result<T, E> {
|
|
433
|
-
return ok;
|
|
434
|
-
},
|
|
435
|
-
|
|
436
|
-
eq(other: Result<T, E>): boolean {
|
|
437
|
-
assertResult(other);
|
|
438
|
-
return other.isOk() && other.unwrap() === value;
|
|
439
|
-
},
|
|
440
|
-
|
|
441
|
-
asOk<F>(): Result<T, F> {
|
|
442
|
-
return ok as unknown as Result<T, F>;
|
|
443
|
-
},
|
|
444
|
-
asErr(): never {
|
|
445
|
-
throw new TypeError('Called `Result::asErr()` on an `Ok` value');
|
|
446
|
-
},
|
|
447
|
-
|
|
448
|
-
toString(): string {
|
|
449
|
-
return `Ok(${ value })`;
|
|
450
|
-
},
|
|
451
|
-
} as const;
|
|
452
|
-
|
|
453
|
-
return ok;
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
/**
|
|
457
|
-
* Creates a `Result<T, E>` representing a failed outcome containing an error.
|
|
458
|
-
* This function is used to construct a `Result` that signifies the operation failed by containing the error `E`.
|
|
459
|
-
*
|
|
460
|
-
* @typeParam T - The type of the value that the result could potentially contain (not used in this case).
|
|
461
|
-
* @typeParam E - The type of the error to be wrapped in the `Err` result.
|
|
462
|
-
* @param error - The error to wrap as an `Err` result.
|
|
463
|
-
* @returns A `Result<T, E>` that contains the provided error, representing the `Err` case.
|
|
464
|
-
*
|
|
465
|
-
* @example
|
|
466
|
-
* ```ts
|
|
467
|
-
* const badResult = Err<number, Error>(new Error('Something went wrong'));
|
|
468
|
-
* if (badResult.isErr()) {
|
|
469
|
-
* console.error(badResult.unwrapErr()); // Outputs: Error: Something went wrong
|
|
470
|
-
* }
|
|
471
|
-
* ```
|
|
472
|
-
*/
|
|
473
|
-
export function Err<T, E>(error: E): Result<T, E> {
|
|
474
|
-
const err: Result<T, E> = {
|
|
475
|
-
[Symbol.toStringTag]: 'Result',
|
|
476
|
-
[ResultKindSymbol]: 'Err',
|
|
477
|
-
|
|
478
|
-
isOk(): false {
|
|
479
|
-
return false;
|
|
480
|
-
},
|
|
481
|
-
isErr(): true {
|
|
482
|
-
return true;
|
|
483
|
-
},
|
|
484
|
-
isOkAnd(_predicate: (value: T) => boolean): false {
|
|
485
|
-
return false;
|
|
486
|
-
},
|
|
487
|
-
isOkAndAsync(_predicate: (value: T) => Promise<boolean>): Promise<boolean> {
|
|
488
|
-
return Promise.resolve(false);
|
|
489
|
-
},
|
|
490
|
-
isErrAnd(predicate: (error: E) => boolean): boolean {
|
|
491
|
-
return predicate(error);
|
|
492
|
-
},
|
|
493
|
-
isErrAndAsync(predicate: (error: E) => Promise<boolean>): Promise<boolean> {
|
|
494
|
-
return predicate(error);
|
|
495
|
-
},
|
|
496
|
-
|
|
497
|
-
expect(msg: string): T {
|
|
498
|
-
throw new TypeError(`${ msg }: ${ error }`);
|
|
499
|
-
},
|
|
500
|
-
unwrap(): T {
|
|
501
|
-
throw new TypeError('Called `Result::unwrap()` on an `Err` value');
|
|
502
|
-
},
|
|
503
|
-
unwrapOr(defaultValue: T): T {
|
|
504
|
-
return defaultValue;
|
|
505
|
-
},
|
|
506
|
-
unwrapOrElse(fn: (error: E) => T): T {
|
|
507
|
-
return fn(error);
|
|
508
|
-
},
|
|
509
|
-
unwrapOrElseAsync(fn: (error: E) => Promise<T>): Promise<T> {
|
|
510
|
-
return fn(error);
|
|
511
|
-
},
|
|
512
|
-
|
|
513
|
-
expectErr(_msg: string): E {
|
|
514
|
-
return error;
|
|
515
|
-
},
|
|
516
|
-
unwrapErr(): E {
|
|
517
|
-
return error;
|
|
518
|
-
},
|
|
519
|
-
|
|
520
|
-
ok(): None {
|
|
521
|
-
return None;
|
|
522
|
-
},
|
|
523
|
-
err(): Option<E> {
|
|
524
|
-
return Some(error);
|
|
525
|
-
},
|
|
526
|
-
transpose<T>(): Option<Result<T, E>> {
|
|
527
|
-
return Some(err as unknown as Result<T, E>);
|
|
528
|
-
},
|
|
529
|
-
|
|
530
|
-
map<U>(_fn: (value: T) => U): Result<U, E> {
|
|
531
|
-
return Err(error);
|
|
532
|
-
},
|
|
533
|
-
mapErr<F>(fn: (error: E) => F): Result<T, F> {
|
|
534
|
-
return Err(fn(error));
|
|
535
|
-
},
|
|
536
|
-
mapOr<U>(defaultValue: U, _fn: (value: T) => U): U {
|
|
537
|
-
return defaultValue;
|
|
538
|
-
},
|
|
539
|
-
mapOrElse<U>(defaultFn: (error: E) => U, _fn: (value: T) => U): U {
|
|
540
|
-
return defaultFn(error);
|
|
541
|
-
},
|
|
542
|
-
flatten<T>(): Result<T, E> {
|
|
543
|
-
return err as unknown as Result<T, E>;
|
|
544
|
-
},
|
|
545
|
-
|
|
546
|
-
and<U>(_other: Result<U, E>): Result<U, E> {
|
|
547
|
-
return err as unknown as Result<U, E>;
|
|
548
|
-
},
|
|
549
|
-
or<F>(other: Result<T, F>): Result<T, F> {
|
|
550
|
-
assertResult(other);
|
|
551
|
-
return other;
|
|
552
|
-
},
|
|
553
|
-
andThen<U>(_fn: (value: T) => Result<U, E>): Result<U, E> {
|
|
554
|
-
return err as unknown as Result<U, E>;
|
|
555
|
-
},
|
|
556
|
-
andThenAsync<U>(_fn: (value: T) => AsyncResult<U, E>): AsyncResult<U, E> {
|
|
557
|
-
return Promise.resolve(err as unknown as Result<U, E>);
|
|
558
|
-
},
|
|
559
|
-
orElse<F>(fn: (error: E) => Result<T, F>): Result<T, F> {
|
|
560
|
-
return fn(error);
|
|
561
|
-
},
|
|
562
|
-
orElseAsync<F>(fn: (error: E) => AsyncResult<T, F>): AsyncResult<T, F> {
|
|
563
|
-
return fn(error);
|
|
564
|
-
},
|
|
565
|
-
|
|
566
|
-
inspect(_fn: (value: T) => void): Result<T, E> {
|
|
567
|
-
return err;
|
|
568
|
-
},
|
|
569
|
-
inspectErr(fn: (error: E) => void): Result<T, E> {
|
|
570
|
-
fn(error);
|
|
571
|
-
return err;
|
|
572
|
-
},
|
|
573
|
-
|
|
574
|
-
eq(other: Result<T, E>): boolean {
|
|
575
|
-
assertResult(other);
|
|
576
|
-
return other.isErr() && other.unwrapErr() === error;
|
|
577
|
-
},
|
|
578
|
-
|
|
579
|
-
asOk(): never {
|
|
580
|
-
throw new TypeError('Called `Result::asOk()` on an `Err` value');
|
|
581
|
-
},
|
|
582
|
-
asErr<U>(): Result<U, E> {
|
|
583
|
-
return err as unknown as Result<U, E>;
|
|
584
|
-
},
|
|
585
|
-
|
|
586
|
-
toString(): string {
|
|
587
|
-
return `Err(${ error })`;
|
|
588
|
-
},
|
|
589
|
-
} as const;
|
|
590
|
-
|
|
591
|
-
return err;
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
/**
|
|
595
|
-
* Asserts that a given value is an `Option`.
|
|
596
|
-
*
|
|
597
|
-
* @typeParam T - The expected type of the value contained within the `Option`.
|
|
598
|
-
* @param o - The value to be checked as an `Option`.
|
|
599
|
-
* @throws {TypeError} If the value is not an `Option`.
|
|
600
|
-
*/
|
|
601
|
-
function assertOption<T>(o: Option<T>): void {
|
|
602
|
-
if (!isOption(o)) {
|
|
603
|
-
throw new TypeError(`This(${ o }) is not an Option`);
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
/**
|
|
608
|
-
* Asserts that a given value is a `Result`.
|
|
609
|
-
*
|
|
610
|
-
* @typeParam T - The expected type of the success value contained within the `Result`.
|
|
611
|
-
* @typeParam E - The expected type of the error value contained within the `Result`.
|
|
612
|
-
* @param r - The value to be checked as a `Result`.
|
|
613
|
-
* @throws {TypeError} If the value is not a `Result`.
|
|
614
|
-
*/
|
|
615
|
-
function assertResult<T, E>(r: Result<T, E>): void {
|
|
616
|
-
if (!isResult(r)) {
|
|
617
|
-
throw new TypeError(`This(${ r }) is not a Result`);
|
|
618
|
-
}
|
|
619
|
-
}
|
package/src/enum/symbols.ts
DELETED
package/src/enum/utils.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
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
|
-
}
|
package/src/mod.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './enum/mod.ts';
|