result_option 0.1.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/LICENSE.APACHE +202 -0
- package/LICENSE.MIT +21 -0
- package/README.md +217 -0
- package/dist/index.d.ts +504 -0
- package/dist/index.js +719 -0
- package/dist/src/index.d.ts +0 -0
- package/dist/src/index.js +1 -0
- package/package.json +28 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,719 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _Option_val, _Result_val, _Result_err;
|
|
13
|
+
/**
|
|
14
|
+
* Represents a value that may or may not exist.
|
|
15
|
+
*/
|
|
16
|
+
export class Option {
|
|
17
|
+
constructor(value) {
|
|
18
|
+
_Option_val.set(this, void 0);
|
|
19
|
+
__classPrivateFieldSet(this, _Option_val, value, "f");
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
*
|
|
23
|
+
* @param value - The value to be `Some`.
|
|
24
|
+
* @returns Option<T> with `Some` = `value`.
|
|
25
|
+
* @example
|
|
26
|
+
* const option = Option.Some(10);
|
|
27
|
+
*/
|
|
28
|
+
static Some(value) {
|
|
29
|
+
return new Option(value);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
*
|
|
33
|
+
* @returns Option<T> with `None`.
|
|
34
|
+
* @example
|
|
35
|
+
* const option = Option.None();
|
|
36
|
+
*/
|
|
37
|
+
static None() {
|
|
38
|
+
return new Option(null);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
*
|
|
42
|
+
* @returns true if the `Option` is a `Some` value.
|
|
43
|
+
* @example
|
|
44
|
+
* const x = Option.Some(10);
|
|
45
|
+
* x.is_some(); // -> true
|
|
46
|
+
*/
|
|
47
|
+
is_some() {
|
|
48
|
+
return __classPrivateFieldGet(this, _Option_val, "f") ? true : false;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
*
|
|
52
|
+
* @returns true if the `Option` is `None`.
|
|
53
|
+
* @example
|
|
54
|
+
* const x = Option.None();
|
|
55
|
+
* x.is_none(); // -> true
|
|
56
|
+
*/
|
|
57
|
+
is_none() {
|
|
58
|
+
return !this.is_some();
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* @returns `true` if the `Option` is a `Some` and the value inside of it maches the predicate.
|
|
62
|
+
* @example
|
|
63
|
+
* const x = Option.Some(2);
|
|
64
|
+
* x.is_some_and((x) => x > 1); // -> true
|
|
65
|
+
*/
|
|
66
|
+
is_some_and(f) {
|
|
67
|
+
return this.match({
|
|
68
|
+
Some: f,
|
|
69
|
+
None: () => false
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
*
|
|
74
|
+
* @returns `true` if the `Option` is `None` or the value inside of it matches the predicate.
|
|
75
|
+
* @example
|
|
76
|
+
* const x = Option.Some(2);
|
|
77
|
+
* x.is_none_or((x) => x > 1); // -> true
|
|
78
|
+
*
|
|
79
|
+
* const y = Option.None();
|
|
80
|
+
* y.is_none_or((x) => x > 1); // -> true
|
|
81
|
+
*/
|
|
82
|
+
is_none_or(f) {
|
|
83
|
+
return this.match({
|
|
84
|
+
Some: f,
|
|
85
|
+
None: () => true,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
*
|
|
90
|
+
* @returns the contained `Some` value.
|
|
91
|
+
* @throws {Error} if `None`.
|
|
92
|
+
* @example
|
|
93
|
+
* const x = Option.Some("hello");
|
|
94
|
+
* const y = x.unwrap(); // y = "hello"
|
|
95
|
+
*
|
|
96
|
+
* const x = Option.None();
|
|
97
|
+
* const y = x.unwrap(); // throws Error
|
|
98
|
+
*/
|
|
99
|
+
unwrap() {
|
|
100
|
+
return this.match({
|
|
101
|
+
Some: (val) => val,
|
|
102
|
+
None: () => {
|
|
103
|
+
throw new Error("called Option.unwrap() on a none value");
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* @param dft default value.
|
|
109
|
+
* @returns the contained `Some` value or `dft`.
|
|
110
|
+
* @example
|
|
111
|
+
* const x = Option.Some("car");
|
|
112
|
+
* const y = x.unwrap_or("bus"); // -> y = "car"
|
|
113
|
+
*
|
|
114
|
+
* const x = Option.None();
|
|
115
|
+
* const y = x.unwrap_or("bus"); // -> y = "bus"
|
|
116
|
+
*/
|
|
117
|
+
unwrap_or(dft) {
|
|
118
|
+
return this.match({
|
|
119
|
+
Some: (val) => val,
|
|
120
|
+
None: () => dft
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* @param f lambda.
|
|
125
|
+
* @returns the contained `Some` value or calls `f` and returns its result.
|
|
126
|
+
* @example
|
|
127
|
+
* const x = Option.Some("car");
|
|
128
|
+
* const y = x.unwrap_or_else(() => "bus"); // -> y = "car"
|
|
129
|
+
*
|
|
130
|
+
* const x = Option.None();
|
|
131
|
+
* const y = x.unwrap_or(() => "bus"); // -> y = "bus"
|
|
132
|
+
*/
|
|
133
|
+
unwrap_or_else(f) {
|
|
134
|
+
return this.match({
|
|
135
|
+
Some: (val) => val,
|
|
136
|
+
None: f
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
*
|
|
141
|
+
* @param msg message in case of a fail.
|
|
142
|
+
* @returns the contained `Some` value.
|
|
143
|
+
* @throws {Error} if the `Option` is `None` with the message `msg`.
|
|
144
|
+
* @example
|
|
145
|
+
* const x = Option.Some("value");
|
|
146
|
+
* const y = x.expect("I want it now!"); // -> y = "value"
|
|
147
|
+
*
|
|
148
|
+
* const x = Option.None();
|
|
149
|
+
* const y = x.expect("I want it now!"); // -> throws Error("I want it now!")
|
|
150
|
+
*/
|
|
151
|
+
expect(msg) {
|
|
152
|
+
return this.match({
|
|
153
|
+
Some: (val) => val,
|
|
154
|
+
None: () => {
|
|
155
|
+
throw new Error(msg);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* @param f lambda.
|
|
161
|
+
* @returns `Option<U>` from `Option<T>` by applying `f` to the value if `Some` and `None` otherwise.
|
|
162
|
+
* @example
|
|
163
|
+
* const x = Option.Some(10);
|
|
164
|
+
* const y = x.map((val) => val * 2); // -> y = Option.Some(20)
|
|
165
|
+
*
|
|
166
|
+
* const x = Option.None();
|
|
167
|
+
* const y = x.map((val) => val * 2); // -> y = Option.None
|
|
168
|
+
*/
|
|
169
|
+
map(f) {
|
|
170
|
+
return this.match({
|
|
171
|
+
Some: (val) => Option.Some(f(val)),
|
|
172
|
+
None: () => Option.None()
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
*
|
|
177
|
+
* @param dft default value.
|
|
178
|
+
* @param f lambda.
|
|
179
|
+
* @returns `dft` if `None` or applies `f` to the value if `Some`.
|
|
180
|
+
* @example
|
|
181
|
+
* const x = Option.Some(10);
|
|
182
|
+
* const y = x.map_or(37, (val) => val*2); // -> y = 20
|
|
183
|
+
*
|
|
184
|
+
* const x = Option.None();
|
|
185
|
+
* const y = x.map_or(37, (val) => val*2); // -> y = 37
|
|
186
|
+
*/
|
|
187
|
+
map_or(dft, f) {
|
|
188
|
+
return this.match({
|
|
189
|
+
Some: f,
|
|
190
|
+
None: () => dft
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
*
|
|
195
|
+
* @param dft default lambda.
|
|
196
|
+
* @param f lambda.
|
|
197
|
+
* @returns calls `dft` if `None` or applies `f` to the value if `Some`.
|
|
198
|
+
* @example
|
|
199
|
+
* const x = Option.Some(10);
|
|
200
|
+
* const y = x.map_or_else(() => 37, (val) => val*2); // -> y = 20
|
|
201
|
+
*
|
|
202
|
+
* const x = Option.None();
|
|
203
|
+
* const y = x.map_or_else(() => 37, (val) => val*2); // -> y = 37
|
|
204
|
+
*/
|
|
205
|
+
map_or_else(dft, f) {
|
|
206
|
+
return this.match({
|
|
207
|
+
Some: f,
|
|
208
|
+
None: dft
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
*
|
|
213
|
+
* @param err error value.
|
|
214
|
+
* @returns `Result<T, E>`, mapping `Option.Some(v)` to `Result.Ok(v)` and `Option.None` to `Result.Err(err)`.
|
|
215
|
+
* @example
|
|
216
|
+
* const x = Option.Some("foo");
|
|
217
|
+
* const y = x.ok_or(0); // -> y = Result.Ok("foo")
|
|
218
|
+
*
|
|
219
|
+
* const x = Option.None();
|
|
220
|
+
* const y = x.ok_or(0); // -> y = Result.Err(0)
|
|
221
|
+
*/
|
|
222
|
+
ok_or(err) {
|
|
223
|
+
return this.match({
|
|
224
|
+
Some: (val) => Result.Ok(val),
|
|
225
|
+
None: () => Result.Err(err)
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
*
|
|
230
|
+
* @param err error lambda.
|
|
231
|
+
* @returns `Result<T, E>`, mapping `Option.Some(v)` to `Result.Ok(v)` and `Option.None` to `Result.Err(err)`.
|
|
232
|
+
* @example
|
|
233
|
+
* const x = Option.Some("foo");
|
|
234
|
+
* const y = x.ok_or_else(() => 0); // -> y = Result.Ok("foo")
|
|
235
|
+
*
|
|
236
|
+
* const x = Option.None();
|
|
237
|
+
* const y = x.ok_or(() => 0); // -> y = Result.Err(0)
|
|
238
|
+
*/
|
|
239
|
+
ok_or_else(err) {
|
|
240
|
+
return this.match({
|
|
241
|
+
Some: (val) => Result.Ok(val),
|
|
242
|
+
None: () => Result.Err(err())
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
*
|
|
247
|
+
* @param optb value.
|
|
248
|
+
* @returns `None` if the `Option` is `None`, otherwise returns `optb`.
|
|
249
|
+
* @example
|
|
250
|
+
* const x = Option.Some("Hello");
|
|
251
|
+
* const y = Option.None();
|
|
252
|
+
* const z = x.and(y); // -> z = Option.None()
|
|
253
|
+
*
|
|
254
|
+
* const x = Option.Some("Hello");
|
|
255
|
+
* const y = Option.Some(3);
|
|
256
|
+
* const z = x.and(y); // -> z = Option.Some(3)
|
|
257
|
+
*/
|
|
258
|
+
and(optb) {
|
|
259
|
+
return this.match({
|
|
260
|
+
Some: (_) => optb,
|
|
261
|
+
None: () => Option.None()
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
*
|
|
266
|
+
* @param optb lambda.
|
|
267
|
+
* @returns `None` if the `Option` is `None`, otherwise calls `optb` with the value and returns the result.
|
|
268
|
+
* @example
|
|
269
|
+
* const x = Option.Some(500);
|
|
270
|
+
* const y = (val: Option<number>) => val / 5;
|
|
271
|
+
* const z = x.and_then(y); // -> z = Option.Some(100)
|
|
272
|
+
*
|
|
273
|
+
* const x = Option.None();
|
|
274
|
+
* const y = (val: Option<number>) => val / 5;
|
|
275
|
+
* const z = x.and_then(y); // -> z = Option.None()
|
|
276
|
+
*/
|
|
277
|
+
and_then(f) {
|
|
278
|
+
return this.match({
|
|
279
|
+
Some: f,
|
|
280
|
+
None: () => Option.None(),
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
*
|
|
285
|
+
* @param optb value.
|
|
286
|
+
* @returns `this` if `Some`, otherwise `optb`.
|
|
287
|
+
* @example
|
|
288
|
+
* const x = Option.Some(2);
|
|
289
|
+
* const y = Option.None();
|
|
290
|
+
* const z = x.or(y); // -> z = Option.Some(2)
|
|
291
|
+
*
|
|
292
|
+
* const x = Option.None();
|
|
293
|
+
* const y = Option.Some(100);
|
|
294
|
+
* const z = x.or(y); // -> z = Option.Some(100)
|
|
295
|
+
*/
|
|
296
|
+
or(optb) {
|
|
297
|
+
return this.match({
|
|
298
|
+
Some: (_) => this,
|
|
299
|
+
None: () => optb
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
*
|
|
304
|
+
* @param optb lambda value.
|
|
305
|
+
* @returns `this` if `Some`, otherwise calls `optb` and returns the result.
|
|
306
|
+
* @example
|
|
307
|
+
* const x = Option.Some(2);
|
|
308
|
+
* const y = () => Option.None();
|
|
309
|
+
* const z = x.or_else(y); // -> z = Option.Some(2)
|
|
310
|
+
*
|
|
311
|
+
* const x = Option.None();
|
|
312
|
+
* const y = () => Option.Some(100);
|
|
313
|
+
* const z = x.or_else(y); // -> z = Option.Some(100)
|
|
314
|
+
*/
|
|
315
|
+
or_else(f) {
|
|
316
|
+
return this.match({
|
|
317
|
+
Some: (_) => this,
|
|
318
|
+
None: f
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* @param cases - An object containing the match arms
|
|
323
|
+
* @param cases.Some - Called with the value if Option is Some
|
|
324
|
+
* @param cases.None - Called if Option is None
|
|
325
|
+
* @returns The result of the matched arm
|
|
326
|
+
* @example
|
|
327
|
+
* const some = Option.some(42);
|
|
328
|
+
* some.match({
|
|
329
|
+
* Some: (value) => `got ${value}`, // -> "got 42"
|
|
330
|
+
* None: () => "got nothing"
|
|
331
|
+
* });
|
|
332
|
+
*
|
|
333
|
+
* const none = Option.none();
|
|
334
|
+
* none.match({
|
|
335
|
+
* Some: (value) => `got ${value}`,
|
|
336
|
+
* None: () => "got nothing" // -> "got nothing"
|
|
337
|
+
* });
|
|
338
|
+
*/
|
|
339
|
+
match(cases) {
|
|
340
|
+
if (__classPrivateFieldGet(this, _Option_val, "f"))
|
|
341
|
+
return cases.Some(__classPrivateFieldGet(this, _Option_val, "f"));
|
|
342
|
+
return cases.None();
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
_Option_val = new WeakMap();
|
|
346
|
+
export class Result {
|
|
347
|
+
constructor(value, error) {
|
|
348
|
+
_Result_val.set(this, void 0);
|
|
349
|
+
_Result_err.set(this, void 0);
|
|
350
|
+
__classPrivateFieldSet(this, _Result_val, value, "f");
|
|
351
|
+
__classPrivateFieldSet(this, _Result_err, error, "f");
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* @param value - The value to be `Ok`.
|
|
355
|
+
* @returns `Result<T, E>` with `Ok` = `value`.
|
|
356
|
+
* @example
|
|
357
|
+
* const result = Result.Ok(10);
|
|
358
|
+
*/
|
|
359
|
+
static Ok(value) {
|
|
360
|
+
return new Result(value, null);
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* @param error - The error to be `Err`.
|
|
364
|
+
* @returns `Result<T, E>` with `Err` = `error`.
|
|
365
|
+
* @example
|
|
366
|
+
* const result = Result.Err("something went wrong");
|
|
367
|
+
*/
|
|
368
|
+
static Err(error) {
|
|
369
|
+
return new Result(null, error);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* @returns `true` if the `Result` is `Ok`.
|
|
373
|
+
* @example
|
|
374
|
+
* const x = Result.Ok(10);
|
|
375
|
+
* x.is_ok(); // -> true
|
|
376
|
+
*
|
|
377
|
+
* const x = Result.Err("error");
|
|
378
|
+
* x.is_ok(); // -> false
|
|
379
|
+
*/
|
|
380
|
+
is_ok() {
|
|
381
|
+
return __classPrivateFieldGet(this, _Result_val, "f") ? true : false;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* @returns `true` if the `Result` is `Err`.
|
|
385
|
+
* @example
|
|
386
|
+
* const x = Result.Ok(10);
|
|
387
|
+
* x.is_err(); // -> false
|
|
388
|
+
*
|
|
389
|
+
* const x = Result.Err("error");
|
|
390
|
+
* x.is_err(); // -> true
|
|
391
|
+
*/
|
|
392
|
+
is_err() {
|
|
393
|
+
return !this.is_ok();
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* @returns `true` if the `Result` is `Ok` and the value matches the predicate.
|
|
397
|
+
* @example
|
|
398
|
+
* const x = Result.Ok(10);
|
|
399
|
+
* x.is_ok_and((x) => x > 5); // -> true
|
|
400
|
+
*
|
|
401
|
+
* const x = Result.Err("error");
|
|
402
|
+
* x.is_ok_and((x) => x > 5); // -> false
|
|
403
|
+
*/
|
|
404
|
+
is_ok_and(f) {
|
|
405
|
+
return this.match({
|
|
406
|
+
Ok: f,
|
|
407
|
+
Err: (_) => false
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* @returns `true` if the `Result` is `Err` and the error matches the predicate.
|
|
412
|
+
* @example
|
|
413
|
+
* const x = Result.Err("error");
|
|
414
|
+
* x.is_err_and((e) => e === "error"); // -> true
|
|
415
|
+
*
|
|
416
|
+
* const x = Result.Ok(10);
|
|
417
|
+
* x.is_err_and((e) => e === "error"); // -> false
|
|
418
|
+
*/
|
|
419
|
+
is_err_and(f) {
|
|
420
|
+
return this.match({
|
|
421
|
+
Ok: (_) => false,
|
|
422
|
+
Err: f
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* @returns `Option.Some(v)` if `Ok(v)`, otherwise `Option.None()`.
|
|
427
|
+
* @example
|
|
428
|
+
* const x = Result.Ok("hello");
|
|
429
|
+
* x.ok(); // -> Option.Some("hello")
|
|
430
|
+
*
|
|
431
|
+
* const x = Result.Err("error");
|
|
432
|
+
* x.ok(); // -> Option.None()
|
|
433
|
+
*/
|
|
434
|
+
ok() {
|
|
435
|
+
return this.match({
|
|
436
|
+
Ok: (x) => Option.Some(x),
|
|
437
|
+
Err: (_) => Option.None()
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* @returns `Option.Some(e)` if `Err(e)`, otherwise `Option.None()`.
|
|
442
|
+
* @example
|
|
443
|
+
* const x = Result.Err("error");
|
|
444
|
+
* x.err(); // -> Option.Some("error")
|
|
445
|
+
*
|
|
446
|
+
* const x = Result.Ok(10);
|
|
447
|
+
* x.err(); // -> Option.None()
|
|
448
|
+
*/
|
|
449
|
+
err() {
|
|
450
|
+
return this.match({
|
|
451
|
+
Ok: (_) => Option.None(),
|
|
452
|
+
Err: (x) => Option.Some(x)
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* @param f - lambda.
|
|
457
|
+
* @returns `Result<U, E>` by applying `f` to the `Ok` value, leaving `Err` untouched.
|
|
458
|
+
* @example
|
|
459
|
+
* const x = Result.Ok(10);
|
|
460
|
+
* const y = x.map((val) => val * 2); // -> y = Result.Ok(20)
|
|
461
|
+
*
|
|
462
|
+
* const x = Result.Err("error");
|
|
463
|
+
* const y = x.map((val) => val * 2); // -> y = Result.Err("error")
|
|
464
|
+
*/
|
|
465
|
+
map(f) {
|
|
466
|
+
return this.match({
|
|
467
|
+
Ok: (t) => Result.Ok(f(t)),
|
|
468
|
+
Err: (e) => Result.Err(e)
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* @param dft - default value.
|
|
473
|
+
* @param f - lambda.
|
|
474
|
+
* @returns `dft` if `Err`, or applies `f` to the `Ok` value.
|
|
475
|
+
* @example
|
|
476
|
+
* const x = Result.Ok(10);
|
|
477
|
+
* const y = x.map_or(37, (val) => val * 2); // -> y = 20
|
|
478
|
+
*
|
|
479
|
+
* const x = Result.Err("error");
|
|
480
|
+
* const y = x.map_or(37, (val) => val * 2); // -> y = 37
|
|
481
|
+
*/
|
|
482
|
+
map_or(dft, f) {
|
|
483
|
+
return this.match({
|
|
484
|
+
Ok: f,
|
|
485
|
+
Err: (_) => dft
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* @param dft - default lambda, receives the error value.
|
|
490
|
+
* @param f - lambda.
|
|
491
|
+
* @returns calls `dft` with the error if `Err`, or applies `f` to the `Ok` value.
|
|
492
|
+
* @example
|
|
493
|
+
* const x = Result.Ok(10);
|
|
494
|
+
* const y = x.map_or_else((e) => 37, (val) => val * 2); // -> y = 20
|
|
495
|
+
*
|
|
496
|
+
* const x = Result.Err("error");
|
|
497
|
+
* const y = x.map_or_else((e) => 37, (val) => val * 2); // -> y = 37
|
|
498
|
+
*/
|
|
499
|
+
map_or_else(dft, f) {
|
|
500
|
+
return this.match({
|
|
501
|
+
Ok: f,
|
|
502
|
+
Err: dft
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* @param op - lambda.
|
|
507
|
+
* @returns `Result<T, F>` by applying `op` to the `Err` value, leaving `Ok` untouched.
|
|
508
|
+
* @example
|
|
509
|
+
* const x = Result.Err("error");
|
|
510
|
+
* const y = x.map_err((e) => e.length); // -> y = Result.Err(5)
|
|
511
|
+
*
|
|
512
|
+
* const x = Result.Ok(10);
|
|
513
|
+
* const y = x.map_err((e) => e.length); // -> y = Result.Ok(10)
|
|
514
|
+
*/
|
|
515
|
+
map_err(op) {
|
|
516
|
+
return this.match({
|
|
517
|
+
Ok: (val) => Result.Ok(val),
|
|
518
|
+
Err: (e) => Result.Err(op(e))
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* @returns the contained `Ok` value.
|
|
523
|
+
* @throws {Error} if the `Result` is `Err`.
|
|
524
|
+
* @example
|
|
525
|
+
* const x = Result.Ok(10);
|
|
526
|
+
* x.unwrap(); // -> 10
|
|
527
|
+
*
|
|
528
|
+
* const x = Result.Err("error");
|
|
529
|
+
* x.unwrap(); // throws Error: "called Result.unwrap() on an Err value: error"
|
|
530
|
+
*/
|
|
531
|
+
unwrap() {
|
|
532
|
+
return this.match({
|
|
533
|
+
Ok: (t) => t,
|
|
534
|
+
Err: (e) => {
|
|
535
|
+
throw new Error(`called Result.unwrap() on an Err value: ${e}`);
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* @param dft - default value.
|
|
541
|
+
* @returns the contained `Ok` value or `dft` if `Err`.
|
|
542
|
+
* @example
|
|
543
|
+
* const x = Result.Ok(10);
|
|
544
|
+
* x.unwrap_or(0); // -> 10
|
|
545
|
+
*
|
|
546
|
+
* const x = Result.Err("error");
|
|
547
|
+
* x.unwrap_or(0); // -> 0
|
|
548
|
+
*/
|
|
549
|
+
unwrap_or(dft) {
|
|
550
|
+
return this.match({
|
|
551
|
+
Ok: (t) => t,
|
|
552
|
+
Err: (_) => dft,
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* @param op - lambda.
|
|
557
|
+
* @returns the contained `Ok` value or calls `op` with the error and returns the result.
|
|
558
|
+
* @example
|
|
559
|
+
* const x = Result.Ok(10);
|
|
560
|
+
* x.unwrap_or_else((e) => 0); // -> 10
|
|
561
|
+
*
|
|
562
|
+
* const x = Result.Err("error");
|
|
563
|
+
* x.unwrap_or_else((e) => 0); // -> 0
|
|
564
|
+
*/
|
|
565
|
+
unwrap_or_else(op) {
|
|
566
|
+
return this.match({
|
|
567
|
+
Ok: (t) => t,
|
|
568
|
+
Err: op
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* @returns the contained `Err` value.
|
|
573
|
+
* @throws {Error} if the `Result` is `Ok`.
|
|
574
|
+
* @example
|
|
575
|
+
* const x = Result.Err("error");
|
|
576
|
+
* x.unwrap_err(); // -> "error"
|
|
577
|
+
*
|
|
578
|
+
* const x = Result.Ok(10);
|
|
579
|
+
* x.unwrap_err(); // throws Error: "called Result.unwrap_err() on an Ok value: 10"
|
|
580
|
+
*/
|
|
581
|
+
unwrap_err() {
|
|
582
|
+
return this.match({
|
|
583
|
+
Ok: (t) => {
|
|
584
|
+
throw new Error(`called Result.unwrap_err() on an Ok value: ${t}`);
|
|
585
|
+
},
|
|
586
|
+
Err: (e) => e,
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* @param msg - message in case of failure.
|
|
591
|
+
* @returns the contained `Ok` value.
|
|
592
|
+
* @throws {Error} if the `Result` is `Err`, with the message `msg: err`.
|
|
593
|
+
* @example
|
|
594
|
+
* const x = Result.Ok(10);
|
|
595
|
+
* x.expect("should have a value"); // -> 10
|
|
596
|
+
*
|
|
597
|
+
* const x = Result.Err("error");
|
|
598
|
+
* x.expect("should have a value"); // throws Error: "should have a value: error"
|
|
599
|
+
*/
|
|
600
|
+
expect(msg) {
|
|
601
|
+
return this.match({
|
|
602
|
+
Ok: (t) => t,
|
|
603
|
+
Err: (e) => {
|
|
604
|
+
throw new Error(`${msg}: ${e}`);
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* @param msg - message in case of failure.
|
|
610
|
+
* @returns the contained `Err` value.
|
|
611
|
+
* @throws {Error} if the `Result` is `Ok`, with the message `msg: value`.
|
|
612
|
+
* @example
|
|
613
|
+
* const x = Result.Err("error");
|
|
614
|
+
* x.expect_err("should have failed"); // -> "error"
|
|
615
|
+
*
|
|
616
|
+
* const x = Result.Ok(10);
|
|
617
|
+
* x.expect_err("should have failed"); // throws Error: "should have failed: 10"
|
|
618
|
+
*/
|
|
619
|
+
expect_err(msg) {
|
|
620
|
+
return this.match({
|
|
621
|
+
Ok: (t) => {
|
|
622
|
+
throw new Error(`${msg}: ${t}`);
|
|
623
|
+
},
|
|
624
|
+
Err: (e) => e,
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* @param res - value.
|
|
629
|
+
* @returns `Err` if the `Result` is `Err`, otherwise returns `res`.
|
|
630
|
+
* @example
|
|
631
|
+
* const x = Result.Ok(10);
|
|
632
|
+
* const y = Result.Err("late error");
|
|
633
|
+
* const z = x.and(y); // -> z = Result.Err("late error")
|
|
634
|
+
*
|
|
635
|
+
* const x = Result.Ok(10);
|
|
636
|
+
* const y = Result.Ok(20);
|
|
637
|
+
* const z = x.and(y); // -> z = Result.Ok(20)
|
|
638
|
+
*/
|
|
639
|
+
and(res) {
|
|
640
|
+
return this.match({
|
|
641
|
+
Ok: (_) => res,
|
|
642
|
+
Err: (e) => Result.Err(e)
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* @param op - lambda.
|
|
647
|
+
* @returns `Err` if the `Result` is `Err`, otherwise calls `op` with the value and returns the result.
|
|
648
|
+
* @example
|
|
649
|
+
* const x = Result.Ok(10);
|
|
650
|
+
* const y = x.and_then((val) => Result.Ok(val * 2)); // -> y = Result.Ok(20)
|
|
651
|
+
*
|
|
652
|
+
* const x = Result.Err("error");
|
|
653
|
+
* const y = x.and_then((val) => Result.Ok(val * 2)); // -> y = Result.Err("error")
|
|
654
|
+
*/
|
|
655
|
+
and_then(op) {
|
|
656
|
+
return this.match({
|
|
657
|
+
Ok: op,
|
|
658
|
+
Err: (e) => Result.Err(e)
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* @param res - value.
|
|
663
|
+
* @returns `this` if `Ok`, otherwise returns `res`.
|
|
664
|
+
* @example
|
|
665
|
+
* const x = Result.Err("error");
|
|
666
|
+
* const y = Result.Ok(10);
|
|
667
|
+
* const z = x.or(y); // -> z = Result.Ok(10)
|
|
668
|
+
*
|
|
669
|
+
* const x = Result.Ok(10);
|
|
670
|
+
* const y = Result.Err("late error");
|
|
671
|
+
* const z = x.or(y); // -> z = Result.Ok(10)
|
|
672
|
+
*/
|
|
673
|
+
or(res) {
|
|
674
|
+
return this.match({
|
|
675
|
+
Ok: (v) => Result.Ok(v),
|
|
676
|
+
Err: (_) => res
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* @param op - lambda.
|
|
681
|
+
* @returns `this` if `Ok`, otherwise calls `op` with the error and returns the result.
|
|
682
|
+
* @example
|
|
683
|
+
* const x = Result.Err("error");
|
|
684
|
+
* const y = x.or_else((e) => Result.Ok(0)); // -> y = Result.Ok(0)
|
|
685
|
+
*
|
|
686
|
+
* const x = Result.Ok(10);
|
|
687
|
+
* const y = x.or_else((e) => Result.Ok(0)); // -> y = Result.Ok(10)
|
|
688
|
+
*/
|
|
689
|
+
or_else(op) {
|
|
690
|
+
return this.match({
|
|
691
|
+
Ok: (t) => Result.Ok(t),
|
|
692
|
+
Err: op
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* @param cases - An object containing the match arms.
|
|
697
|
+
* @param cases.Ok - Called with the value if `Result` is `Ok`.
|
|
698
|
+
* @param cases.Err - Called with the error if `Result` is `Err`.
|
|
699
|
+
* @returns The result of the matched arm.
|
|
700
|
+
* @example
|
|
701
|
+
* const ok = Result.Ok(42);
|
|
702
|
+
* ok.match({
|
|
703
|
+
* Ok: (value) => `got ${value}`, // -> "got 42"
|
|
704
|
+
* Err: (error) => `error: ${error}`
|
|
705
|
+
* });
|
|
706
|
+
*
|
|
707
|
+
* const err = Result.Err("oops");
|
|
708
|
+
* err.match({
|
|
709
|
+
* Ok: (value) => `got ${value}`,
|
|
710
|
+
* Err: (error) => `error: ${error}` // -> "error: oops"
|
|
711
|
+
* });
|
|
712
|
+
*/
|
|
713
|
+
match(cases) {
|
|
714
|
+
if (__classPrivateFieldGet(this, _Result_val, "f"))
|
|
715
|
+
return cases.Ok(__classPrivateFieldGet(this, _Result_val, "f"));
|
|
716
|
+
return cases.Err(__classPrivateFieldGet(this, _Result_err, "f"));
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
_Result_val = new WeakMap(), _Result_err = new WeakMap();
|
|
File without changes
|