rsult 1.2.0 → 1.4.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/lib.d.ts +2 -0
- package/dist/lib.js +19 -0
- package/dist/lib.js.map +1 -0
- package/dist/option.d.ts +307 -0
- package/dist/option.js +195 -0
- package/dist/option.js.map +1 -0
- package/dist/result.d.ts +410 -0
- package/dist/result.js +231 -0
- package/dist/result.js.map +1 -0
- package/package.json +18 -17
- package/src/option.ts +4 -4
- package/tsconfig.json +0 -1
package/dist/lib.d.ts
ADDED
package/dist/lib.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./result"), exports);
|
|
18
|
+
__exportStar(require("./option"), exports);
|
|
19
|
+
//# sourceMappingURL=lib.js.map
|
package/dist/lib.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lib.js","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,2CAAyB"}
|
package/dist/option.d.ts
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
export type Option<T> = OptionSome<T> | OptionNone<T>;
|
|
2
|
+
export interface IOptionCheck<T> {
|
|
3
|
+
/**
|
|
4
|
+
* Determines if the Option is Some.
|
|
5
|
+
* @returns true if the Option is Some, otherwise false.
|
|
6
|
+
*
|
|
7
|
+
* Usage Example:
|
|
8
|
+
* const myOption = Some(5);
|
|
9
|
+
* if (myOption.is_some()) {
|
|
10
|
+
* console.log("It's Some!");
|
|
11
|
+
* }
|
|
12
|
+
*/
|
|
13
|
+
is_some(): this is OptionSome<T>;
|
|
14
|
+
/**
|
|
15
|
+
* Determines if the Option is Some and the contained value meets a condition.
|
|
16
|
+
* @param f The condition to apply to the contained value if it's Some.
|
|
17
|
+
* @returns true if the Option is Some and the condition returns true, otherwise false.
|
|
18
|
+
*
|
|
19
|
+
* Usage Example:
|
|
20
|
+
* const myOption = Some(5);
|
|
21
|
+
* const isGreaterThanThree = myOption.is_some_and(x => x > 3); // true
|
|
22
|
+
*/
|
|
23
|
+
is_some_and(f: (arg: T) => boolean): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Determines if the Option is None.
|
|
26
|
+
* @returns true if the Option is None, otherwise false.
|
|
27
|
+
*
|
|
28
|
+
* Usage Example:
|
|
29
|
+
* const myOption = None();
|
|
30
|
+
* if (myOption.is_none()) {
|
|
31
|
+
* console.log("It's None!");
|
|
32
|
+
* }
|
|
33
|
+
*/
|
|
34
|
+
is_none(): boolean;
|
|
35
|
+
}
|
|
36
|
+
export interface IOptionExpect<T> {
|
|
37
|
+
/**
|
|
38
|
+
* Extracts the value from a Some, throwing an error if it is None.
|
|
39
|
+
* @param msg The error message to throw if the Option is None.
|
|
40
|
+
* @returns The contained value if the Option is Some.
|
|
41
|
+
* @throws Error with provided message if the Option is None.
|
|
42
|
+
*
|
|
43
|
+
* Usage Example:
|
|
44
|
+
* const myOption = Some(5);
|
|
45
|
+
* const value = myOption.expect("Expected a value!"); // 5
|
|
46
|
+
*/
|
|
47
|
+
expect(msg: string): T | never;
|
|
48
|
+
}
|
|
49
|
+
export interface IOptionTransform<T> {
|
|
50
|
+
/**
|
|
51
|
+
* Transforms the contained value of a Some with a provided function. Returns None if this Option is None.
|
|
52
|
+
* @param fn The mapping function to apply to the contained value.
|
|
53
|
+
* @returns An Option containing the result of applying fn to the original value if it was Some, else None.
|
|
54
|
+
*
|
|
55
|
+
* Usage Example:
|
|
56
|
+
* const myOption = Some(5);
|
|
57
|
+
* const newOption = myOption.map(x => x * 2); // Some(10)
|
|
58
|
+
*/
|
|
59
|
+
map<U>(fn: (arg: T) => U): Option<U>;
|
|
60
|
+
/**
|
|
61
|
+
* Applies a function to the contained value if Some, otherwise returns a provided default.
|
|
62
|
+
* @param defaultVal The default value to return if the Option is None.
|
|
63
|
+
* @param fn The function to apply to the contained value if Some.
|
|
64
|
+
* @returns The result of applying fn to the contained value if this Option is Some, else defaultVal.
|
|
65
|
+
*
|
|
66
|
+
* Usage Example:
|
|
67
|
+
* const myOption = None();
|
|
68
|
+
* const value = myOption.map_or(0, x => x * 2); // 0
|
|
69
|
+
*/
|
|
70
|
+
map_or<U>(defaultVal: U, fn: (arg: T) => U): U;
|
|
71
|
+
}
|
|
72
|
+
export interface IOptionCombine<T> {
|
|
73
|
+
/**
|
|
74
|
+
* Returns the passed Option if this Option is Some, else returns None.
|
|
75
|
+
* @param opt The Option to return if this Option is Some.
|
|
76
|
+
* @returns The passed Option if this Option is Some, else None.
|
|
77
|
+
*
|
|
78
|
+
* Usage Example:
|
|
79
|
+
* const opt1 = Some(5);
|
|
80
|
+
* const opt2 = Some(10);
|
|
81
|
+
* const result = opt1.and(opt2); // Some(10)
|
|
82
|
+
*/
|
|
83
|
+
and<U>(opt: Option<U>): Option<U>;
|
|
84
|
+
/**
|
|
85
|
+
* Returns the result of applying a function to the contained value if Some, otherwise returns None.
|
|
86
|
+
* @param fn The function to apply to the contained value.
|
|
87
|
+
* @returns An Option containing the result of applying fn to the original value if it was Some, else None.
|
|
88
|
+
*
|
|
89
|
+
* Usage Example:
|
|
90
|
+
* const myOption = Some(5);
|
|
91
|
+
* const newOption = myOption.and_then(x => Some(x * 2)); // Some(10)
|
|
92
|
+
*/
|
|
93
|
+
and_then<U>(fn: (arg: T) => Option<U>): Option<U>;
|
|
94
|
+
/**
|
|
95
|
+
* Returns the passed Option if this Option is None, else returns this Option.
|
|
96
|
+
* @param opt The alternative Option to return if this Option is None.
|
|
97
|
+
* @returns This Option if it is Some, otherwise the passed Option.
|
|
98
|
+
*
|
|
99
|
+
* Usage Example:
|
|
100
|
+
* const opt1 = None();
|
|
101
|
+
* const opt2 = Some(10);
|
|
102
|
+
* const result = opt1.or(opt2); // Some(10)
|
|
103
|
+
*/
|
|
104
|
+
or<U>(opt: Option<U>): Option<T> | Option<U>;
|
|
105
|
+
/**
|
|
106
|
+
* Returns the result of applying a function if this Option is None, else returns this Option.
|
|
107
|
+
* @param fn The function that produces an Option to return if this Option is None.
|
|
108
|
+
* @returns This Option if it is Some, otherwise the Option produced by fn.
|
|
109
|
+
*
|
|
110
|
+
* Usage Example:
|
|
111
|
+
* const opt1 = None();
|
|
112
|
+
* const result = opt1.or_else(() => Some(10)); // Some(10)
|
|
113
|
+
*/
|
|
114
|
+
or_else<U>(fn: () => Option<U>): Option<T> | Option<U>;
|
|
115
|
+
/**
|
|
116
|
+
* Returns None if both this and the passed Option are Some. Otherwise returns the Option that is Some.
|
|
117
|
+
* @param optb The other Option to compare with.
|
|
118
|
+
* @returns None if both Options are Some, otherwise the Option that is Some.
|
|
119
|
+
*
|
|
120
|
+
* Usage Example:
|
|
121
|
+
* const opt1 = Some(5);
|
|
122
|
+
* const opt2 = None();
|
|
123
|
+
* const result = opt1.xor(opt2); // Some(5)
|
|
124
|
+
*/
|
|
125
|
+
xor(optb: Option<T>): Option<T>;
|
|
126
|
+
}
|
|
127
|
+
export interface IOptionUtility<T> {
|
|
128
|
+
/**
|
|
129
|
+
* Unwraps the Option, returning the contained value, or throws an error if the Option is None.
|
|
130
|
+
* @returns The contained value if this Option is Some.
|
|
131
|
+
* @throws Error if this Option is None.
|
|
132
|
+
*
|
|
133
|
+
* Usage Example:
|
|
134
|
+
* const myOption = Some(5);
|
|
135
|
+
* const value = myOption.unwrap(); // 5
|
|
136
|
+
*/
|
|
137
|
+
unwrap(): T | never;
|
|
138
|
+
/**
|
|
139
|
+
* Returns the contained value if Some, else returns a provided alternative.
|
|
140
|
+
* @param optb The alternative value to return if this Option is None.
|
|
141
|
+
* @returns The contained value if this Option is Some, else optb.
|
|
142
|
+
*
|
|
143
|
+
* Usage Example:
|
|
144
|
+
* const myOption = None();
|
|
145
|
+
* const value = myOption.unwrap_or(10); // 10
|
|
146
|
+
*/
|
|
147
|
+
unwrap_or(optb: T): T;
|
|
148
|
+
/**
|
|
149
|
+
* Returns the contained value if Some, else computes a value from a provided function.
|
|
150
|
+
* @param fn The function to compute the alternative value from if this Option is None.
|
|
151
|
+
* @returns The contained value if this Option is Some, else the result of fn.
|
|
152
|
+
*
|
|
153
|
+
* Usage Example:
|
|
154
|
+
* const myOption = None();
|
|
155
|
+
* const value = myOption.unwrap_or_else(() => 10); // 10
|
|
156
|
+
*/
|
|
157
|
+
unwrap_or_else(fn: () => T): T;
|
|
158
|
+
/**
|
|
159
|
+
* Returns the contained value if Some, otherwise the default value for the type.
|
|
160
|
+
* @returns The contained value if Some, else the type’s default value.
|
|
161
|
+
*
|
|
162
|
+
* Usage Example:
|
|
163
|
+
* const myOption is None<number>();
|
|
164
|
+
* const value = myOption.unwrap_or_default(); // 0
|
|
165
|
+
*/
|
|
166
|
+
unwrap_or_default(): T | null;
|
|
167
|
+
}
|
|
168
|
+
export interface IOptionMutate<T> {
|
|
169
|
+
/**
|
|
170
|
+
* Takes the contained value out of the Option, leaving a None in its place.
|
|
171
|
+
* @returns The Option containing the original value before it was taken.
|
|
172
|
+
*
|
|
173
|
+
* Usage Example:
|
|
174
|
+
* const myOption = Some(5);
|
|
175
|
+
* const takenValue = myOption.take(); // Some(5), myOption is now None
|
|
176
|
+
*/
|
|
177
|
+
take(): Option<T>;
|
|
178
|
+
/**
|
|
179
|
+
* Takes the contained value out of the Option if it satisfies a predicate, leaving a None in its place.
|
|
180
|
+
* @param predicate The predicate to apply to the contained value.
|
|
181
|
+
* @returns The Option containing the original value if the predicate returns true, otherwise None.
|
|
182
|
+
*
|
|
183
|
+
* Usage Example:
|
|
184
|
+
* const myOption = Some(5);
|
|
185
|
+
* const takenValue = myOption.take_if(x => x > 3); // Some(5), myOption is now None
|
|
186
|
+
*/
|
|
187
|
+
take_if(predicate: (arg: T) => boolean): Option<T>;
|
|
188
|
+
/**
|
|
189
|
+
* Replaces the contained value with another, returning the old value wrapped in an Option.
|
|
190
|
+
* @param value The new value to put in the Option.
|
|
191
|
+
* @returns An Option containing the old value.
|
|
192
|
+
*
|
|
193
|
+
* Usage Example:
|
|
194
|
+
* const myOption = Some(5);
|
|
195
|
+
* const oldValue = myOption.replace(10); // Some(5), myOption now contains 10
|
|
196
|
+
*
|
|
197
|
+
*/
|
|
198
|
+
replace(value: T): Option<T>;
|
|
199
|
+
}
|
|
200
|
+
export interface IOptionZip<T> {
|
|
201
|
+
/**
|
|
202
|
+
* Combines two Option values into a single Option containing a tuple of their values if both are Some, otherwise returns None.
|
|
203
|
+
* @param other The other Option to zip with.
|
|
204
|
+
* @returns An Option containing a tuple of the two Option values if both are Some, otherwise None.
|
|
205
|
+
*
|
|
206
|
+
* Usage Example:
|
|
207
|
+
* const opt1 = Some(5);
|
|
208
|
+
* const opt2 = Some("hello");
|
|
209
|
+
* const zipped = opt1.zip(opt2); // Some([5, "hello"])
|
|
210
|
+
*/
|
|
211
|
+
zip<U>(other: Option<U>): Option<[T, U]>;
|
|
212
|
+
/**
|
|
213
|
+
* Combines two Option values by applying a function if both are Some, otherwise returns None.
|
|
214
|
+
* @param other The other Option to zip with.
|
|
215
|
+
* @param f The function to apply to the values if both Options are Some.
|
|
216
|
+
* @returns An Option containing the result of the function if both Options are Some, otherwise None.
|
|
217
|
+
*
|
|
218
|
+
* Usage Example:
|
|
219
|
+
* const opt1 = Some(5);
|
|
220
|
+
* const opt2 = Some(10);
|
|
221
|
+
* const added = opt1.zip_with(opt2, (a, b) => a + b); // Some(15)
|
|
222
|
+
*/
|
|
223
|
+
zip_with<U, R>(other: Option<U>, f: (val: T, other: U) => R): Option<R>;
|
|
224
|
+
}
|
|
225
|
+
export interface IOptionFilter<T> {
|
|
226
|
+
/**
|
|
227
|
+
* Applies a predicate to the contained value if Some, returns None if the predicate does not hold or if this Option is None.
|
|
228
|
+
* @param predicate The predicate function to apply.
|
|
229
|
+
* @returns The original Option if it is Some and the predicate holds, else None.
|
|
230
|
+
*
|
|
231
|
+
* Usage Example:
|
|
232
|
+
* const myOption = Some(5);
|
|
233
|
+
* const filteredOption = myOption.filter(x => x > 3); // Some(5)
|
|
234
|
+
*/
|
|
235
|
+
filter(predicate: (arg: T) => boolean): Option<T>;
|
|
236
|
+
}
|
|
237
|
+
export interface IOptionFlatten<T> {
|
|
238
|
+
/**
|
|
239
|
+
* Flattens a nested Option, if the Option contains another Option, returning the inner Option if it's Some.
|
|
240
|
+
* @returns The contained Option if this is an Option of an Option and the inner Option is Some, otherwise None.
|
|
241
|
+
*
|
|
242
|
+
* Usage Example:
|
|
243
|
+
* const opt = Some(Some(5));
|
|
244
|
+
* const flattened = opt.flatten(); // Some(5)
|
|
245
|
+
*/
|
|
246
|
+
flatten<T extends Option<T>>(): Option<T>;
|
|
247
|
+
}
|
|
248
|
+
export interface IOption<T> extends IOptionCheck<T>, IOptionExpect<T>, IOptionTransform<T>, IOptionCombine<T>, IOptionUtility<T>, IOptionMutate<T>, IOptionZip<T>, IOptionFilter<T>, IOptionFlatten<T> {
|
|
249
|
+
}
|
|
250
|
+
export declare class OptionSome<T> implements IOption<T> {
|
|
251
|
+
readonly value: T;
|
|
252
|
+
readonly _tag: "Some";
|
|
253
|
+
readonly _T: T;
|
|
254
|
+
constructor(value: T);
|
|
255
|
+
is_some(): this is OptionSome<T>;
|
|
256
|
+
is_some_and(f: (arg: T) => boolean): boolean;
|
|
257
|
+
is_none(): this is never;
|
|
258
|
+
expect(_msg: string): T;
|
|
259
|
+
map<U>(fn: (arg: T) => U): Option<U>;
|
|
260
|
+
map_or<U>(_defaultVal: U, fn: (arg: T) => U): U;
|
|
261
|
+
and<U>(opt: Option<U>): Option<U>;
|
|
262
|
+
and_then<U>(fn: (arg: T) => Option<U>): Option<U>;
|
|
263
|
+
filter(predicate: (arg: T) => boolean): Option<T>;
|
|
264
|
+
or<U>(_opt: Option<U>): Option<T>;
|
|
265
|
+
or_else<U>(_fn: () => Option<U>): Option<T>;
|
|
266
|
+
xor(optb: Option<T>): Option<T>;
|
|
267
|
+
unwrap(): T;
|
|
268
|
+
unwrap_or(optb: T): T;
|
|
269
|
+
unwrap_or_else(_fn: () => T): T;
|
|
270
|
+
unwrap_or_default(): T | null;
|
|
271
|
+
take(): Option<T>;
|
|
272
|
+
take_if(predicate: (arg: T) => boolean): Option<T>;
|
|
273
|
+
replace(value: T): Option<T>;
|
|
274
|
+
zip<U>(other: Option<U>): Option<[T, U]>;
|
|
275
|
+
zip_with<U, R>(other: Option<U>, f: (val: T, other: U) => R): Option<R>;
|
|
276
|
+
flatten<T extends Option<T>>(): Option<T>;
|
|
277
|
+
}
|
|
278
|
+
export declare class OptionNone<T> implements IOption<T> {
|
|
279
|
+
private readonly _tag;
|
|
280
|
+
private readonly _T;
|
|
281
|
+
is_some(): this is never;
|
|
282
|
+
is_some_and(_f: (arg: T) => boolean): boolean;
|
|
283
|
+
is_none(): this is OptionNone<T>;
|
|
284
|
+
expect(msg: string): never;
|
|
285
|
+
map<U>(_fn: (arg: T) => U): Option<U>;
|
|
286
|
+
map_or<U>(defaultVal: U, _fn: (arg: T) => U): U;
|
|
287
|
+
and<U>(_opt: Option<U>): Option<U>;
|
|
288
|
+
and_then<U>(_fn: (arg: T) => Option<U>): Option<U>;
|
|
289
|
+
filter(_predicate: (arg: T) => boolean): Option<T>;
|
|
290
|
+
or<U>(opt: Option<U>): Option<U>;
|
|
291
|
+
or_else<U>(fn: () => Option<U>): Option<U>;
|
|
292
|
+
xor(optb: Option<T>): Option<T>;
|
|
293
|
+
unwrap(): never;
|
|
294
|
+
unwrap_or(optb: T): T;
|
|
295
|
+
unwrap_or_else(fn: () => T): T;
|
|
296
|
+
unwrap_or_default(): T | null;
|
|
297
|
+
take(): Option<T>;
|
|
298
|
+
take_if(_predicate: (arg: T) => boolean): Option<T>;
|
|
299
|
+
replace(value: T): Option<T>;
|
|
300
|
+
zip<U>(other: Option<U>): Option<[T, U]>;
|
|
301
|
+
zip_with<U, R>(_other: Option<U>, _f: (val: T, other: U) => R): Option<R>;
|
|
302
|
+
flatten<T extends Option<T>>(): Option<T>;
|
|
303
|
+
}
|
|
304
|
+
export declare const Some: <T>(val: T) => Option<T>;
|
|
305
|
+
export declare const None: <T>() => Option<T>;
|
|
306
|
+
export declare const option_from_nullable: <T>(val: T | null | undefined) => Option<T>;
|
|
307
|
+
export declare const option_from_promise: <T>(promise: Promise<T>) => Promise<Option<T>>;
|
package/dist/option.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.option_from_promise = exports.option_from_nullable = exports.None = exports.Some = exports.OptionNone = exports.OptionSome = void 0;
|
|
4
|
+
class OptionSome {
|
|
5
|
+
constructor(value) {
|
|
6
|
+
this.value = value;
|
|
7
|
+
this._tag = 'Some';
|
|
8
|
+
}
|
|
9
|
+
is_some() {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
is_some_and(f) {
|
|
13
|
+
return f(this.value);
|
|
14
|
+
}
|
|
15
|
+
is_none() {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
expect(_msg) {
|
|
19
|
+
return this.value;
|
|
20
|
+
}
|
|
21
|
+
map(fn) {
|
|
22
|
+
return new OptionSome(fn(this.value));
|
|
23
|
+
}
|
|
24
|
+
map_or(_defaultVal, fn) {
|
|
25
|
+
return fn(this.value);
|
|
26
|
+
}
|
|
27
|
+
and(opt) {
|
|
28
|
+
return opt;
|
|
29
|
+
}
|
|
30
|
+
and_then(fn) {
|
|
31
|
+
return fn(this.value);
|
|
32
|
+
}
|
|
33
|
+
filter(predicate) {
|
|
34
|
+
if (predicate(this.value)) {
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
return (0, exports.None)();
|
|
38
|
+
}
|
|
39
|
+
or(_opt) {
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
or_else(_fn) {
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
xor(optb) {
|
|
46
|
+
if (optb.is_some()) {
|
|
47
|
+
return (0, exports.None)();
|
|
48
|
+
}
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
unwrap() {
|
|
52
|
+
return this.value;
|
|
53
|
+
}
|
|
54
|
+
unwrap_or(optb) {
|
|
55
|
+
return this.value;
|
|
56
|
+
}
|
|
57
|
+
unwrap_or_else(_fn) {
|
|
58
|
+
return this.value;
|
|
59
|
+
}
|
|
60
|
+
unwrap_or_default() {
|
|
61
|
+
return this.value;
|
|
62
|
+
}
|
|
63
|
+
take() {
|
|
64
|
+
const value = this.value;
|
|
65
|
+
// @ts-ignore "administrative override" :-)
|
|
66
|
+
this.value = undefined;
|
|
67
|
+
return new OptionSome(value);
|
|
68
|
+
}
|
|
69
|
+
take_if(predicate) {
|
|
70
|
+
if (predicate(this.value)) {
|
|
71
|
+
return this.take();
|
|
72
|
+
}
|
|
73
|
+
return (0, exports.None)();
|
|
74
|
+
}
|
|
75
|
+
replace(value) {
|
|
76
|
+
const oldValue = this.value;
|
|
77
|
+
// @ts-ignore "administrative override" :-)
|
|
78
|
+
this.value = value;
|
|
79
|
+
return new OptionSome(oldValue);
|
|
80
|
+
}
|
|
81
|
+
zip(other) {
|
|
82
|
+
if (other.is_some()) {
|
|
83
|
+
return new OptionSome([this.value, other.unwrap()]);
|
|
84
|
+
}
|
|
85
|
+
return new OptionNone();
|
|
86
|
+
}
|
|
87
|
+
zip_with(other, f) {
|
|
88
|
+
if (other.is_some()) {
|
|
89
|
+
return new OptionSome(f(this.value, other.unwrap()));
|
|
90
|
+
}
|
|
91
|
+
return new OptionNone();
|
|
92
|
+
}
|
|
93
|
+
flatten() {
|
|
94
|
+
if (this.value instanceof OptionSome) {
|
|
95
|
+
// If the value is an OptionSome, we return it directly.
|
|
96
|
+
return this.value;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
// If the value is not an OptionSome (meaning it's an OptionNone or another type),
|
|
100
|
+
// we return None<T>(). This assumes that None<T>() creates an OptionNone<T> instance.
|
|
101
|
+
return (0, exports.None)();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
exports.OptionSome = OptionSome;
|
|
106
|
+
class OptionNone {
|
|
107
|
+
constructor() {
|
|
108
|
+
this._tag = 'None';
|
|
109
|
+
}
|
|
110
|
+
is_some() {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
is_some_and(_f) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
is_none() {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
expect(msg) {
|
|
120
|
+
throw new Error(msg);
|
|
121
|
+
}
|
|
122
|
+
map(_fn) {
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
map_or(defaultVal, _fn) {
|
|
126
|
+
return defaultVal;
|
|
127
|
+
}
|
|
128
|
+
and(_opt) {
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
and_then(_fn) {
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
filter(_predicate) {
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
or(opt) {
|
|
138
|
+
return opt;
|
|
139
|
+
}
|
|
140
|
+
or_else(fn) {
|
|
141
|
+
return fn();
|
|
142
|
+
}
|
|
143
|
+
xor(optb) {
|
|
144
|
+
return optb;
|
|
145
|
+
}
|
|
146
|
+
unwrap() {
|
|
147
|
+
throw new Error('Called Option.unwrap() on a None value');
|
|
148
|
+
}
|
|
149
|
+
unwrap_or(optb) {
|
|
150
|
+
return optb;
|
|
151
|
+
}
|
|
152
|
+
unwrap_or_else(fn) {
|
|
153
|
+
return fn();
|
|
154
|
+
}
|
|
155
|
+
unwrap_or_default() {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
take() {
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
take_if(_predicate) {
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
replace(value) {
|
|
165
|
+
return new OptionSome(value);
|
|
166
|
+
}
|
|
167
|
+
zip(other) {
|
|
168
|
+
return (0, exports.None)();
|
|
169
|
+
}
|
|
170
|
+
zip_with(_other, _f) {
|
|
171
|
+
return (0, exports.None)();
|
|
172
|
+
}
|
|
173
|
+
flatten() {
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
exports.OptionNone = OptionNone;
|
|
178
|
+
const Some = (val) => {
|
|
179
|
+
return new OptionSome(val);
|
|
180
|
+
};
|
|
181
|
+
exports.Some = Some;
|
|
182
|
+
const None = () => {
|
|
183
|
+
return new OptionNone();
|
|
184
|
+
};
|
|
185
|
+
exports.None = None;
|
|
186
|
+
const option_from_nullable = (val) => {
|
|
187
|
+
if (val === null || val === undefined) {
|
|
188
|
+
return (0, exports.None)();
|
|
189
|
+
}
|
|
190
|
+
return (0, exports.Some)(val);
|
|
191
|
+
};
|
|
192
|
+
exports.option_from_nullable = option_from_nullable;
|
|
193
|
+
const option_from_promise = (promise) => promise.then(exports.Some).catch(() => (0, exports.None)());
|
|
194
|
+
exports.option_from_promise = option_from_promise;
|
|
195
|
+
//# sourceMappingURL=option.js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ED
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
import { Option } from './option';
|
|
2
|
+
export type Result<T, E> = ResultOk<T, E> | ResultErr<T, E>;
|
|
3
|
+
export interface IResultCore<T, E> {
|
|
4
|
+
/**
|
|
5
|
+
* Checks if the result is an instance of `ResultOk`.
|
|
6
|
+
* @returns true if the result is `ResultOk`, otherwise false.
|
|
7
|
+
*
|
|
8
|
+
* Usage Example:
|
|
9
|
+
* const result = Ok(5);
|
|
10
|
+
* if (result.is_ok()) {
|
|
11
|
+
* console.log("Result is Ok");
|
|
12
|
+
* }
|
|
13
|
+
*/
|
|
14
|
+
is_ok(): this is ResultOk<T, E>;
|
|
15
|
+
/**
|
|
16
|
+
* Checks if the result is an instance of `ResultErr`.
|
|
17
|
+
* @returns true if the result is `ResultErr`, otherwise false.
|
|
18
|
+
*
|
|
19
|
+
* Usage Example:
|
|
20
|
+
* const result = Err(new Error("Error"));
|
|
21
|
+
* if (result.is_err()) {
|
|
22
|
+
* console.log("Result is Err");
|
|
23
|
+
* }
|
|
24
|
+
*/
|
|
25
|
+
is_err(): this is ResultErr<T, E>;
|
|
26
|
+
/**
|
|
27
|
+
* Retrieves the value from `ResultOk`, wrapped in an `Option`.
|
|
28
|
+
* @returns `Some` containing the value if the result is `ResultOk`, otherwise `None`.
|
|
29
|
+
*
|
|
30
|
+
* Usage Example:
|
|
31
|
+
* const result = Ok(5);
|
|
32
|
+
* const value = result.ok();
|
|
33
|
+
* if (value.is_some()) {
|
|
34
|
+
* console.log("Value:", value.unwrap());
|
|
35
|
+
* }
|
|
36
|
+
*/
|
|
37
|
+
ok(): Option<T>;
|
|
38
|
+
/**
|
|
39
|
+
* Retrieves the error from `ResultErr`, wrapped in an `Option`.
|
|
40
|
+
* @returns `Some` containing the error if the result is `ResultErr`, otherwise `None`.
|
|
41
|
+
*
|
|
42
|
+
* Usage Example:
|
|
43
|
+
* const result = Err(new Error("Error"));
|
|
44
|
+
* const error = result.err();
|
|
45
|
+
* if (error.is_some()) {
|
|
46
|
+
* console.log("Error:", error.unwrap());
|
|
47
|
+
* }
|
|
48
|
+
*/
|
|
49
|
+
err(): Option<E>;
|
|
50
|
+
/**
|
|
51
|
+
* Returns the contained `ResultOk` value, but throws an error with a provided message if
|
|
52
|
+
* the result is a `ResultErr`.
|
|
53
|
+
* @param msg The message to throw with if the result is an error.
|
|
54
|
+
* @returns The contained `ResultOk` value.
|
|
55
|
+
*
|
|
56
|
+
* Usage Example:
|
|
57
|
+
* const result = Ok(5);
|
|
58
|
+
* console.log(result.expect("This should not fail"));
|
|
59
|
+
*/
|
|
60
|
+
expect(msg: string): T;
|
|
61
|
+
/**
|
|
62
|
+
* Unwraps a `ResultOk`, yielding the contained value.
|
|
63
|
+
* @returns The `ResultOk` value.
|
|
64
|
+
* @throws Throws if the result is `ResultErr`.
|
|
65
|
+
*
|
|
66
|
+
* Usage Example:
|
|
67
|
+
* const result = Ok(5);
|
|
68
|
+
* console.log(result.unwrap());
|
|
69
|
+
*/
|
|
70
|
+
unwrap(): T;
|
|
71
|
+
/**
|
|
72
|
+
* Returns the contained `ResultErr` error, but throws an error with a provided message if
|
|
73
|
+
* the result is a `ResultOk`.
|
|
74
|
+
* @param msg The message to throw with if the result is Ok.
|
|
75
|
+
* @returns The contained `ResultErr` error.
|
|
76
|
+
*
|
|
77
|
+
* Usage Example:
|
|
78
|
+
* const result = Err(new Error("Failure"));
|
|
79
|
+
* console.log(result.expect_err("Expected an error"));
|
|
80
|
+
*/
|
|
81
|
+
expect_err(msg: string): E;
|
|
82
|
+
/**
|
|
83
|
+
* Unwraps a `ResultErr`, yielding the contained error.
|
|
84
|
+
* @returns The `ResultErr` error.
|
|
85
|
+
* @throws Throws if the result is `ResultOk`.
|
|
86
|
+
*
|
|
87
|
+
* Usage Example:
|
|
88
|
+
* const result = Err(new Error("Failure"));
|
|
89
|
+
* console.log(result.unwrap_err());
|
|
90
|
+
*/
|
|
91
|
+
unwrap_err(): E;
|
|
92
|
+
/**
|
|
93
|
+
* Converts from `IResultCore<T, E>` to `T`.
|
|
94
|
+
* @returns The contained `ResultOk` value.
|
|
95
|
+
* @throws Throws if the result is `ResultErr`.
|
|
96
|
+
*
|
|
97
|
+
* Usage Example:
|
|
98
|
+
* const result = Ok(5);
|
|
99
|
+
* console.log(result.into_ok());
|
|
100
|
+
*/
|
|
101
|
+
into_ok(): T;
|
|
102
|
+
/**
|
|
103
|
+
* Converts from `IResultCore<T, E>` to `E`.
|
|
104
|
+
* @returns The contained `ResultErr` error.
|
|
105
|
+
* @throws Throws if the result is `ResultOk`.
|
|
106
|
+
*
|
|
107
|
+
* Usage Example:
|
|
108
|
+
* const result = Err(new Error("Failure"));
|
|
109
|
+
* console.log(result.into_err());
|
|
110
|
+
*/
|
|
111
|
+
into_err(): E;
|
|
112
|
+
/**
|
|
113
|
+
* Converts between different forms of `Result`, allowing the error type to be widened.
|
|
114
|
+
*
|
|
115
|
+
* If called on a `ResultOk<T, E>`, it will return a `Result<T, never>`, effectively discarding
|
|
116
|
+
* the error type. This is useful when you want to use the `Result` in a context that expects
|
|
117
|
+
* a `Result` with a specific error type, but you know that the `Result` is an `Ok` variant.
|
|
118
|
+
*
|
|
119
|
+
* If called on a `ResultErr<T, E>`, it will return a `Result<never, E>`, effectively discarding
|
|
120
|
+
* the value type. This is useful when you want to use the `Result` in a context that expects
|
|
121
|
+
* a `Result` with a specific value type, but you know that the `Result` is an `Err` variant.
|
|
122
|
+
*
|
|
123
|
+
* This is particularly useful when trying to forward a ResultErr returned by a function whose
|
|
124
|
+
* error type overlaps with the returned error type of the current function, but whose value type
|
|
125
|
+
* does not.
|
|
126
|
+
*
|
|
127
|
+
* Usage Example:
|
|
128
|
+
* const result: Result<number, Error> = Ok<number, Error>(5);
|
|
129
|
+
* const transmuted: Result<number, never> = result.transmute();
|
|
130
|
+
*
|
|
131
|
+
* const result: Result<number, Error> = Err<number, Error>(new Error("Failure"));
|
|
132
|
+
* const transmuted: Result<never, Error> = result.transmute();
|
|
133
|
+
*/
|
|
134
|
+
transmute(): Result<T, never> | Result<never, E>;
|
|
135
|
+
}
|
|
136
|
+
export interface IResultExt<T, E> extends IResultCore<T, E> {
|
|
137
|
+
/**
|
|
138
|
+
* Checks if the result is Ok and the contained value passes a specified condition.
|
|
139
|
+
* @param f A predicate to apply to the contained value if the result is Ok.
|
|
140
|
+
* @returns true if the result is Ok and the predicate returns true, otherwise false.
|
|
141
|
+
*
|
|
142
|
+
* Usage Examples:
|
|
143
|
+
* const result = Ok(5);
|
|
144
|
+
* if (result.is_ok_and(x => x > 3)) {
|
|
145
|
+
* console.log("Result is Ok and greater than 3");
|
|
146
|
+
* }
|
|
147
|
+
*
|
|
148
|
+
* const result = Ok(2);
|
|
149
|
+
* if (!result.is_ok_and(x => x > 3)) {
|
|
150
|
+
* console.log("Result is not Ok or not greater than 3");
|
|
151
|
+
* }
|
|
152
|
+
*/
|
|
153
|
+
is_ok_and(f: (value: T) => boolean): boolean;
|
|
154
|
+
/**
|
|
155
|
+
* Checks if the result is Err and the contained error passes a specified condition.
|
|
156
|
+
* @param f A predicate to apply to the contained error if the result is Err.
|
|
157
|
+
* @returns true if the result is Err and the predicate returns true, otherwise false.
|
|
158
|
+
*
|
|
159
|
+
* Usage Examples:
|
|
160
|
+
* const result = Err(new Error("Network failure"));
|
|
161
|
+
* if (result.is_err_and(e => e.message.includes("Network"))) {
|
|
162
|
+
* console.log("Network error occurred");
|
|
163
|
+
* }
|
|
164
|
+
*/
|
|
165
|
+
is_err_and(f: (value: E) => boolean): boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Transforms the result via a mapping function if it is Ok.
|
|
168
|
+
* @param fn A function to transform the Ok value.
|
|
169
|
+
* @returns A new Result where the Ok value has been transformed.
|
|
170
|
+
*
|
|
171
|
+
* Usage Example:
|
|
172
|
+
* const result = Ok(5);
|
|
173
|
+
* const mapped = result.map(x => x * 2);
|
|
174
|
+
*
|
|
175
|
+
* const result = Err("Error");
|
|
176
|
+
* const mapped = result.map(x => x * 2); // remains Err
|
|
177
|
+
*/
|
|
178
|
+
map<U>(fn: (arg: T) => U): Result<U, E>;
|
|
179
|
+
/**
|
|
180
|
+
* Transforms the result via a mapping function if it is Ok, otherwise returns a default value.
|
|
181
|
+
* @param defaultVal A default value to return if the result is Err.
|
|
182
|
+
* @param f A function to transform the Ok value.
|
|
183
|
+
* @returns The transformed Ok value or the default value.
|
|
184
|
+
*
|
|
185
|
+
* Usage Example:
|
|
186
|
+
* const result = Ok(5);
|
|
187
|
+
* const value = result.map_or(0, x => x * 2);
|
|
188
|
+
*
|
|
189
|
+
* const result = Err("Error");
|
|
190
|
+
* const value = result.map_or(0, x => x * 2); // 0
|
|
191
|
+
*/
|
|
192
|
+
map_or<U>(defaultVal: U, f: (arg: T) => U): U;
|
|
193
|
+
/**
|
|
194
|
+
* Transforms the result via a mapping function if it is Ok, otherwise computes a default value using a function.
|
|
195
|
+
* @param defaultFunc A function to compute a default value if the result is Err.
|
|
196
|
+
* @param f A function to transform the Ok value.
|
|
197
|
+
* @returns The transformed Ok value or the computed default value.
|
|
198
|
+
*
|
|
199
|
+
* Usage Example:
|
|
200
|
+
* const result = Ok(5);
|
|
201
|
+
* const value = result.map_or_else(() => 0, x => x * 2);
|
|
202
|
+
*
|
|
203
|
+
* const result = Err("Error");
|
|
204
|
+
* const value = result.map_or_else(() => 0, x => x * 2); // 0
|
|
205
|
+
*/
|
|
206
|
+
map_or_else<U>(defaultFunc: (err: E) => U, f: (arg: T) => U): U;
|
|
207
|
+
/**
|
|
208
|
+
* Maps a `Result<T, E>` to `Result<T, U>` by applying a function to a contained `Err` value, leaving an `Ok` value untouched.
|
|
209
|
+
* @param fn A function to transform the Err value.
|
|
210
|
+
* @returns A new Result where the Err has been transformed.
|
|
211
|
+
*
|
|
212
|
+
* Usage Example:
|
|
213
|
+
* const result = Err("Error");
|
|
214
|
+
* const mappedErr = result.map_err(e => new Error(e));
|
|
215
|
+
*/
|
|
216
|
+
map_err<U>(fn: (arg: E) => U): Result<T, U>;
|
|
217
|
+
/**
|
|
218
|
+
* Applies a function to the contained value (if Ok), then returns the unmodified Result.
|
|
219
|
+
* @param f A function to apply to the Ok value.
|
|
220
|
+
* @returns The original Result.
|
|
221
|
+
*
|
|
222
|
+
* Usage Example:
|
|
223
|
+
* const result = Ok(5);
|
|
224
|
+
* result.inspect(x => console.log(`Value: ${x}`));
|
|
225
|
+
*/
|
|
226
|
+
inspect(f: (val: T) => void): Result<T, E>;
|
|
227
|
+
/**
|
|
228
|
+
* Applies a function to the contained error (if Err), then returns the unmodified Result.
|
|
229
|
+
* @param f A function to apply to the Err value.
|
|
230
|
+
* @returns The original Result.
|
|
231
|
+
*
|
|
232
|
+
* Usage Example:
|
|
233
|
+
* const result = Err("Error");
|
|
234
|
+
* result.inspect_err(e => console.log(`Error: ${e}`));
|
|
235
|
+
*/
|
|
236
|
+
inspect_err(f: (val: E) => void): Result<T, E>;
|
|
237
|
+
/**
|
|
238
|
+
* Returns `res` if the result is Ok, otherwise returns the Err value of `self`.
|
|
239
|
+
* @param res The result to return if `self` is Ok.
|
|
240
|
+
* @returns Either `res` or the original Err.
|
|
241
|
+
*
|
|
242
|
+
* Usage Example:
|
|
243
|
+
* const result = Ok(5);
|
|
244
|
+
* const other = Ok("Hello");
|
|
245
|
+
* const finalResult = result.and(other); // Ok("Hello")
|
|
246
|
+
*/
|
|
247
|
+
and<U>(res: Result<U, E>): Result<U, E>;
|
|
248
|
+
/**
|
|
249
|
+
* Calls `fn` if the result is Ok, otherwise returns the Err value of `self`.
|
|
250
|
+
* @param fn A function to apply to the Ok value.
|
|
251
|
+
* @returns The result of `fn` if the original result is Ok, otherwise the Err.
|
|
252
|
+
*
|
|
253
|
+
* Usage Example:
|
|
254
|
+
* const result = Ok(5);
|
|
255
|
+
* const finalResult = result.and_then(x => Ok(x * 2)); // Ok(10)
|
|
256
|
+
*/
|
|
257
|
+
and_then<U>(fn: (arg: T) => Result<U, E>): Result<U, E>;
|
|
258
|
+
/**
|
|
259
|
+
* Returns `res` if the result is Err, otherwise returns the Ok value of `self`.
|
|
260
|
+
* @param res The result to return if `self` is Err.
|
|
261
|
+
* @returns Either `res` or the original Ok.
|
|
262
|
+
*
|
|
263
|
+
* Usage Example:
|
|
264
|
+
* const result = Err("Error");
|
|
265
|
+
* const other = Ok(5);
|
|
266
|
+
* const finalResult = result.or(other); // Ok(5)
|
|
267
|
+
*/
|
|
268
|
+
or<U>(res: Result<U, E>): Result<U, E>;
|
|
269
|
+
/**
|
|
270
|
+
* Calls `fn` if the result is Err, otherwise returns the Ok value of `self`.
|
|
271
|
+
* @param fn A function to apply to the Err value.
|
|
272
|
+
* @returns The result of `fn` if the original result is Err, otherwise the Ok.
|
|
273
|
+
*
|
|
274
|
+
* Usage Example:
|
|
275
|
+
* const result = Err("Error");
|
|
276
|
+
* const finalResult = result.or_else(e => Ok(`Handled ${e}`)); // Ok("Handled Error")
|
|
277
|
+
*/
|
|
278
|
+
or_else<U>(fn: (arg: E) => Result<T, U>): Result<T, U>;
|
|
279
|
+
/**
|
|
280
|
+
* Returns the contained Ok value or a provided default.
|
|
281
|
+
* @param defaultVal The default value to return if the result is Err.
|
|
282
|
+
* @returns The Ok value or the default.
|
|
283
|
+
*
|
|
284
|
+
* Usage Example:
|
|
285
|
+
* const result = Ok(5);
|
|
286
|
+
* console.log(result.unwrap_or(0)); // 5
|
|
287
|
+
*
|
|
288
|
+
* const result = Err("Error");
|
|
289
|
+
* console.log(result.unwrap_or(0)); // 0
|
|
290
|
+
*/
|
|
291
|
+
unwrap_or(defaultVal: T): T;
|
|
292
|
+
/**
|
|
293
|
+
* Returns the contained Ok value or computes it from a function.
|
|
294
|
+
* @param fn A function to compute the default value if the result is Err.
|
|
295
|
+
* @returns The Ok value or the computed one.
|
|
296
|
+
*
|
|
297
|
+
* Usage Example:
|
|
298
|
+
* const result = Err("Error");
|
|
299
|
+
* console.log(result.unwrap_or_else(() => 5)); // 5
|
|
300
|
+
*/
|
|
301
|
+
unwrap_or_else(fn: (arg: E) => T): T;
|
|
302
|
+
}
|
|
303
|
+
type UnwrapResult<T> = T extends Result<infer U, any> ? U : T;
|
|
304
|
+
export interface IResultIteration<T, E> extends IResultCore<T, E> {
|
|
305
|
+
/**
|
|
306
|
+
* Returns an iterator over the potentially contained value.
|
|
307
|
+
* @returns An iterator which yields the contained value if it is `ResultOk<T, E>`.
|
|
308
|
+
*
|
|
309
|
+
* Usage Example:
|
|
310
|
+
* const okResult = Ok(5);
|
|
311
|
+
* for (const value of okResult.iter()) {
|
|
312
|
+
* console.log(value); // prints 5
|
|
313
|
+
* }
|
|
314
|
+
*
|
|
315
|
+
* const errResult = Err(new Error("error"));
|
|
316
|
+
* for (const value of errResult.iter()) {
|
|
317
|
+
* // This block will not be executed.
|
|
318
|
+
* }
|
|
319
|
+
*/
|
|
320
|
+
iter(): IterableIterator<T>;
|
|
321
|
+
/**
|
|
322
|
+
* Flattens a nested `Result` if the contained value is itself a `Result`.
|
|
323
|
+
* @returns A single-layer `Result`, by stripping one layer of `Result` container.
|
|
324
|
+
*
|
|
325
|
+
* Usage Example:
|
|
326
|
+
* const nestedOk = Ok(Ok(5));
|
|
327
|
+
* const flattened = nestedOk.flatten(); // Results in Ok(5)
|
|
328
|
+
*
|
|
329
|
+
* const nestedErr = Ok(Err(new Error("error")));
|
|
330
|
+
* const flattenedError = nestedErr.flatten(); // Results in Err(new Error("error"))
|
|
331
|
+
*/
|
|
332
|
+
flatten(): Result<UnwrapResult<T>, E>;
|
|
333
|
+
}
|
|
334
|
+
export interface IResult<T, E> extends IResultCore<T, E>, IResultExt<T, E>, IResultIteration<T, E> {
|
|
335
|
+
}
|
|
336
|
+
export declare const isResultOk: <T, E>(val: any) => val is ResultOk<T, E>;
|
|
337
|
+
export declare const isResultErr: <T, E>(val: any) => val is ResultErr<T, E>;
|
|
338
|
+
export declare class ResultOk<T, E> implements IResult<T, E> {
|
|
339
|
+
readonly value: T;
|
|
340
|
+
private readonly _tag;
|
|
341
|
+
private readonly _T;
|
|
342
|
+
private readonly _E;
|
|
343
|
+
constructor(value: T);
|
|
344
|
+
is_ok(): this is ResultOk<T, E>;
|
|
345
|
+
is_err(): this is never;
|
|
346
|
+
is_ok_and(f: (value: T) => boolean): boolean;
|
|
347
|
+
is_err_and(_f: (value: E) => boolean): boolean;
|
|
348
|
+
ok(): Option<T>;
|
|
349
|
+
err(): Option<E>;
|
|
350
|
+
map<U>(fn: (arg: T) => U): Result<U, E>;
|
|
351
|
+
map_or<U>(_d: U, f: (arg: T) => U): U;
|
|
352
|
+
map_or_else<U>(_d: (e: E) => U, f: (arg: T) => U): U;
|
|
353
|
+
map_err<U>(_fn: (arg: E) => U): Result<T, U>;
|
|
354
|
+
inspect(f: (val: T) => void): Result<T, E>;
|
|
355
|
+
inspect_err(_f: (val: E) => void): Result<T, E>;
|
|
356
|
+
iter(): IterableIterator<T>;
|
|
357
|
+
expect(_msg: string): T;
|
|
358
|
+
unwrap(): T;
|
|
359
|
+
expect_err(msg: string): E;
|
|
360
|
+
unwrap_err(): never;
|
|
361
|
+
and<U>(res: Result<U, E>): Result<U, E>;
|
|
362
|
+
and_then<U>(fn: (arg: T) => Result<U, E>): Result<U, E>;
|
|
363
|
+
or<U>(_res: Result<U, E>): Result<U, E>;
|
|
364
|
+
or_else<U>(fn: (arg: E) => Result<T, U>): Result<T, U>;
|
|
365
|
+
unwrap_or(_optb: T): T;
|
|
366
|
+
unwrap_or_else(_fn: (arg: E) => T): T;
|
|
367
|
+
flatten(): Result<UnwrapResult<T>, E>;
|
|
368
|
+
into_ok(): T;
|
|
369
|
+
into_err(): never;
|
|
370
|
+
transmute(): Result<T, never>;
|
|
371
|
+
}
|
|
372
|
+
export declare class ResultErr<T, E> implements IResult<T, E> {
|
|
373
|
+
readonly value: E;
|
|
374
|
+
private readonly _tag;
|
|
375
|
+
private readonly _T;
|
|
376
|
+
private readonly _E;
|
|
377
|
+
constructor(value: E);
|
|
378
|
+
is_ok(): this is never;
|
|
379
|
+
is_err(): this is ResultErr<T, E>;
|
|
380
|
+
is_ok_and(_f: (value: T) => boolean): boolean;
|
|
381
|
+
is_err_and(f: (value: E) => boolean): boolean;
|
|
382
|
+
ok(): Option<T>;
|
|
383
|
+
err(): Option<E>;
|
|
384
|
+
map<U>(_fn: (arg: T) => U): Result<U, E>;
|
|
385
|
+
map_or<U>(d: U, _f: (arg: T) => U): U;
|
|
386
|
+
map_or_else<U>(d: (e: E) => U, _f: (arg: T) => U): U;
|
|
387
|
+
map_err<U>(fn: (arg: E) => U): Result<T, U>;
|
|
388
|
+
inspect(_f: (val: T) => void): Result<T, E>;
|
|
389
|
+
inspect_err(f: (val: E) => void): Result<T, E>;
|
|
390
|
+
iter(): IterableIterator<T>;
|
|
391
|
+
expect(msg: string): never;
|
|
392
|
+
unwrap(): never;
|
|
393
|
+
expect_err(_msg: string): E;
|
|
394
|
+
unwrap_err(): E;
|
|
395
|
+
and<U>(_res: Result<U, E>): Result<U, E>;
|
|
396
|
+
and_then<U>(_fn: (arg: T) => Result<U, E>): Result<U, E>;
|
|
397
|
+
or<U>(res: Result<U, E>): Result<U, E>;
|
|
398
|
+
or_else<U>(fn: (arg: E) => Result<T, U>): Result<T, U>;
|
|
399
|
+
unwrap_or(optb: T): T;
|
|
400
|
+
unwrap_or_else(fn: (arg: E) => T): T;
|
|
401
|
+
flatten(): Result<UnwrapResult<T>, E>;
|
|
402
|
+
into_ok(): T;
|
|
403
|
+
into_err(): E;
|
|
404
|
+
transmute(): Result<never, E>;
|
|
405
|
+
}
|
|
406
|
+
export declare const Ok: <T, E>(val: T) => Result<T, never>;
|
|
407
|
+
export declare const Err: <T, E>(val: E) => Result<never, E>;
|
|
408
|
+
export declare const try_catch: <T, E = Error>(fn: () => T) => Result<T, E>;
|
|
409
|
+
export declare const result_from_promise: <T, E = Error>(val: Promise<T>) => Promise<Result<T, E>>;
|
|
410
|
+
export {};
|
package/dist/result.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.result_from_promise = exports.try_catch = exports.Err = exports.Ok = exports.ResultErr = exports.ResultOk = exports.isResultErr = exports.isResultOk = void 0;
|
|
4
|
+
const option_1 = require("./option");
|
|
5
|
+
const isResultOk = (val) => {
|
|
6
|
+
return val instanceof ResultOk;
|
|
7
|
+
};
|
|
8
|
+
exports.isResultOk = isResultOk;
|
|
9
|
+
const isResultErr = (val) => {
|
|
10
|
+
return val instanceof ResultErr;
|
|
11
|
+
};
|
|
12
|
+
exports.isResultErr = isResultErr;
|
|
13
|
+
class ResultOk {
|
|
14
|
+
constructor(value) {
|
|
15
|
+
this.value = value;
|
|
16
|
+
this._tag = 'Ok';
|
|
17
|
+
}
|
|
18
|
+
is_ok() {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
is_err() {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
is_ok_and(f) {
|
|
25
|
+
return f(this.value);
|
|
26
|
+
}
|
|
27
|
+
is_err_and(_f) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
ok() {
|
|
31
|
+
return (0, option_1.Some)(this.value);
|
|
32
|
+
}
|
|
33
|
+
err() {
|
|
34
|
+
return (0, option_1.None)();
|
|
35
|
+
}
|
|
36
|
+
map(fn) {
|
|
37
|
+
return new ResultOk(fn(this.value));
|
|
38
|
+
}
|
|
39
|
+
map_or(_d, f) {
|
|
40
|
+
return f(this.value);
|
|
41
|
+
}
|
|
42
|
+
map_or_else(_d, f) {
|
|
43
|
+
return f(this.value);
|
|
44
|
+
}
|
|
45
|
+
map_err(_fn) {
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
inspect(f) {
|
|
49
|
+
f(this.value);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
inspect_err(_f) {
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
iter() {
|
|
56
|
+
return [this.value][Symbol.iterator]();
|
|
57
|
+
}
|
|
58
|
+
expect(_msg) {
|
|
59
|
+
return this.value;
|
|
60
|
+
}
|
|
61
|
+
unwrap() {
|
|
62
|
+
return this.value;
|
|
63
|
+
}
|
|
64
|
+
//unwrap_or_default(): T {
|
|
65
|
+
// ! not implemented
|
|
66
|
+
//}
|
|
67
|
+
expect_err(msg) {
|
|
68
|
+
throw new Error(msg);
|
|
69
|
+
}
|
|
70
|
+
unwrap_err() {
|
|
71
|
+
throw new Error('Called Result.unwrap_err() on an Ok value: ' + this.value);
|
|
72
|
+
}
|
|
73
|
+
and(res) {
|
|
74
|
+
return res;
|
|
75
|
+
}
|
|
76
|
+
and_then(fn) {
|
|
77
|
+
return fn(this.value);
|
|
78
|
+
}
|
|
79
|
+
or(_res) {
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
or_else(fn) {
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
unwrap_or(_optb) {
|
|
86
|
+
return this.value;
|
|
87
|
+
}
|
|
88
|
+
unwrap_or_else(_fn) {
|
|
89
|
+
return this.value;
|
|
90
|
+
}
|
|
91
|
+
flatten() {
|
|
92
|
+
if (this.value instanceof ResultOk || this.value instanceof ResultErr) {
|
|
93
|
+
return this.value;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
// This case should not happen if T is always a Result,
|
|
97
|
+
// but it's here to satisfy TypeScript's type checker.
|
|
98
|
+
return new ResultOk(this.value);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
into_ok() {
|
|
102
|
+
return this.value;
|
|
103
|
+
}
|
|
104
|
+
into_err() {
|
|
105
|
+
throw new Error('Called Result.into_err() on an Ok value: ' + this.value);
|
|
106
|
+
}
|
|
107
|
+
transmute() {
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.ResultOk = ResultOk;
|
|
112
|
+
class ResultErr {
|
|
113
|
+
constructor(value) {
|
|
114
|
+
this.value = value;
|
|
115
|
+
this._tag = 'Err';
|
|
116
|
+
}
|
|
117
|
+
is_ok() {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
is_err() {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
is_ok_and(_f) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
is_err_and(f) {
|
|
127
|
+
return f(this.value);
|
|
128
|
+
}
|
|
129
|
+
ok() {
|
|
130
|
+
return (0, option_1.None)();
|
|
131
|
+
}
|
|
132
|
+
err() {
|
|
133
|
+
return (0, option_1.Some)(this.value);
|
|
134
|
+
}
|
|
135
|
+
map(_fn) {
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
map_or(d, _f) {
|
|
139
|
+
return d;
|
|
140
|
+
}
|
|
141
|
+
map_or_else(d, _f) {
|
|
142
|
+
return d(this.value);
|
|
143
|
+
}
|
|
144
|
+
map_err(fn) {
|
|
145
|
+
return new ResultErr(fn(this.value));
|
|
146
|
+
}
|
|
147
|
+
inspect(_f) {
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
inspect_err(f) {
|
|
151
|
+
f(this.value);
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
iter() {
|
|
155
|
+
return [][Symbol.iterator]();
|
|
156
|
+
}
|
|
157
|
+
expect(msg) {
|
|
158
|
+
throw new Error(msg);
|
|
159
|
+
}
|
|
160
|
+
unwrap() {
|
|
161
|
+
throw new Error('Called Result.unwrap() on an Err value: ' + this.value);
|
|
162
|
+
}
|
|
163
|
+
//unwrap_or_default(): never {
|
|
164
|
+
// // ! not implemented
|
|
165
|
+
//}
|
|
166
|
+
expect_err(_msg) {
|
|
167
|
+
return this.value;
|
|
168
|
+
}
|
|
169
|
+
unwrap_err() {
|
|
170
|
+
return this.value;
|
|
171
|
+
}
|
|
172
|
+
and(_res) {
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
and_then(_fn) {
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
or(res) {
|
|
179
|
+
return res;
|
|
180
|
+
}
|
|
181
|
+
or_else(fn) {
|
|
182
|
+
return fn(this.value);
|
|
183
|
+
}
|
|
184
|
+
unwrap_or(optb) {
|
|
185
|
+
return optb;
|
|
186
|
+
}
|
|
187
|
+
unwrap_or_else(fn) {
|
|
188
|
+
return fn(this.value);
|
|
189
|
+
}
|
|
190
|
+
flatten() {
|
|
191
|
+
return new ResultErr(this.value);
|
|
192
|
+
}
|
|
193
|
+
into_ok() {
|
|
194
|
+
throw new Error('Called Result.into_ok() on an Err value: ' + this.value);
|
|
195
|
+
}
|
|
196
|
+
into_err() {
|
|
197
|
+
return this.value;
|
|
198
|
+
}
|
|
199
|
+
transmute() {
|
|
200
|
+
return this;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
exports.ResultErr = ResultErr;
|
|
204
|
+
const Ok = (val) => {
|
|
205
|
+
return new ResultOk(val);
|
|
206
|
+
};
|
|
207
|
+
exports.Ok = Ok;
|
|
208
|
+
const Err = (val) => {
|
|
209
|
+
return new ResultErr(val);
|
|
210
|
+
};
|
|
211
|
+
exports.Err = Err;
|
|
212
|
+
const try_catch = (fn) => {
|
|
213
|
+
try {
|
|
214
|
+
return (0, exports.Ok)(fn());
|
|
215
|
+
// @ts-ignore (error is nominally of type any / unknown, not Error)
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
return (0, exports.Err)(error);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
exports.try_catch = try_catch;
|
|
222
|
+
const result_from_promise = async (val) => {
|
|
223
|
+
try {
|
|
224
|
+
return new ResultOk(await val);
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
return new ResultErr(error);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
exports.result_from_promise = result_from_promise;
|
|
231
|
+
//# sourceMappingURL=result.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result.js","sourceRo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M,EAAoB,EAAE;IACjD,OAAO,IAAI,QAAQ,CAAO,GAAG,CAAqB,CAAC;AACvD,CAAC,CAAC;AAFW,QAAA,EAAE,MAEb;AAEK,MAAM,GAAG,GAAG,CAAO,GAAM,EAAoB,EAAE;IAClD,OAAO,IAAI,SAAS,CAAO,GAAG,CAAqB,CAAC;AACxD,CAAC,CAAC;AAFW,QAAA,GAAG,OAEd;AAEK,MAAM,SAAS,GAClB,CACI,EAAW,EACC,EAAE;IACd,IAAI;QACA,OAAO,IAAA,UAAE,EAAC,EAAE,EAAE,CAAC,CAAC;QAChB,mEAAmE;KACtE;IAAC,OAAO,KAAY,EAAE;QACnB,OAAO,IAAA,WAAG,EAAC,KAAK,CAAC,CAAC;KACrB;AACL,CAAC,CAAC;AAVO,QAAA,SAAS,aAUhB;AAEC,MAAM,mBAAmB,GAC5B,KAAK,EACD,GAAe,EACM,EAAE;IACvB,IAAI;QACA,OAAO,IAAI,QAAQ,CAAW,MAAM,GAAG,CAAC,CAAC;KAC5C;IAAC,OAAO,KAAc,EAAE;QACrB,OAAO,IAAI,SAAS,CAAW,KAAU,CAAC,CAAC;KAC9C;AACL,CAAC,CAAC;AATO,QAAA,mBAAmB,uBAS1B"}
|
package/package.json
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
2
|
+
"name": "rsult",
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/lib.js",
|
|
6
|
+
"types": "dist/lib.d.ts",
|
|
7
|
+
"repository": "https://github.com/indicium-ag/rsult",
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"author": "Kenan Sulayman <kenan@sly.mn>",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@swc/core": "^1.4.12",
|
|
13
|
+
"@swc/jest": "^0.2.36",
|
|
14
|
+
"@types/jest": "^29.5.12",
|
|
15
|
+
"jest": "^29.7.0"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
19
|
+
}
|
|
19
20
|
}
|
package/src/option.ts
CHANGED
|
@@ -429,7 +429,7 @@ export class OptionNone<T> implements IOption<T> {
|
|
|
429
429
|
throw new Error(msg);
|
|
430
430
|
}
|
|
431
431
|
|
|
432
|
-
map<U>(_fn: (arg:
|
|
432
|
+
map<U>(_fn: (arg: T) => U): Option<U> {
|
|
433
433
|
return this as any;
|
|
434
434
|
}
|
|
435
435
|
|
|
@@ -441,7 +441,7 @@ export class OptionNone<T> implements IOption<T> {
|
|
|
441
441
|
return this as any;
|
|
442
442
|
}
|
|
443
443
|
|
|
444
|
-
and_then<U>(_fn: (arg:
|
|
444
|
+
and_then<U>(_fn: (arg: T) => Option<U>): Option<U> {
|
|
445
445
|
return this as any;
|
|
446
446
|
}
|
|
447
447
|
|
|
@@ -465,11 +465,11 @@ export class OptionNone<T> implements IOption<T> {
|
|
|
465
465
|
throw new Error('Called Option.unwrap() on a None value');
|
|
466
466
|
}
|
|
467
467
|
|
|
468
|
-
unwrap_or(optb:
|
|
468
|
+
unwrap_or(optb: T): T {
|
|
469
469
|
return optb;
|
|
470
470
|
}
|
|
471
471
|
|
|
472
|
-
unwrap_or_else(fn: () =>
|
|
472
|
+
unwrap_or_else(fn: () => T): T {
|
|
473
473
|
return fn();
|
|
474
474
|
}
|
|
475
475
|
|