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.
@@ -0,0 +1,504 @@
1
+ /**
2
+ * Represents a value that may or may not exist.
3
+ */
4
+ export declare class Option<T> {
5
+ #private;
6
+ private constructor();
7
+ /**
8
+ *
9
+ * @param value - The value to be `Some`.
10
+ * @returns Option<T> with `Some` = `value`.
11
+ * @example
12
+ * const option = Option.Some(10);
13
+ */
14
+ static Some<T>(value: T): Option<T>;
15
+ /**
16
+ *
17
+ * @returns Option<T> with `None`.
18
+ * @example
19
+ * const option = Option.None();
20
+ */
21
+ static None(): Option<never>;
22
+ /**
23
+ *
24
+ * @returns true if the `Option` is a `Some` value.
25
+ * @example
26
+ * const x = Option.Some(10);
27
+ * x.is_some(); // -> true
28
+ */
29
+ is_some(): boolean;
30
+ /**
31
+ *
32
+ * @returns true if the `Option` is `None`.
33
+ * @example
34
+ * const x = Option.None();
35
+ * x.is_none(); // -> true
36
+ */
37
+ is_none(): boolean;
38
+ /**
39
+ * @returns `true` if the `Option` is a `Some` and the value inside of it maches the predicate.
40
+ * @example
41
+ * const x = Option.Some(2);
42
+ * x.is_some_and((x) => x > 1); // -> true
43
+ */
44
+ is_some_and(f: (value: T) => boolean): boolean;
45
+ /**
46
+ *
47
+ * @returns `true` if the `Option` is `None` or the value inside of it matches the predicate.
48
+ * @example
49
+ * const x = Option.Some(2);
50
+ * x.is_none_or((x) => x > 1); // -> true
51
+ *
52
+ * const y = Option.None();
53
+ * y.is_none_or((x) => x > 1); // -> true
54
+ */
55
+ is_none_or(f: (value: T) => boolean): boolean;
56
+ /**
57
+ *
58
+ * @returns the contained `Some` value.
59
+ * @throws {Error} if `None`.
60
+ * @example
61
+ * const x = Option.Some("hello");
62
+ * const y = x.unwrap(); // y = "hello"
63
+ *
64
+ * const x = Option.None();
65
+ * const y = x.unwrap(); // throws Error
66
+ */
67
+ unwrap(): T;
68
+ /**
69
+ * @param dft default value.
70
+ * @returns the contained `Some` value or `dft`.
71
+ * @example
72
+ * const x = Option.Some("car");
73
+ * const y = x.unwrap_or("bus"); // -> y = "car"
74
+ *
75
+ * const x = Option.None();
76
+ * const y = x.unwrap_or("bus"); // -> y = "bus"
77
+ */
78
+ unwrap_or(dft: T): T;
79
+ /**
80
+ * @param f lambda.
81
+ * @returns the contained `Some` value or calls `f` and returns its result.
82
+ * @example
83
+ * const x = Option.Some("car");
84
+ * const y = x.unwrap_or_else(() => "bus"); // -> y = "car"
85
+ *
86
+ * const x = Option.None();
87
+ * const y = x.unwrap_or(() => "bus"); // -> y = "bus"
88
+ */
89
+ unwrap_or_else(f: () => T): T;
90
+ /**
91
+ *
92
+ * @param msg message in case of a fail.
93
+ * @returns the contained `Some` value.
94
+ * @throws {Error} if the `Option` is `None` with the message `msg`.
95
+ * @example
96
+ * const x = Option.Some("value");
97
+ * const y = x.expect("I want it now!"); // -> y = "value"
98
+ *
99
+ * const x = Option.None();
100
+ * const y = x.expect("I want it now!"); // -> throws Error("I want it now!")
101
+ */
102
+ expect(msg: string): T;
103
+ /**
104
+ * @param f lambda.
105
+ * @returns `Option<U>` from `Option<T>` by applying `f` to the value if `Some` and `None` otherwise.
106
+ * @example
107
+ * const x = Option.Some(10);
108
+ * const y = x.map((val) => val * 2); // -> y = Option.Some(20)
109
+ *
110
+ * const x = Option.None();
111
+ * const y = x.map((val) => val * 2); // -> y = Option.None
112
+ */
113
+ map<U>(f: (val: T) => U): Option<U>;
114
+ /**
115
+ *
116
+ * @param dft default value.
117
+ * @param f lambda.
118
+ * @returns `dft` if `None` or applies `f` to the value if `Some`.
119
+ * @example
120
+ * const x = Option.Some(10);
121
+ * const y = x.map_or(37, (val) => val*2); // -> y = 20
122
+ *
123
+ * const x = Option.None();
124
+ * const y = x.map_or(37, (val) => val*2); // -> y = 37
125
+ */
126
+ map_or<U>(dft: U, f: (val: T) => U): U;
127
+ /**
128
+ *
129
+ * @param dft default lambda.
130
+ * @param f lambda.
131
+ * @returns calls `dft` if `None` or applies `f` to the value if `Some`.
132
+ * @example
133
+ * const x = Option.Some(10);
134
+ * const y = x.map_or_else(() => 37, (val) => val*2); // -> y = 20
135
+ *
136
+ * const x = Option.None();
137
+ * const y = x.map_or_else(() => 37, (val) => val*2); // -> y = 37
138
+ */
139
+ map_or_else<U>(dft: () => U, f: (val: T) => U): U;
140
+ /**
141
+ *
142
+ * @param err error value.
143
+ * @returns `Result<T, E>`, mapping `Option.Some(v)` to `Result.Ok(v)` and `Option.None` to `Result.Err(err)`.
144
+ * @example
145
+ * const x = Option.Some("foo");
146
+ * const y = x.ok_or(0); // -> y = Result.Ok("foo")
147
+ *
148
+ * const x = Option.None();
149
+ * const y = x.ok_or(0); // -> y = Result.Err(0)
150
+ */
151
+ ok_or<E>(err: E): Result<T, E>;
152
+ /**
153
+ *
154
+ * @param err error lambda.
155
+ * @returns `Result<T, E>`, mapping `Option.Some(v)` to `Result.Ok(v)` and `Option.None` to `Result.Err(err)`.
156
+ * @example
157
+ * const x = Option.Some("foo");
158
+ * const y = x.ok_or_else(() => 0); // -> y = Result.Ok("foo")
159
+ *
160
+ * const x = Option.None();
161
+ * const y = x.ok_or(() => 0); // -> y = Result.Err(0)
162
+ */
163
+ ok_or_else<E>(err: () => E): Result<T, E>;
164
+ /**
165
+ *
166
+ * @param optb value.
167
+ * @returns `None` if the `Option` is `None`, otherwise returns `optb`.
168
+ * @example
169
+ * const x = Option.Some("Hello");
170
+ * const y = Option.None();
171
+ * const z = x.and(y); // -> z = Option.None()
172
+ *
173
+ * const x = Option.Some("Hello");
174
+ * const y = Option.Some(3);
175
+ * const z = x.and(y); // -> z = Option.Some(3)
176
+ */
177
+ and<U>(optb: Option<U>): Option<U>;
178
+ /**
179
+ *
180
+ * @param optb lambda.
181
+ * @returns `None` if the `Option` is `None`, otherwise calls `optb` with the value and returns the result.
182
+ * @example
183
+ * const x = Option.Some(500);
184
+ * const y = (val: Option<number>) => val / 5;
185
+ * const z = x.and_then(y); // -> z = Option.Some(100)
186
+ *
187
+ * const x = Option.None();
188
+ * const y = (val: Option<number>) => val / 5;
189
+ * const z = x.and_then(y); // -> z = Option.None()
190
+ */
191
+ and_then<U>(f: (val: T) => Option<U>): Option<U>;
192
+ /**
193
+ *
194
+ * @param optb value.
195
+ * @returns `this` if `Some`, otherwise `optb`.
196
+ * @example
197
+ * const x = Option.Some(2);
198
+ * const y = Option.None();
199
+ * const z = x.or(y); // -> z = Option.Some(2)
200
+ *
201
+ * const x = Option.None();
202
+ * const y = Option.Some(100);
203
+ * const z = x.or(y); // -> z = Option.Some(100)
204
+ */
205
+ or(optb: Option<T>): Option<T>;
206
+ /**
207
+ *
208
+ * @param optb lambda value.
209
+ * @returns `this` if `Some`, otherwise calls `optb` and returns the result.
210
+ * @example
211
+ * const x = Option.Some(2);
212
+ * const y = () => Option.None();
213
+ * const z = x.or_else(y); // -> z = Option.Some(2)
214
+ *
215
+ * const x = Option.None();
216
+ * const y = () => Option.Some(100);
217
+ * const z = x.or_else(y); // -> z = Option.Some(100)
218
+ */
219
+ or_else(f: () => Option<T>): Option<T>;
220
+ /**
221
+ * @param cases - An object containing the match arms
222
+ * @param cases.Some - Called with the value if Option is Some
223
+ * @param cases.None - Called if Option is None
224
+ * @returns The result of the matched arm
225
+ * @example
226
+ * const some = Option.some(42);
227
+ * some.match({
228
+ * Some: (value) => `got ${value}`, // -> "got 42"
229
+ * None: () => "got nothing"
230
+ * });
231
+ *
232
+ * const none = Option.none();
233
+ * none.match({
234
+ * Some: (value) => `got ${value}`,
235
+ * None: () => "got nothing" // -> "got nothing"
236
+ * });
237
+ */
238
+ match<R>(cases: {
239
+ Some: (value: T) => R;
240
+ None: () => R;
241
+ }): R;
242
+ }
243
+ export declare class Result<T, E> {
244
+ #private;
245
+ private constructor();
246
+ /**
247
+ * @param value - The value to be `Ok`.
248
+ * @returns `Result<T, E>` with `Ok` = `value`.
249
+ * @example
250
+ * const result = Result.Ok(10);
251
+ */
252
+ static Ok<T, E>(value: T): Result<T, E>;
253
+ /**
254
+ * @param error - The error to be `Err`.
255
+ * @returns `Result<T, E>` with `Err` = `error`.
256
+ * @example
257
+ * const result = Result.Err("something went wrong");
258
+ */
259
+ static Err<T, E>(error: E): Result<T, E>;
260
+ /**
261
+ * @returns `true` if the `Result` is `Ok`.
262
+ * @example
263
+ * const x = Result.Ok(10);
264
+ * x.is_ok(); // -> true
265
+ *
266
+ * const x = Result.Err("error");
267
+ * x.is_ok(); // -> false
268
+ */
269
+ is_ok(): boolean;
270
+ /**
271
+ * @returns `true` if the `Result` is `Err`.
272
+ * @example
273
+ * const x = Result.Ok(10);
274
+ * x.is_err(); // -> false
275
+ *
276
+ * const x = Result.Err("error");
277
+ * x.is_err(); // -> true
278
+ */
279
+ is_err(): boolean;
280
+ /**
281
+ * @returns `true` if the `Result` is `Ok` and the value matches the predicate.
282
+ * @example
283
+ * const x = Result.Ok(10);
284
+ * x.is_ok_and((x) => x > 5); // -> true
285
+ *
286
+ * const x = Result.Err("error");
287
+ * x.is_ok_and((x) => x > 5); // -> false
288
+ */
289
+ is_ok_and(f: (value: T) => boolean): boolean;
290
+ /**
291
+ * @returns `true` if the `Result` is `Err` and the error matches the predicate.
292
+ * @example
293
+ * const x = Result.Err("error");
294
+ * x.is_err_and((e) => e === "error"); // -> true
295
+ *
296
+ * const x = Result.Ok(10);
297
+ * x.is_err_and((e) => e === "error"); // -> false
298
+ */
299
+ is_err_and(f: (error: E) => boolean): boolean;
300
+ /**
301
+ * @returns `Option.Some(v)` if `Ok(v)`, otherwise `Option.None()`.
302
+ * @example
303
+ * const x = Result.Ok("hello");
304
+ * x.ok(); // -> Option.Some("hello")
305
+ *
306
+ * const x = Result.Err("error");
307
+ * x.ok(); // -> Option.None()
308
+ */
309
+ ok(): Option<T>;
310
+ /**
311
+ * @returns `Option.Some(e)` if `Err(e)`, otherwise `Option.None()`.
312
+ * @example
313
+ * const x = Result.Err("error");
314
+ * x.err(); // -> Option.Some("error")
315
+ *
316
+ * const x = Result.Ok(10);
317
+ * x.err(); // -> Option.None()
318
+ */
319
+ err(): Option<E>;
320
+ /**
321
+ * @param f - lambda.
322
+ * @returns `Result<U, E>` by applying `f` to the `Ok` value, leaving `Err` untouched.
323
+ * @example
324
+ * const x = Result.Ok(10);
325
+ * const y = x.map((val) => val * 2); // -> y = Result.Ok(20)
326
+ *
327
+ * const x = Result.Err("error");
328
+ * const y = x.map((val) => val * 2); // -> y = Result.Err("error")
329
+ */
330
+ map<U>(f: (val: T) => U): Result<U, E>;
331
+ /**
332
+ * @param dft - default value.
333
+ * @param f - lambda.
334
+ * @returns `dft` if `Err`, or applies `f` to the `Ok` value.
335
+ * @example
336
+ * const x = Result.Ok(10);
337
+ * const y = x.map_or(37, (val) => val * 2); // -> y = 20
338
+ *
339
+ * const x = Result.Err("error");
340
+ * const y = x.map_or(37, (val) => val * 2); // -> y = 37
341
+ */
342
+ map_or<U>(dft: U, f: (val: T) => U): U;
343
+ /**
344
+ * @param dft - default lambda, receives the error value.
345
+ * @param f - lambda.
346
+ * @returns calls `dft` with the error if `Err`, or applies `f` to the `Ok` value.
347
+ * @example
348
+ * const x = Result.Ok(10);
349
+ * const y = x.map_or_else((e) => 37, (val) => val * 2); // -> y = 20
350
+ *
351
+ * const x = Result.Err("error");
352
+ * const y = x.map_or_else((e) => 37, (val) => val * 2); // -> y = 37
353
+ */
354
+ map_or_else<U>(dft: (err: E) => U, f: (val: T) => U): U;
355
+ /**
356
+ * @param op - lambda.
357
+ * @returns `Result<T, F>` by applying `op` to the `Err` value, leaving `Ok` untouched.
358
+ * @example
359
+ * const x = Result.Err("error");
360
+ * const y = x.map_err((e) => e.length); // -> y = Result.Err(5)
361
+ *
362
+ * const x = Result.Ok(10);
363
+ * const y = x.map_err((e) => e.length); // -> y = Result.Ok(10)
364
+ */
365
+ map_err<F>(op: (err: E) => F): Result<T, F>;
366
+ /**
367
+ * @returns the contained `Ok` value.
368
+ * @throws {Error} if the `Result` is `Err`.
369
+ * @example
370
+ * const x = Result.Ok(10);
371
+ * x.unwrap(); // -> 10
372
+ *
373
+ * const x = Result.Err("error");
374
+ * x.unwrap(); // throws Error: "called Result.unwrap() on an Err value: error"
375
+ */
376
+ unwrap(): T;
377
+ /**
378
+ * @param dft - default value.
379
+ * @returns the contained `Ok` value or `dft` if `Err`.
380
+ * @example
381
+ * const x = Result.Ok(10);
382
+ * x.unwrap_or(0); // -> 10
383
+ *
384
+ * const x = Result.Err("error");
385
+ * x.unwrap_or(0); // -> 0
386
+ */
387
+ unwrap_or(dft: T): T;
388
+ /**
389
+ * @param op - lambda.
390
+ * @returns the contained `Ok` value or calls `op` with the error and returns the result.
391
+ * @example
392
+ * const x = Result.Ok(10);
393
+ * x.unwrap_or_else((e) => 0); // -> 10
394
+ *
395
+ * const x = Result.Err("error");
396
+ * x.unwrap_or_else((e) => 0); // -> 0
397
+ */
398
+ unwrap_or_else(op: (err: E) => T): T;
399
+ /**
400
+ * @returns the contained `Err` value.
401
+ * @throws {Error} if the `Result` is `Ok`.
402
+ * @example
403
+ * const x = Result.Err("error");
404
+ * x.unwrap_err(); // -> "error"
405
+ *
406
+ * const x = Result.Ok(10);
407
+ * x.unwrap_err(); // throws Error: "called Result.unwrap_err() on an Ok value: 10"
408
+ */
409
+ unwrap_err(): E;
410
+ /**
411
+ * @param msg - message in case of failure.
412
+ * @returns the contained `Ok` value.
413
+ * @throws {Error} if the `Result` is `Err`, with the message `msg: err`.
414
+ * @example
415
+ * const x = Result.Ok(10);
416
+ * x.expect("should have a value"); // -> 10
417
+ *
418
+ * const x = Result.Err("error");
419
+ * x.expect("should have a value"); // throws Error: "should have a value: error"
420
+ */
421
+ expect(msg: string): T;
422
+ /**
423
+ * @param msg - message in case of failure.
424
+ * @returns the contained `Err` value.
425
+ * @throws {Error} if the `Result` is `Ok`, with the message `msg: value`.
426
+ * @example
427
+ * const x = Result.Err("error");
428
+ * x.expect_err("should have failed"); // -> "error"
429
+ *
430
+ * const x = Result.Ok(10);
431
+ * x.expect_err("should have failed"); // throws Error: "should have failed: 10"
432
+ */
433
+ expect_err(msg: string): E;
434
+ /**
435
+ * @param res - value.
436
+ * @returns `Err` if the `Result` is `Err`, otherwise returns `res`.
437
+ * @example
438
+ * const x = Result.Ok(10);
439
+ * const y = Result.Err("late error");
440
+ * const z = x.and(y); // -> z = Result.Err("late error")
441
+ *
442
+ * const x = Result.Ok(10);
443
+ * const y = Result.Ok(20);
444
+ * const z = x.and(y); // -> z = Result.Ok(20)
445
+ */
446
+ and<U>(res: Result<U, E>): Result<U, E>;
447
+ /**
448
+ * @param op - lambda.
449
+ * @returns `Err` if the `Result` is `Err`, otherwise calls `op` with the value and returns the result.
450
+ * @example
451
+ * const x = Result.Ok(10);
452
+ * const y = x.and_then((val) => Result.Ok(val * 2)); // -> y = Result.Ok(20)
453
+ *
454
+ * const x = Result.Err("error");
455
+ * const y = x.and_then((val) => Result.Ok(val * 2)); // -> y = Result.Err("error")
456
+ */
457
+ and_then<U>(op: (val: T) => Result<U, E>): Result<U, E>;
458
+ /**
459
+ * @param res - value.
460
+ * @returns `this` if `Ok`, otherwise returns `res`.
461
+ * @example
462
+ * const x = Result.Err("error");
463
+ * const y = Result.Ok(10);
464
+ * const z = x.or(y); // -> z = Result.Ok(10)
465
+ *
466
+ * const x = Result.Ok(10);
467
+ * const y = Result.Err("late error");
468
+ * const z = x.or(y); // -> z = Result.Ok(10)
469
+ */
470
+ or<F>(res: Result<T, F>): Result<T, F>;
471
+ /**
472
+ * @param op - lambda.
473
+ * @returns `this` if `Ok`, otherwise calls `op` with the error and returns the result.
474
+ * @example
475
+ * const x = Result.Err("error");
476
+ * const y = x.or_else((e) => Result.Ok(0)); // -> y = Result.Ok(0)
477
+ *
478
+ * const x = Result.Ok(10);
479
+ * const y = x.or_else((e) => Result.Ok(0)); // -> y = Result.Ok(10)
480
+ */
481
+ or_else<F>(op: (err: E) => Result<T, F>): Result<T, F>;
482
+ /**
483
+ * @param cases - An object containing the match arms.
484
+ * @param cases.Ok - Called with the value if `Result` is `Ok`.
485
+ * @param cases.Err - Called with the error if `Result` is `Err`.
486
+ * @returns The result of the matched arm.
487
+ * @example
488
+ * const ok = Result.Ok(42);
489
+ * ok.match({
490
+ * Ok: (value) => `got ${value}`, // -> "got 42"
491
+ * Err: (error) => `error: ${error}`
492
+ * });
493
+ *
494
+ * const err = Result.Err("oops");
495
+ * err.match({
496
+ * Ok: (value) => `got ${value}`,
497
+ * Err: (error) => `error: ${error}` // -> "error: oops"
498
+ * });
499
+ */
500
+ match<R>(cases: {
501
+ Ok: (value: T) => R;
502
+ Err: (Err: E) => R;
503
+ }): R;
504
+ }