result_option 0.1.5 → 0.1.7
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 +48 -0
- package/dist/index.js +108 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -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.
|
|
@@ -117,6 +125,10 @@ export declare class Option<T> {
|
|
|
117
125
|
* const y = x.map((val) => val * 2); // -> y = Option.None
|
|
118
126
|
*/
|
|
119
127
|
map<U>(f: (val: T) => U): Option<U>;
|
|
128
|
+
/**
|
|
129
|
+
* Same as `Option.map()` but async.
|
|
130
|
+
*/
|
|
131
|
+
map_async<U>(f: (val: T) => Promise<U>): Promise<Option<U>>;
|
|
120
132
|
/**
|
|
121
133
|
*
|
|
122
134
|
* @param dft default value.
|
|
@@ -167,6 +179,10 @@ export declare class Option<T> {
|
|
|
167
179
|
* const y = x.ok_or(() => 0); // -> y = Result.Err(0)
|
|
168
180
|
*/
|
|
169
181
|
ok_or_else<E>(err: () => E): Result<T, E>;
|
|
182
|
+
/**
|
|
183
|
+
* Same as `Option.ok_or_else()` but async.
|
|
184
|
+
*/
|
|
185
|
+
ok_or_else_async<E>(err: () => Promise<E>): Promise<Result<T, E>>;
|
|
170
186
|
/**
|
|
171
187
|
*
|
|
172
188
|
* @param optb value.
|
|
@@ -195,6 +211,10 @@ export declare class Option<T> {
|
|
|
195
211
|
* const z = x.and_then(y); // -> z = Option.None()
|
|
196
212
|
*/
|
|
197
213
|
and_then<U>(f: (val: T) => Option<U>): Option<U>;
|
|
214
|
+
/**
|
|
215
|
+
* Same as `Option.and_then()` but async.
|
|
216
|
+
*/
|
|
217
|
+
and_then_async<U>(f: (val: T) => Promise<Option<U>>): Promise<Option<U>>;
|
|
198
218
|
/**
|
|
199
219
|
*
|
|
200
220
|
* @param optb value.
|
|
@@ -223,6 +243,10 @@ export declare class Option<T> {
|
|
|
223
243
|
* const z = x.or_else(y); // -> z = Option.Some(100)
|
|
224
244
|
*/
|
|
225
245
|
or_else(f: () => Option<T>): Option<T>;
|
|
246
|
+
/**
|
|
247
|
+
* Same as `Option.or_else()` but async.
|
|
248
|
+
*/
|
|
249
|
+
or_else_async(f: () => Promise<Option<T>>): Promise<Option<T>>;
|
|
226
250
|
/**
|
|
227
251
|
* @param cases - An object containing the match arms
|
|
228
252
|
* @param cases.Some - Called with the value if Option is Some
|
|
@@ -293,6 +317,10 @@ export declare class Result<T, E> {
|
|
|
293
317
|
* x.is_ok_and((x) => x > 5); // -> false
|
|
294
318
|
*/
|
|
295
319
|
is_ok_and(f: (value: T) => boolean): boolean;
|
|
320
|
+
/**
|
|
321
|
+
* Same as `Result.is_ok_and()` but async.
|
|
322
|
+
*/
|
|
323
|
+
is_ok_and_async(f: (value: T) => Promise<boolean>): Promise<boolean>;
|
|
296
324
|
/**
|
|
297
325
|
* @returns `true` if the `Result` is `Err` and the error matches the predicate.
|
|
298
326
|
* @example
|
|
@@ -303,6 +331,10 @@ export declare class Result<T, E> {
|
|
|
303
331
|
* x.is_err_and((e) => e === "error"); // -> false
|
|
304
332
|
*/
|
|
305
333
|
is_err_and(f: (error: E) => boolean): boolean;
|
|
334
|
+
/**
|
|
335
|
+
* Same as `Result.is_ok_and()` but async.
|
|
336
|
+
*/
|
|
337
|
+
is_err_and_async(f: (error: E) => Promise<boolean>): Promise<boolean>;
|
|
306
338
|
/**
|
|
307
339
|
* @returns `Option.Some(v)` if `Ok(v)`, otherwise `Option.None()`.
|
|
308
340
|
* @example
|
|
@@ -334,6 +366,10 @@ export declare class Result<T, E> {
|
|
|
334
366
|
* const y = x.map((val) => val * 2); // -> y = Result.Err("error")
|
|
335
367
|
*/
|
|
336
368
|
map<U>(f: (val: T) => U): Result<U, E>;
|
|
369
|
+
/**
|
|
370
|
+
* Same as `Result.map()` but async.
|
|
371
|
+
*/
|
|
372
|
+
map_async<U>(f: (val: T) => Promise<U>): Promise<Result<U, E>>;
|
|
337
373
|
/**
|
|
338
374
|
* @param dft - default value.
|
|
339
375
|
* @param f - lambda.
|
|
@@ -369,6 +405,10 @@ export declare class Result<T, E> {
|
|
|
369
405
|
* const y = x.map_err((e) => e.length); // -> y = Result.Ok(10)
|
|
370
406
|
*/
|
|
371
407
|
map_err<F>(op: (err: E) => F): Result<T, F>;
|
|
408
|
+
/**
|
|
409
|
+
* Same as `Result.map_err()` but async.
|
|
410
|
+
*/
|
|
411
|
+
map_err_async<F>(op: (err: E) => Promise<F>): Promise<Result<T, F>>;
|
|
372
412
|
/**
|
|
373
413
|
* @returns the contained `Ok` value.
|
|
374
414
|
* @throws {Error} if the `Result` is `Err`.
|
|
@@ -461,6 +501,10 @@ export declare class Result<T, E> {
|
|
|
461
501
|
* const y = x.and_then((val) => Result.Ok(val * 2)); // -> y = Result.Err("error")
|
|
462
502
|
*/
|
|
463
503
|
and_then<U>(op: (val: T) => Result<U, E>): Result<U, E>;
|
|
504
|
+
/**
|
|
505
|
+
* Same as `Result.and_then()` but async.
|
|
506
|
+
*/
|
|
507
|
+
and_then_async<U>(op: (val: T) => Promise<Result<U, E>>): Promise<Result<U, E>>;
|
|
464
508
|
/**
|
|
465
509
|
* @param res - value.
|
|
466
510
|
* @returns `this` if `Ok`, otherwise returns `res`.
|
|
@@ -485,6 +529,10 @@ export declare class Result<T, E> {
|
|
|
485
529
|
* const y = x.or_else((e) => Result.Ok(0)); // -> y = Result.Ok(10)
|
|
486
530
|
*/
|
|
487
531
|
or_else<F>(op: (err: E) => Result<T, F>): Result<T, F>;
|
|
532
|
+
/**
|
|
533
|
+
* Same as `Result.or_else()` but async.
|
|
534
|
+
*/
|
|
535
|
+
or_else_async<F>(op: (err: E) => Promise<Result<T, F>>): Promise<Result<T, F>>;
|
|
488
536
|
/**
|
|
489
537
|
* @param cases - An object containing the match arms.
|
|
490
538
|
* @param cases.Ok - Called with the value if `Result` is `Ok`.
|
package/dist/index.js
CHANGED
|
@@ -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.
|
|
@@ -180,6 +198,15 @@ export class Option {
|
|
|
180
198
|
None: () => Option.None(),
|
|
181
199
|
});
|
|
182
200
|
}
|
|
201
|
+
/**
|
|
202
|
+
* Same as `Option.map()` but async.
|
|
203
|
+
*/
|
|
204
|
+
async map_async(f) {
|
|
205
|
+
return this.match({
|
|
206
|
+
Some: async (val) => Option.Some(await f(val)),
|
|
207
|
+
None: async () => Option.None(),
|
|
208
|
+
});
|
|
209
|
+
}
|
|
183
210
|
/**
|
|
184
211
|
*
|
|
185
212
|
* @param dft default value.
|
|
@@ -250,6 +277,15 @@ export class Option {
|
|
|
250
277
|
None: () => Result.Err(err()),
|
|
251
278
|
});
|
|
252
279
|
}
|
|
280
|
+
/**
|
|
281
|
+
* Same as `Option.ok_or_else()` but async.
|
|
282
|
+
*/
|
|
283
|
+
async ok_or_else_async(err) {
|
|
284
|
+
return this.match({
|
|
285
|
+
Some: async (val) => Result.Ok(val),
|
|
286
|
+
None: async () => Result.Err(await err()),
|
|
287
|
+
});
|
|
288
|
+
}
|
|
253
289
|
/**
|
|
254
290
|
*
|
|
255
291
|
* @param optb value.
|
|
@@ -288,6 +324,15 @@ export class Option {
|
|
|
288
324
|
None: () => Option.None(),
|
|
289
325
|
});
|
|
290
326
|
}
|
|
327
|
+
/**
|
|
328
|
+
* Same as `Option.and_then()` but async.
|
|
329
|
+
*/
|
|
330
|
+
async and_then_async(f) {
|
|
331
|
+
return this.match({
|
|
332
|
+
Some: f,
|
|
333
|
+
None: async () => Option.None(),
|
|
334
|
+
});
|
|
335
|
+
}
|
|
291
336
|
/**
|
|
292
337
|
*
|
|
293
338
|
* @param optb value.
|
|
@@ -326,6 +371,15 @@ export class Option {
|
|
|
326
371
|
None: f,
|
|
327
372
|
});
|
|
328
373
|
}
|
|
374
|
+
/**
|
|
375
|
+
* Same as `Option.or_else()` but async.
|
|
376
|
+
*/
|
|
377
|
+
async or_else_async(f) {
|
|
378
|
+
return this.match({
|
|
379
|
+
Some: async (_) => this,
|
|
380
|
+
None: f,
|
|
381
|
+
});
|
|
382
|
+
}
|
|
329
383
|
/**
|
|
330
384
|
* @param cases - An object containing the match arms
|
|
331
385
|
* @param cases.Some - Called with the value if Option is Some
|
|
@@ -413,6 +467,15 @@ export class Result {
|
|
|
413
467
|
Err: (_) => false,
|
|
414
468
|
});
|
|
415
469
|
}
|
|
470
|
+
/**
|
|
471
|
+
* Same as `Result.is_ok_and()` but async.
|
|
472
|
+
*/
|
|
473
|
+
async is_ok_and_async(f) {
|
|
474
|
+
return this.match({
|
|
475
|
+
Ok: f,
|
|
476
|
+
Err: async (_) => false,
|
|
477
|
+
});
|
|
478
|
+
}
|
|
416
479
|
/**
|
|
417
480
|
* @returns `true` if the `Result` is `Err` and the error matches the predicate.
|
|
418
481
|
* @example
|
|
@@ -428,6 +491,15 @@ export class Result {
|
|
|
428
491
|
Err: f,
|
|
429
492
|
});
|
|
430
493
|
}
|
|
494
|
+
/**
|
|
495
|
+
* Same as `Result.is_ok_and()` but async.
|
|
496
|
+
*/
|
|
497
|
+
async is_err_and_async(f) {
|
|
498
|
+
return this.match({
|
|
499
|
+
Ok: async (_) => false,
|
|
500
|
+
Err: f,
|
|
501
|
+
});
|
|
502
|
+
}
|
|
431
503
|
/**
|
|
432
504
|
* @returns `Option.Some(v)` if `Ok(v)`, otherwise `Option.None()`.
|
|
433
505
|
* @example
|
|
@@ -474,6 +546,15 @@ export class Result {
|
|
|
474
546
|
Err: (e) => Result.Err(e),
|
|
475
547
|
});
|
|
476
548
|
}
|
|
549
|
+
/**
|
|
550
|
+
* Same as `Result.map()` but async.
|
|
551
|
+
*/
|
|
552
|
+
async map_async(f) {
|
|
553
|
+
return this.match({
|
|
554
|
+
Ok: async (t) => Result.Ok(await f(t)),
|
|
555
|
+
Err: async (e) => Result.Err(e),
|
|
556
|
+
});
|
|
557
|
+
}
|
|
477
558
|
/**
|
|
478
559
|
* @param dft - default value.
|
|
479
560
|
* @param f - lambda.
|
|
@@ -524,6 +605,15 @@ export class Result {
|
|
|
524
605
|
Err: (e) => Result.Err(op(e)),
|
|
525
606
|
});
|
|
526
607
|
}
|
|
608
|
+
/**
|
|
609
|
+
* Same as `Result.map_err()` but async.
|
|
610
|
+
*/
|
|
611
|
+
async map_err_async(op) {
|
|
612
|
+
return this.match({
|
|
613
|
+
Ok: async (val) => Result.Ok(val),
|
|
614
|
+
Err: async (e) => Result.Err(await op(e)),
|
|
615
|
+
});
|
|
616
|
+
}
|
|
527
617
|
/**
|
|
528
618
|
* @returns the contained `Ok` value.
|
|
529
619
|
* @throws {Error} if the `Result` is `Err`.
|
|
@@ -664,6 +754,15 @@ export class Result {
|
|
|
664
754
|
Err: (e) => Result.Err(e),
|
|
665
755
|
});
|
|
666
756
|
}
|
|
757
|
+
/**
|
|
758
|
+
* Same as `Result.and_then()` but async.
|
|
759
|
+
*/
|
|
760
|
+
async and_then_async(op) {
|
|
761
|
+
return this.match({
|
|
762
|
+
Ok: op,
|
|
763
|
+
Err: async (e) => Result.Err(e),
|
|
764
|
+
});
|
|
765
|
+
}
|
|
667
766
|
/**
|
|
668
767
|
* @param res - value.
|
|
669
768
|
* @returns `this` if `Ok`, otherwise returns `res`.
|
|
@@ -698,6 +797,15 @@ export class Result {
|
|
|
698
797
|
Err: op,
|
|
699
798
|
});
|
|
700
799
|
}
|
|
800
|
+
/**
|
|
801
|
+
* Same as `Result.or_else()` but async.
|
|
802
|
+
*/
|
|
803
|
+
async or_else_async(op) {
|
|
804
|
+
return this.match({
|
|
805
|
+
Ok: async (t) => Result.Ok(t),
|
|
806
|
+
Err: op,
|
|
807
|
+
});
|
|
808
|
+
}
|
|
701
809
|
/**
|
|
702
810
|
* @param cases - An object containing the match arms.
|
|
703
811
|
* @param cases.Ok - Called with the value if `Result` is `Ok`.
|