happy-rusty 1.3.2 → 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/main.cjs +2 -0
- package/dist/main.cjs.map +1 -1
- package/dist/main.mjs +2 -1
- package/dist/main.mjs.map +1 -1
- package/dist/types.d.ts +29 -1
- package/docs/README.md +5 -0
- package/docs/functions/Err.md +1 -1
- package/docs/functions/Ok.md +30 -6
- package/docs/functions/Some.md +1 -1
- package/docs/functions/isOption.md +1 -1
- package/docs/functions/isResult.md +1 -1
- package/docs/functions/promiseToAsyncResult.md +1 -1
- package/docs/interfaces/None.md +28 -28
- package/docs/interfaces/Option.md +27 -27
- package/docs/interfaces/Result.md +29 -29
- package/docs/type-aliases/AsyncIOResult.md +1 -1
- package/docs/type-aliases/AsyncOption.md +1 -1
- package/docs/type-aliases/AsyncResult.md +1 -1
- package/docs/type-aliases/AsyncVoidIOResult.md +17 -0
- package/docs/type-aliases/AsyncVoidResult.md +23 -0
- package/docs/type-aliases/IOResult.md +1 -1
- package/docs/type-aliases/VoidIOResult.md +17 -0
- package/docs/type-aliases/VoidResult.md +23 -0
- package/docs/variables/None.md +1 -1
- package/docs/variables/RESULT_FALSE.md +1 -1
- package/docs/variables/RESULT_TRUE.md +1 -1
- package/docs/variables/RESULT_VOID.md +17 -0
- package/docs/variables/RESULT_ZERO.md +1 -1
- package/package.json +1 -1
- package/src/enum/constants.ts +6 -1
- package/src/enum/defines.ts +25 -1
- package/src/enum/prelude.ts +18 -13
|
@@ -29,7 +29,7 @@ pub enum Result<T, E> {
|
|
|
29
29
|
|
|
30
30
|
| Property | Type | Description | Defined in |
|
|
31
31
|
| ------ | ------ | ------ | ------ |
|
|
32
|
-
| `[toStringTag]` | `"Result"` | [object Result]. | [core.ts:304](https://github.com/JiangJie/happy-rusty/blob/
|
|
32
|
+
| `[toStringTag]` | `"Result"` | [object Result]. | [core.ts:304](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L304) |
|
|
33
33
|
|
|
34
34
|
## Methods
|
|
35
35
|
|
|
@@ -61,7 +61,7 @@ The passed `Result` if `this` is `Ok`, otherwise returns `this` (which is `Err`)
|
|
|
61
61
|
|
|
62
62
|
#### Defined in
|
|
63
63
|
|
|
64
|
-
[core.ts:489](https://github.com/JiangJie/happy-rusty/blob/
|
|
64
|
+
[core.ts:489](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L489)
|
|
65
65
|
|
|
66
66
|
***
|
|
67
67
|
|
|
@@ -93,7 +93,7 @@ The result of `fn` if `this` is `Ok`, otherwise `this` as `Err`.
|
|
|
93
93
|
|
|
94
94
|
#### Defined in
|
|
95
95
|
|
|
96
|
-
[core.ts:505](https://github.com/JiangJie/happy-rusty/blob/
|
|
96
|
+
[core.ts:505](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L505)
|
|
97
97
|
|
|
98
98
|
***
|
|
99
99
|
|
|
@@ -122,7 +122,7 @@ Just same as `result as unknown as Result<U, E>`.
|
|
|
122
122
|
|
|
123
123
|
#### Defined in
|
|
124
124
|
|
|
125
|
-
[core.ts:563](https://github.com/JiangJie/happy-rusty/blob/
|
|
125
|
+
[core.ts:563](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L563)
|
|
126
126
|
|
|
127
127
|
***
|
|
128
128
|
|
|
@@ -150,7 +150,7 @@ Just same as `result as unknown as Result<T, F>`.
|
|
|
150
150
|
|
|
151
151
|
#### Defined in
|
|
152
152
|
|
|
153
|
-
[core.ts:552](https://github.com/JiangJie/happy-rusty/blob/
|
|
153
|
+
[core.ts:552](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L552)
|
|
154
154
|
|
|
155
155
|
***
|
|
156
156
|
|
|
@@ -176,7 +176,7 @@ Tests whether `this` and `other` are both `Ok` containing equal values, or both
|
|
|
176
176
|
|
|
177
177
|
#### Defined in
|
|
178
178
|
|
|
179
|
-
[core.ts:540](https://github.com/JiangJie/happy-rusty/blob/
|
|
179
|
+
[core.ts:540](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L540)
|
|
180
180
|
|
|
181
181
|
***
|
|
182
182
|
|
|
@@ -196,7 +196,7 @@ If the result is `Ok`, returns `None`.
|
|
|
196
196
|
|
|
197
197
|
#### Defined in
|
|
198
198
|
|
|
199
|
-
[core.ts:413](https://github.com/JiangJie/happy-rusty/blob/
|
|
199
|
+
[core.ts:413](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L413)
|
|
200
200
|
|
|
201
201
|
***
|
|
202
202
|
|
|
@@ -224,7 +224,7 @@ Throws an error with the provided message if the result is an `Err`.
|
|
|
224
224
|
|
|
225
225
|
#### Defined in
|
|
226
226
|
|
|
227
|
-
[core.ts:356](https://github.com/JiangJie/happy-rusty/blob/
|
|
227
|
+
[core.ts:356](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L356)
|
|
228
228
|
|
|
229
229
|
***
|
|
230
230
|
|
|
@@ -252,7 +252,7 @@ Throws an error with the provided message if the result is an `Ok`.
|
|
|
252
252
|
|
|
253
253
|
#### Defined in
|
|
254
254
|
|
|
255
|
-
[core.ts:385](https://github.com/JiangJie/happy-rusty/blob/
|
|
255
|
+
[core.ts:385](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L385)
|
|
256
256
|
|
|
257
257
|
***
|
|
258
258
|
|
|
@@ -284,7 +284,7 @@ If the result is `Ok(Err(E))` or `Err(E)`, returns `Err(E)`.
|
|
|
284
284
|
|
|
285
285
|
#### Defined in
|
|
286
286
|
|
|
287
|
-
[core.ts:473](https://github.com/JiangJie/happy-rusty/blob/
|
|
287
|
+
[core.ts:473](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L473)
|
|
288
288
|
|
|
289
289
|
***
|
|
290
290
|
|
|
@@ -311,7 +311,7 @@ Does not modify the `Result`.
|
|
|
311
311
|
|
|
312
312
|
#### Defined in
|
|
313
313
|
|
|
314
|
-
[core.ts:523](https://github.com/JiangJie/happy-rusty/blob/
|
|
314
|
+
[core.ts:523](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L523)
|
|
315
315
|
|
|
316
316
|
***
|
|
317
317
|
|
|
@@ -338,7 +338,7 @@ Does not modify the `Result`.
|
|
|
338
338
|
|
|
339
339
|
#### Defined in
|
|
340
340
|
|
|
341
|
-
[core.ts:531](https://github.com/JiangJie/happy-rusty/blob/
|
|
341
|
+
[core.ts:531](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L531)
|
|
342
342
|
|
|
343
343
|
***
|
|
344
344
|
|
|
@@ -356,7 +356,7 @@ Returns `true` if the result is `Err`.
|
|
|
356
356
|
|
|
357
357
|
#### Defined in
|
|
358
358
|
|
|
359
|
-
[core.ts:329](https://github.com/JiangJie/happy-rusty/blob/
|
|
359
|
+
[core.ts:329](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L329)
|
|
360
360
|
|
|
361
361
|
***
|
|
362
362
|
|
|
@@ -380,7 +380,7 @@ Returns `true` if the result is `Err` and the provided predicate returns `true`
|
|
|
380
380
|
|
|
381
381
|
#### Defined in
|
|
382
382
|
|
|
383
|
-
[core.ts:341](https://github.com/JiangJie/happy-rusty/blob/
|
|
383
|
+
[core.ts:341](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L341)
|
|
384
384
|
|
|
385
385
|
***
|
|
386
386
|
|
|
@@ -398,7 +398,7 @@ Returns `true` if the result is `Ok`.
|
|
|
398
398
|
|
|
399
399
|
#### Defined in
|
|
400
400
|
|
|
401
|
-
[core.ts:324](https://github.com/JiangJie/happy-rusty/blob/
|
|
401
|
+
[core.ts:324](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L324)
|
|
402
402
|
|
|
403
403
|
***
|
|
404
404
|
|
|
@@ -422,7 +422,7 @@ Returns `true` if the result is `Ok` and the provided predicate returns `true` f
|
|
|
422
422
|
|
|
423
423
|
#### Defined in
|
|
424
424
|
|
|
425
|
-
[core.ts:335](https://github.com/JiangJie/happy-rusty/blob/
|
|
425
|
+
[core.ts:335](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L335)
|
|
426
426
|
|
|
427
427
|
***
|
|
428
428
|
|
|
@@ -453,7 +453,7 @@ leaving an `Err` value untouched.
|
|
|
453
453
|
|
|
454
454
|
#### Defined in
|
|
455
455
|
|
|
456
|
-
[core.ts:434](https://github.com/JiangJie/happy-rusty/blob/
|
|
456
|
+
[core.ts:434](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L434)
|
|
457
457
|
|
|
458
458
|
***
|
|
459
459
|
|
|
@@ -484,7 +484,7 @@ leaving an `Ok` value untouched.
|
|
|
484
484
|
|
|
485
485
|
#### Defined in
|
|
486
486
|
|
|
487
|
-
[core.ts:446](https://github.com/JiangJie/happy-rusty/blob/
|
|
487
|
+
[core.ts:446](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L446)
|
|
488
488
|
|
|
489
489
|
***
|
|
490
490
|
|
|
@@ -515,7 +515,7 @@ Maps a `Result<T, E>` to `U` by applying a function to the contained `Ok` value
|
|
|
515
515
|
|
|
516
516
|
#### Defined in
|
|
517
517
|
|
|
518
|
-
[core.ts:458](https://github.com/JiangJie/happy-rusty/blob/
|
|
518
|
+
[core.ts:458](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L458)
|
|
519
519
|
|
|
520
520
|
***
|
|
521
521
|
|
|
@@ -546,7 +546,7 @@ Maps a `Result<T, E>` to `U` by applying a function to the contained `Ok` value
|
|
|
546
546
|
|
|
547
547
|
#### Defined in
|
|
548
548
|
|
|
549
|
-
[core.ts:466](https://github.com/JiangJie/happy-rusty/blob/
|
|
549
|
+
[core.ts:466](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L466)
|
|
550
550
|
|
|
551
551
|
***
|
|
552
552
|
|
|
@@ -566,7 +566,7 @@ If the result is `Err`, returns `None`.
|
|
|
566
566
|
|
|
567
567
|
#### Defined in
|
|
568
568
|
|
|
569
|
-
[core.ts:406](https://github.com/JiangJie/happy-rusty/blob/
|
|
569
|
+
[core.ts:406](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L406)
|
|
570
570
|
|
|
571
571
|
***
|
|
572
572
|
|
|
@@ -598,7 +598,7 @@ Returns `this` if it is `Ok`, otherwise returns the passed `Result`.
|
|
|
598
598
|
|
|
599
599
|
#### Defined in
|
|
600
600
|
|
|
601
|
-
[core.ts:497](https://github.com/JiangJie/happy-rusty/blob/
|
|
601
|
+
[core.ts:497](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L497)
|
|
602
602
|
|
|
603
603
|
***
|
|
604
604
|
|
|
@@ -630,7 +630,7 @@ The result of `fn` if `this` is `Err`, otherwise `this` as `Ok`.
|
|
|
630
630
|
|
|
631
631
|
#### Defined in
|
|
632
632
|
|
|
633
|
-
[core.ts:513](https://github.com/JiangJie/happy-rusty/blob/
|
|
633
|
+
[core.ts:513](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L513)
|
|
634
634
|
|
|
635
635
|
***
|
|
636
636
|
|
|
@@ -648,7 +648,7 @@ Custom `toString` implementation that uses the `Result`'s contained value.
|
|
|
648
648
|
|
|
649
649
|
#### Defined in
|
|
650
650
|
|
|
651
|
-
[core.ts:568](https://github.com/JiangJie/happy-rusty/blob/
|
|
651
|
+
[core.ts:568](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L568)
|
|
652
652
|
|
|
653
653
|
***
|
|
654
654
|
|
|
@@ -682,7 +682,7 @@ Transposes a `Result` of an `Option` into an `Option` of a `Result`.
|
|
|
682
682
|
|
|
683
683
|
#### Defined in
|
|
684
684
|
|
|
685
|
-
[core.ts:422](https://github.com/JiangJie/happy-rusty/blob/
|
|
685
|
+
[core.ts:422](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L422)
|
|
686
686
|
|
|
687
687
|
***
|
|
688
688
|
|
|
@@ -704,7 +704,7 @@ Throws an error if the result is an `Err`.
|
|
|
704
704
|
|
|
705
705
|
#### Defined in
|
|
706
706
|
|
|
707
|
-
[core.ts:362](https://github.com/JiangJie/happy-rusty/blob/
|
|
707
|
+
[core.ts:362](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L362)
|
|
708
708
|
|
|
709
709
|
***
|
|
710
710
|
|
|
@@ -726,7 +726,7 @@ Throws an error if the result is an `Ok`.
|
|
|
726
726
|
|
|
727
727
|
#### Defined in
|
|
728
728
|
|
|
729
|
-
[core.ts:391](https://github.com/JiangJie/happy-rusty/blob/
|
|
729
|
+
[core.ts:391](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L391)
|
|
730
730
|
|
|
731
731
|
***
|
|
732
732
|
|
|
@@ -750,7 +750,7 @@ Returns the contained `Ok` value or a provided default.
|
|
|
750
750
|
|
|
751
751
|
#### Defined in
|
|
752
752
|
|
|
753
|
-
[core.ts:368](https://github.com/JiangJie/happy-rusty/blob/
|
|
753
|
+
[core.ts:368](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L368)
|
|
754
754
|
|
|
755
755
|
***
|
|
756
756
|
|
|
@@ -774,4 +774,4 @@ Returns the contained `Ok` value or computes it from a closure if the result is
|
|
|
774
774
|
|
|
775
775
|
#### Defined in
|
|
776
776
|
|
|
777
|
-
[core.ts:374](https://github.com/JiangJie/happy-rusty/blob/
|
|
777
|
+
[core.ts:374](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/core.ts#L374)
|
|
@@ -21,4 +21,4 @@ This is a promise that resolves to `Ok(T)` if the I/O operation was successful,
|
|
|
21
21
|
|
|
22
22
|
## Defined in
|
|
23
23
|
|
|
24
|
-
[defines.ts:
|
|
24
|
+
[defines.ts:52](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/defines.ts#L52)
|
|
@@ -21,4 +21,4 @@ This is a promise that resolves to either `Some(T)` if the value is present, or
|
|
|
21
21
|
|
|
22
22
|
## Defined in
|
|
23
23
|
|
|
24
|
-
[defines.ts:13](https://github.com/JiangJie/happy-rusty/blob/
|
|
24
|
+
[defines.ts:13](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/defines.ts#L13)
|
|
@@ -22,4 +22,4 @@ This is a promise that resolves to `Ok(T)` if the operation was successful, or `
|
|
|
22
22
|
|
|
23
23
|
## Defined in
|
|
24
24
|
|
|
25
|
-
[defines.ts:22](https://github.com/JiangJie/happy-rusty/blob/
|
|
25
|
+
[defines.ts:22](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/defines.ts#L22)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[**happy-rusty**](../README.md) • **Docs**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[happy-rusty](../README.md) / AsyncVoidIOResult
|
|
6
|
+
|
|
7
|
+
# Type Alias: AsyncVoidIOResult
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
type AsyncVoidIOResult: AsyncIOResult<void>;
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`VoidIOResult` wrapped by `Promise`.
|
|
14
|
+
|
|
15
|
+
## Defined in
|
|
16
|
+
|
|
17
|
+
[defines.ts:62](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/defines.ts#L62)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[**happy-rusty**](../README.md) • **Docs**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[happy-rusty](../README.md) / AsyncVoidResult
|
|
6
|
+
|
|
7
|
+
# Type Alias: AsyncVoidResult\<E\>
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
type AsyncVoidResult<E>: Promise<VoidResult<E>>;
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`VoidResult<E>` wrapped by `Promise`.
|
|
14
|
+
|
|
15
|
+
## Type Parameters
|
|
16
|
+
|
|
17
|
+
| Type Parameter | Description |
|
|
18
|
+
| ------ | ------ |
|
|
19
|
+
| `E` | The type of the error that may be produced by a failed operation. |
|
|
20
|
+
|
|
21
|
+
## Defined in
|
|
22
|
+
|
|
23
|
+
[defines.ts:36](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/defines.ts#L36)
|
|
@@ -21,4 +21,4 @@ This is a result that is either `Ok(T)` if the operation was successful, or `Err
|
|
|
21
21
|
|
|
22
22
|
## Defined in
|
|
23
23
|
|
|
24
|
-
[defines.ts:
|
|
24
|
+
[defines.ts:44](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/defines.ts#L44)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[**happy-rusty**](../README.md) • **Docs**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[happy-rusty](../README.md) / VoidIOResult
|
|
6
|
+
|
|
7
|
+
# Type Alias: VoidIOResult
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
type VoidIOResult: IOResult<void>;
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Similar to Rust's `Result<(), Error>`.
|
|
14
|
+
|
|
15
|
+
## Defined in
|
|
16
|
+
|
|
17
|
+
[defines.ts:57](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/defines.ts#L57)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[**happy-rusty**](../README.md) • **Docs**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[happy-rusty](../README.md) / VoidResult
|
|
6
|
+
|
|
7
|
+
# Type Alias: VoidResult\<E\>
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
type VoidResult<E>: Result<void, E>;
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Similar to Rust's `Result<(), E>`.
|
|
14
|
+
|
|
15
|
+
## Type Parameters
|
|
16
|
+
|
|
17
|
+
| Type Parameter | Description |
|
|
18
|
+
| ------ | ------ |
|
|
19
|
+
| `E` | The type of the error that may be produced by a failed operation. |
|
|
20
|
+
|
|
21
|
+
## Defined in
|
|
22
|
+
|
|
23
|
+
[defines.ts:29](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/defines.ts#L29)
|
package/docs/variables/None.md
CHANGED
|
@@ -15,4 +15,4 @@ This constant is frozen to ensure it is immutable and cannot be altered, preserv
|
|
|
15
15
|
|
|
16
16
|
## Defined in
|
|
17
17
|
|
|
18
|
-
[prelude.ts:10](https://github.com/JiangJie/happy-rusty/blob/
|
|
18
|
+
[prelude.ts:10](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/prelude.ts#L10)
|
|
@@ -15,4 +15,4 @@ Can be used anywhere due to immutability.
|
|
|
15
15
|
|
|
16
16
|
## Defined in
|
|
17
17
|
|
|
18
|
-
[constants.ts:19](https://github.com/JiangJie/happy-rusty/blob/
|
|
18
|
+
[constants.ts:19](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/constants.ts#L19)
|
|
@@ -15,4 +15,4 @@ Can be used anywhere due to immutability.
|
|
|
15
15
|
|
|
16
16
|
## Defined in
|
|
17
17
|
|
|
18
|
-
[constants.ts:13](https://github.com/JiangJie/happy-rusty/blob/
|
|
18
|
+
[constants.ts:13](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/constants.ts#L13)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[**happy-rusty**](../README.md) • **Docs**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[happy-rusty](../README.md) / RESULT\_VOID
|
|
6
|
+
|
|
7
|
+
# Variable: RESULT\_VOID
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const RESULT_VOID: Result<void, any>;
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Result constant for `void` or `()`.
|
|
14
|
+
|
|
15
|
+
## Defined in
|
|
16
|
+
|
|
17
|
+
[constants.ts:30](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/constants.ts#L30)
|
|
@@ -15,4 +15,4 @@ Can be used anywhere due to immutability.
|
|
|
15
15
|
|
|
16
16
|
## Defined in
|
|
17
17
|
|
|
18
|
-
[constants.ts:25](https://github.com/JiangJie/happy-rusty/blob/
|
|
18
|
+
[constants.ts:25](https://github.com/JiangJie/happy-rusty/blob/7d7f4ab2132e507f77594d030495f95b5688b84a/src/enum/constants.ts#L25)
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Porting some excellent design implementations from Rust to JavaScript.",
|
|
4
4
|
"author": "jiang115jie@gmail.com",
|
|
5
5
|
"license": "GPL-3.0",
|
|
6
|
-
"version": "1.
|
|
6
|
+
"version": "1.4.0",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"source": "src/mod.ts",
|
|
9
9
|
"main": "dist/main.cjs",
|
package/src/enum/constants.ts
CHANGED
|
@@ -22,4 +22,9 @@ export const RESULT_FALSE: Result<boolean, any> = Ok(false);
|
|
|
22
22
|
* Result constant for `0`.
|
|
23
23
|
* Can be used anywhere due to immutability.
|
|
24
24
|
*/
|
|
25
|
-
export const RESULT_ZERO: Result<number, any> = Ok(0);
|
|
25
|
+
export const RESULT_ZERO: Result<number, any> = Ok(0);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Result constant for `void` or `()`.
|
|
29
|
+
*/
|
|
30
|
+
export const RESULT_VOID: Result<void, any> = Ok();
|
package/src/enum/defines.ts
CHANGED
|
@@ -21,6 +21,20 @@ export type AsyncOption<T> = Promise<Option<T>>;
|
|
|
21
21
|
*/
|
|
22
22
|
export type AsyncResult<T, E> = Promise<Result<T, E>>;
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Similar to Rust's `Result<(), E>`.
|
|
26
|
+
*
|
|
27
|
+
* @typeParam E - The type of the error that may be produced by a failed operation.
|
|
28
|
+
*/
|
|
29
|
+
export type VoidResult<E> = Result<void, E>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* `VoidResult<E>` wrapped by `Promise`.
|
|
33
|
+
*
|
|
34
|
+
* @typeParam E - The type of the error that may be produced by a failed operation.
|
|
35
|
+
*/
|
|
36
|
+
export type AsyncVoidResult<E> = Promise<VoidResult<E>>;
|
|
37
|
+
|
|
24
38
|
/**
|
|
25
39
|
* Represents a synchronous operation that yields a `Result<T, Error>`.
|
|
26
40
|
* This is a result that is either `Ok(T)` if the operation was successful, or `Err(Error)` if there was an error.
|
|
@@ -35,4 +49,14 @@ export type IOResult<T> = Result<T, Error>;
|
|
|
35
49
|
*
|
|
36
50
|
* @typeParam T - The type of the value that is produced by a successful I/O operation.
|
|
37
51
|
*/
|
|
38
|
-
export type AsyncIOResult<T> = AsyncResult<T, Error>;
|
|
52
|
+
export type AsyncIOResult<T> = AsyncResult<T, Error>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Similar to Rust's `Result<(), Error>`.
|
|
56
|
+
*/
|
|
57
|
+
export type VoidIOResult = IOResult<void>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* `VoidIOResult` wrapped by `Promise`.
|
|
61
|
+
*/
|
|
62
|
+
export type AsyncVoidIOResult = AsyncIOResult<void>;
|
package/src/enum/prelude.ts
CHANGED
|
@@ -283,7 +283,12 @@ export const None = Object.freeze<None>({
|
|
|
283
283
|
* }
|
|
284
284
|
* ```
|
|
285
285
|
*/
|
|
286
|
-
export function Ok<T, E>(value: T): Result<T, E
|
|
286
|
+
export function Ok<T, E>(value: T): Result<T, E>;
|
|
287
|
+
/**
|
|
288
|
+
* Because javascript does not have a `()` type, use `void` instead.
|
|
289
|
+
*/
|
|
290
|
+
export function Ok<E>(): Result<void, E>;
|
|
291
|
+
export function Ok<T, E>(value?: T): Result<T, E> {
|
|
287
292
|
const ok: Result<T, E> = {
|
|
288
293
|
[Symbol.toStringTag]: 'Result',
|
|
289
294
|
[ResultKindSymbol]: 'Ok',
|
|
@@ -295,23 +300,23 @@ export function Ok<T, E>(value: T): Result<T, E> {
|
|
|
295
300
|
return false;
|
|
296
301
|
},
|
|
297
302
|
isOkAnd(predicate: (value: T) => boolean): boolean {
|
|
298
|
-
return predicate(value);
|
|
303
|
+
return predicate(value as T);
|
|
299
304
|
},
|
|
300
305
|
isErrAnd(_predicate: (error: E) => boolean): false {
|
|
301
306
|
return false;
|
|
302
307
|
},
|
|
303
308
|
|
|
304
309
|
expect(_msg: string): T {
|
|
305
|
-
return value;
|
|
310
|
+
return value as T;
|
|
306
311
|
},
|
|
307
312
|
unwrap(): T {
|
|
308
|
-
return value;
|
|
313
|
+
return value as T;
|
|
309
314
|
},
|
|
310
315
|
unwrapOr(_defaultValue: T): T {
|
|
311
|
-
return value;
|
|
316
|
+
return value as T;
|
|
312
317
|
},
|
|
313
318
|
unwrapOrElse(_fn: (error: E) => T): T {
|
|
314
|
-
return value;
|
|
319
|
+
return value as T;
|
|
315
320
|
},
|
|
316
321
|
|
|
317
322
|
expectErr(msg: string): E {
|
|
@@ -322,7 +327,7 @@ export function Ok<T, E>(value: T): Result<T, E> {
|
|
|
322
327
|
},
|
|
323
328
|
|
|
324
329
|
ok(): Option<T> {
|
|
325
|
-
return Some(value);
|
|
330
|
+
return Some(value as T);
|
|
326
331
|
},
|
|
327
332
|
err(): None {
|
|
328
333
|
return None;
|
|
@@ -334,16 +339,16 @@ export function Ok<T, E>(value: T): Result<T, E> {
|
|
|
334
339
|
},
|
|
335
340
|
|
|
336
341
|
map<U>(fn: (value: T) => U): Result<U, E> {
|
|
337
|
-
return Ok(fn(value));
|
|
342
|
+
return Ok(fn(value as T));
|
|
338
343
|
},
|
|
339
344
|
mapErr<F>(_fn: (error: E) => F): Result<T, F> {
|
|
340
|
-
return Ok(value);
|
|
345
|
+
return Ok(value as T);
|
|
341
346
|
},
|
|
342
347
|
mapOr<U>(_defaultValue: U, fn: (value: T) => U): U {
|
|
343
|
-
return fn(value);
|
|
348
|
+
return fn(value as T);
|
|
344
349
|
},
|
|
345
350
|
mapOrElse<U>(_defaultFn: (error: E) => U, fn: (value: T) => U): U {
|
|
346
|
-
return fn(value);
|
|
351
|
+
return fn(value as T);
|
|
347
352
|
},
|
|
348
353
|
flatten<T>(): Result<T, E> {
|
|
349
354
|
const r = value as Result<T, E>;
|
|
@@ -359,14 +364,14 @@ export function Ok<T, E>(value: T): Result<T, E> {
|
|
|
359
364
|
return ok as unknown as Result<T, F>;
|
|
360
365
|
},
|
|
361
366
|
andThen<U>(fn: (value: T) => Result<U, E>): Result<U, E> {
|
|
362
|
-
return fn(value);
|
|
367
|
+
return fn(value as T);
|
|
363
368
|
},
|
|
364
369
|
orElse<F>(_fn: (error: E) => Result<T, F>): Result<T, F> {
|
|
365
370
|
return ok as unknown as Result<T, F>;
|
|
366
371
|
},
|
|
367
372
|
|
|
368
373
|
inspect(fn: (value: T) => void): Result<T, E> {
|
|
369
|
-
fn(value);
|
|
374
|
+
fn(value as T);
|
|
370
375
|
return ok;
|
|
371
376
|
},
|
|
372
377
|
inspectErr(_fn: (error: E) => void): Result<T, E> {
|