result_option 0.1.4 → 0.1.6

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/index.d.ts CHANGED
@@ -9,7 +9,7 @@ export declare class Option<T> {
9
9
  * @param value
10
10
  * @returns `Option<T>` where `Some(value)` if `value` !== null and `None` if `value` === null.
11
11
  */
12
- static From<T>(value: T | null): Option<T>;
12
+ static from<T>(value: T | null): Option<T>;
13
13
  /**
14
14
  *
15
15
  * @param value - The value to be `Some`.
@@ -48,6 +48,10 @@ export declare class Option<T> {
48
48
  * x.is_some_and((x) => x > 1); // -> true
49
49
  */
50
50
  is_some_and(f: (value: T) => boolean): boolean;
51
+ /**
52
+ * Same as `Option.is_some_and()` but async.
53
+ */
54
+ is_some_and_async(f: (value: T) => Promise<boolean>): Promise<boolean>;
51
55
  /**
52
56
  *
53
57
  * @returns `true` if the `Option` is `None` or the value inside of it matches the predicate.
@@ -59,6 +63,10 @@ export declare class Option<T> {
59
63
  * y.is_none_or((x) => x > 1); // -> true
60
64
  */
61
65
  is_none_or(f: (value: T) => boolean): boolean;
66
+ /**
67
+ * Same as `Option.is_none_or()` but async.
68
+ */
69
+ is_none_or_async(f: (value: T) => Promise<boolean>): Promise<boolean>;
62
70
  /**
63
71
  *
64
72
  * @returns the contained `Some` value.
@@ -195,6 +203,10 @@ export declare class Option<T> {
195
203
  * const z = x.and_then(y); // -> z = Option.None()
196
204
  */
197
205
  and_then<U>(f: (val: T) => Option<U>): Option<U>;
206
+ /**
207
+ * Same as `Option.and_then()` but async.
208
+ */
209
+ and_then_async<U>(f: (val: T) => Promise<Option<U>>): Promise<Option<U>>;
198
210
  /**
199
211
  *
200
212
  * @param optb value.
@@ -223,6 +235,10 @@ export declare class Option<T> {
223
235
  * const z = x.or_else(y); // -> z = Option.Some(100)
224
236
  */
225
237
  or_else(f: () => Option<T>): Option<T>;
238
+ /**
239
+ * Same as `Option.or_else()` but async.
240
+ */
241
+ or_else_async(f: () => Promise<Option<T>>): Promise<Option<T>>;
226
242
  /**
227
243
  * @param cases - An object containing the match arms
228
244
  * @param cases.Some - Called with the value if Option is Some
@@ -293,6 +309,10 @@ export declare class Result<T, E> {
293
309
  * x.is_ok_and((x) => x > 5); // -> false
294
310
  */
295
311
  is_ok_and(f: (value: T) => boolean): boolean;
312
+ /**
313
+ * Same as `Result.is_ok_and()` but async.
314
+ */
315
+ is_ok_and_async(f: (value: T) => Promise<boolean>): Promise<boolean>;
296
316
  /**
297
317
  * @returns `true` if the `Result` is `Err` and the error matches the predicate.
298
318
  * @example
@@ -303,6 +323,10 @@ export declare class Result<T, E> {
303
323
  * x.is_err_and((e) => e === "error"); // -> false
304
324
  */
305
325
  is_err_and(f: (error: E) => boolean): boolean;
326
+ /**
327
+ * Same as `Result.is_ok_and()` but async.
328
+ */
329
+ is_err_and_async(f: (error: E) => Promise<boolean>): Promise<boolean>;
306
330
  /**
307
331
  * @returns `Option.Some(v)` if `Ok(v)`, otherwise `Option.None()`.
308
332
  * @example
@@ -461,6 +485,10 @@ export declare class Result<T, E> {
461
485
  * const y = x.and_then((val) => Result.Ok(val * 2)); // -> y = Result.Err("error")
462
486
  */
463
487
  and_then<U>(op: (val: T) => Result<U, E>): Result<U, E>;
488
+ /**
489
+ * Same as `Result.and_then()` but async.
490
+ */
491
+ and_then_async<U>(op: (val: T) => Promise<Result<U, E>>): Promise<Result<U, E>>;
464
492
  /**
465
493
  * @param res - value.
466
494
  * @returns `this` if `Ok`, otherwise returns `res`.
@@ -485,6 +513,10 @@ export declare class Result<T, E> {
485
513
  * const y = x.or_else((e) => Result.Ok(0)); // -> y = Result.Ok(10)
486
514
  */
487
515
  or_else<F>(op: (err: E) => Result<T, F>): Result<T, F>;
516
+ /**
517
+ * Same as `Result.or_else()` but async.
518
+ */
519
+ or_else_async<F>(op: (err: E) => Promise<Result<T, F>>): Promise<Result<T, F>>;
488
520
  /**
489
521
  * @param cases - An object containing the match arms.
490
522
  * @param cases.Ok - Called with the value if `Result` is `Ok`.
package/dist/index.js CHANGED
@@ -23,7 +23,7 @@ export class Option {
23
23
  * @param value
24
24
  * @returns `Option<T>` where `Some(value)` if `value` !== null and `None` if `value` === null.
25
25
  */
26
- static From(value) {
26
+ static from(value) {
27
27
  return value === null ? Option.None() : Option.Some(value);
28
28
  }
29
29
  /**
@@ -77,6 +77,15 @@ export class Option {
77
77
  None: () => false,
78
78
  });
79
79
  }
80
+ /**
81
+ * Same as `Option.is_some_and()` but async.
82
+ */
83
+ async is_some_and_async(f) {
84
+ return this.match({
85
+ Some: f,
86
+ None: async () => false,
87
+ });
88
+ }
80
89
  /**
81
90
  *
82
91
  * @returns `true` if the `Option` is `None` or the value inside of it matches the predicate.
@@ -93,6 +102,15 @@ export class Option {
93
102
  None: () => true,
94
103
  });
95
104
  }
105
+ /**
106
+ * Same as `Option.is_none_or()` but async.
107
+ */
108
+ async is_none_or_async(f) {
109
+ return this.match({
110
+ Some: f,
111
+ None: async () => true,
112
+ });
113
+ }
96
114
  /**
97
115
  *
98
116
  * @returns the contained `Some` value.
@@ -288,6 +306,15 @@ export class Option {
288
306
  None: () => Option.None(),
289
307
  });
290
308
  }
309
+ /**
310
+ * Same as `Option.and_then()` but async.
311
+ */
312
+ async and_then_async(f) {
313
+ return this.match({
314
+ Some: f,
315
+ None: async () => Option.None(),
316
+ });
317
+ }
291
318
  /**
292
319
  *
293
320
  * @param optb value.
@@ -326,6 +353,15 @@ export class Option {
326
353
  None: f,
327
354
  });
328
355
  }
356
+ /**
357
+ * Same as `Option.or_else()` but async.
358
+ */
359
+ async or_else_async(f) {
360
+ return this.match({
361
+ Some: async (_) => this,
362
+ None: f,
363
+ });
364
+ }
329
365
  /**
330
366
  * @param cases - An object containing the match arms
331
367
  * @param cases.Some - Called with the value if Option is Some
@@ -413,6 +449,15 @@ export class Result {
413
449
  Err: (_) => false,
414
450
  });
415
451
  }
452
+ /**
453
+ * Same as `Result.is_ok_and()` but async.
454
+ */
455
+ async is_ok_and_async(f) {
456
+ return this.match({
457
+ Ok: f,
458
+ Err: async (_) => false,
459
+ });
460
+ }
416
461
  /**
417
462
  * @returns `true` if the `Result` is `Err` and the error matches the predicate.
418
463
  * @example
@@ -428,6 +473,15 @@ export class Result {
428
473
  Err: f,
429
474
  });
430
475
  }
476
+ /**
477
+ * Same as `Result.is_ok_and()` but async.
478
+ */
479
+ async is_err_and_async(f) {
480
+ return this.match({
481
+ Ok: async (_) => false,
482
+ Err: f,
483
+ });
484
+ }
431
485
  /**
432
486
  * @returns `Option.Some(v)` if `Ok(v)`, otherwise `Option.None()`.
433
487
  * @example
@@ -664,6 +718,15 @@ export class Result {
664
718
  Err: (e) => Result.Err(e),
665
719
  });
666
720
  }
721
+ /**
722
+ * Same as `Result.and_then()` but async.
723
+ */
724
+ async and_then_async(op) {
725
+ return this.match({
726
+ Ok: op,
727
+ Err: async (e) => Result.Err(e),
728
+ });
729
+ }
667
730
  /**
668
731
  * @param res - value.
669
732
  * @returns `this` if `Ok`, otherwise returns `res`.
@@ -698,6 +761,15 @@ export class Result {
698
761
  Err: op,
699
762
  });
700
763
  }
764
+ /**
765
+ * Same as `Result.or_else()` but async.
766
+ */
767
+ async or_else_async(op) {
768
+ return this.match({
769
+ Ok: async (t) => Result.Ok(t),
770
+ Err: op,
771
+ });
772
+ }
701
773
  /**
702
774
  * @param cases - An object containing the match arms.
703
775
  * @param cases.Ok - Called with the value if `Result` is `Ok`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "result_option",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Result and Option types for typescript.",
5
5
  "main": "./dist/index.ts",
6
6
  "module": "./dist/index.mts",