happy-rusty 1.1.1 → 1.1.2

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.
@@ -55,7 +55,7 @@ The passed `Result` if `this` is `Ok`, otherwise returns `this` (which is `Err`)
55
55
 
56
56
  #### Defined in
57
57
 
58
- [prelude.ts:516](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L516)
58
+ [prelude.ts:516](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L516)
59
59
 
60
60
  ***
61
61
 
@@ -87,7 +87,64 @@ The result of `fn` if `this` is `Ok`, otherwise `this` as `Err`.
87
87
 
88
88
  #### Defined in
89
89
 
90
- [prelude.ts:532](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L532)
90
+ [prelude.ts:532](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L532)
91
+
92
+ ***
93
+
94
+ ### asErr()
95
+
96
+ ```ts
97
+ asErr<U>(): Result<U, E>
98
+ ```
99
+
100
+ Transforms the current Result into a new Result where the type of the success result is replaced with a new type `U`.
101
+ The type of the error result remains unchanged.
102
+ Useful where you need to return an Error chained to another type.
103
+ Just same as `result as unknown as Result<U, E>`.
104
+
105
+ #### Type Parameters
106
+
107
+ | Type Parameter | Description |
108
+ | ------ | ------ |
109
+ | `U` | The new type for the success result. |
110
+
111
+ #### Returns
112
+
113
+ [`Result`](Result.md)\<`U`, `E`\>
114
+
115
+ `this` but the success result type is `U`.
116
+
117
+ #### Defined in
118
+
119
+ [prelude.ts:590](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L590)
120
+
121
+ ***
122
+
123
+ ### asOk()
124
+
125
+ ```ts
126
+ asOk<F>(): Result<T, F>
127
+ ```
128
+
129
+ Transforms the current Result into a new Result where the type of the error result is replaced with a new type `F`.
130
+ The type of the success result remains unchanged.
131
+ Just same as `result as unknown as Result<T, F>`.
132
+
133
+ #### Type Parameters
134
+
135
+ | Type Parameter | Description |
136
+ | ------ | ------ |
137
+ | `F` | The new type for the error result. |
138
+
139
+ #### Returns
140
+
141
+ [`Result`](Result.md)\<`T`, `F`\>
142
+
143
+ `this` but the error result type is `F`.
144
+
145
+ #### Defined in
146
+
147
+ [prelude.ts:579](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L579)
91
148
 
92
149
  ***
93
150
 
@@ -113,7 +170,7 @@ Tests whether `this` and `other` are both `Ok` containing equal values, or both
113
170
 
114
171
  #### Defined in
115
172
 
116
- [prelude.ts:567](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L567)
173
+ [prelude.ts:567](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L567)
117
174
 
118
175
  ***
119
176
 
@@ -133,7 +190,7 @@ If the result is `Ok`, returns `None`.
133
190
 
134
191
  #### Defined in
135
192
 
136
- [prelude.ts:440](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L440)
193
+ [prelude.ts:440](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L440)
137
194
 
138
195
  ***
139
196
 
@@ -161,7 +218,7 @@ Throws an error with the provided message if the result is an `Err`.
161
218
 
162
219
  #### Defined in
163
220
 
164
- [prelude.ts:383](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L383)
221
+ [prelude.ts:383](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L383)
165
222
 
166
223
  ***
167
224
 
@@ -189,7 +246,7 @@ Throws an error with the provided message if the result is an `Ok`.
189
246
 
190
247
  #### Defined in
191
248
 
192
- [prelude.ts:412](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L412)
249
+ [prelude.ts:412](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L412)
193
250
 
194
251
  ***
195
252
 
@@ -221,7 +278,7 @@ If the result is `Ok(Err(E))` or `Err(E)`, returns `Err(E)`.
221
278
 
222
279
  #### Defined in
223
280
 
224
- [prelude.ts:500](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L500)
281
+ [prelude.ts:500](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L500)
225
282
 
226
283
  ***
227
284
 
@@ -248,7 +305,7 @@ Does not modify the `Result`.
248
305
 
249
306
  #### Defined in
250
307
 
251
- [prelude.ts:550](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L550)
308
+ [prelude.ts:550](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L550)
252
309
 
253
310
  ***
254
311
 
@@ -275,7 +332,7 @@ Does not modify the `Result`.
275
332
 
276
333
  #### Defined in
277
334
 
278
- [prelude.ts:558](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L558)
335
+ [prelude.ts:558](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L558)
279
336
 
280
337
  ***
281
338
 
@@ -293,7 +350,7 @@ Returns `true` if the result is `Err`.
293
350
 
294
351
  #### Defined in
295
352
 
296
- [prelude.ts:356](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L356)
353
+ [prelude.ts:356](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L356)
297
354
 
298
355
  ***
299
356
 
@@ -317,7 +374,7 @@ Returns `true` if the result is `Err` and the provided predicate returns `true`
317
374
 
318
375
  #### Defined in
319
376
 
320
- [prelude.ts:368](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L368)
377
+ [prelude.ts:368](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L368)
321
378
 
322
379
  ***
323
380
 
@@ -335,7 +392,7 @@ Returns `true` if the result is `Ok`.
335
392
 
336
393
  #### Defined in
337
394
 
338
- [prelude.ts:351](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L351)
395
+ [prelude.ts:351](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L351)
339
396
 
340
397
  ***
341
398
 
@@ -359,7 +416,7 @@ Returns `true` if the result is `Ok` and the provided predicate returns `true` f
359
416
 
360
417
  #### Defined in
361
418
 
362
- [prelude.ts:362](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L362)
419
+ [prelude.ts:362](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L362)
363
420
 
364
421
  ***
365
422
 
@@ -390,7 +447,7 @@ leaving an `Err` value untouched.
390
447
 
391
448
  #### Defined in
392
449
 
393
- [prelude.ts:461](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L461)
450
+ [prelude.ts:461](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L461)
394
451
 
395
452
  ***
396
453
 
@@ -421,7 +478,7 @@ leaving an `Ok` value untouched.
421
478
 
422
479
  #### Defined in
423
480
 
424
- [prelude.ts:473](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L473)
481
+ [prelude.ts:473](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L473)
425
482
 
426
483
  ***
427
484
 
@@ -452,7 +509,7 @@ Maps a `Result<T, E>` to `U` by applying a function to the contained `Ok` value
452
509
 
453
510
  #### Defined in
454
511
 
455
- [prelude.ts:485](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L485)
512
+ [prelude.ts:485](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L485)
456
513
 
457
514
  ***
458
515
 
@@ -483,7 +540,7 @@ Maps a `Result<T, E>` to `U` by applying a function to the contained `Ok` value
483
540
 
484
541
  #### Defined in
485
542
 
486
- [prelude.ts:493](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L493)
543
+ [prelude.ts:493](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L493)
487
544
 
488
545
  ***
489
546
 
@@ -503,7 +560,7 @@ If the result is `Err`, returns `None`.
503
560
 
504
561
  #### Defined in
505
562
 
506
- [prelude.ts:433](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L433)
563
+ [prelude.ts:433](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L433)
507
564
 
508
565
  ***
509
566
 
@@ -535,7 +592,7 @@ Returns `this` if it is `Ok`, otherwise returns the passed `Result`.
535
592
 
536
593
  #### Defined in
537
594
 
538
- [prelude.ts:524](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L524)
595
+ [prelude.ts:524](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L524)
539
596
 
540
597
  ***
541
598
 
@@ -567,7 +624,7 @@ The result of `fn` if `this` is `Err`, otherwise `this` as `Ok`.
567
624
 
568
625
  #### Defined in
569
626
 
570
- [prelude.ts:540](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L540)
627
+ [prelude.ts:540](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L540)
571
628
 
572
629
  ***
573
630
 
@@ -601,7 +658,7 @@ Transposes a `Result` of an `Option` into an `Option` of a `Result`.
601
658
 
602
659
  #### Defined in
603
660
 
604
- [prelude.ts:449](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L449)
661
+ [prelude.ts:449](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L449)
605
662
 
606
663
  ***
607
664
 
@@ -623,7 +680,7 @@ Throws an error if the result is an `Err`.
623
680
 
624
681
  #### Defined in
625
682
 
626
- [prelude.ts:389](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L389)
683
+ [prelude.ts:389](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L389)
627
684
 
628
685
  ***
629
686
 
@@ -645,7 +702,7 @@ Throws an error if the result is an `Ok`.
645
702
 
646
703
  #### Defined in
647
704
 
648
- [prelude.ts:418](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L418)
705
+ [prelude.ts:418](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L418)
649
706
 
650
707
  ***
651
708
 
@@ -669,7 +726,7 @@ Returns the contained `Ok` value or a provided default.
669
726
 
670
727
  #### Defined in
671
728
 
672
- [prelude.ts:395](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L395)
729
+ [prelude.ts:395](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L395)
673
730
 
674
731
  ***
675
732
 
@@ -693,4 +750,4 @@ Returns the contained `Ok` value or computes it from a closure if the result is
693
750
 
694
751
  #### Defined in
695
752
 
696
- [prelude.ts:401](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L401)
753
+ [prelude.ts:401](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L401)
@@ -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
- [prelude.ts:607](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L607)
24
+ [prelude.ts:628](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L628)
@@ -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
- [prelude.ts:582](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L582)
24
+ [prelude.ts:603](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L603)
@@ -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
- [prelude.ts:591](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L591)
25
+ [prelude.ts:612](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L612)
@@ -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
- [prelude.ts:599](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L599)
24
+ [prelude.ts:620](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L620)
@@ -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:286](https://github.com/JiangJie/happy-rusty/blob/82bfb94138be23b97750c830432d7e013c0e5b80/src/enum/prelude.ts#L286)
18
+ [prelude.ts:286](https://github.com/JiangJie/happy-rusty/blob/7218a182717eb5dbba4bfaf783977bc5e378815a/src/enum/prelude.ts#L286)
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.1.1",
6
+ "version": "1.1.2",
7
7
  "type": "module",
8
8
  "source": "src/mod.ts",
9
9
  "main": "dist/main.cjs",
@@ -47,8 +47,8 @@
47
47
  "Err"
48
48
  ],
49
49
  "devDependencies": {
50
- "@typescript-eslint/eslint-plugin": "^7.16.0",
51
- "@typescript-eslint/parser": "^7.16.0",
50
+ "@typescript-eslint/eslint-plugin": "^7.16.1",
51
+ "@typescript-eslint/parser": "^7.16.1",
52
52
  "eslint": "^8.57.0",
53
53
  "rollup": "^4.18.1",
54
54
  "rollup-plugin-dts": "^6.1.1",
@@ -567,6 +567,27 @@ export interface Result<T, E> {
567
567
  eq(other: Result<T, E>): boolean;
568
568
 
569
569
  // #endregion
570
+
571
+ /**
572
+ * Transforms the current Result into a new Result where the type of the error result is replaced with a new type `F`.
573
+ * The type of the success result remains unchanged.
574
+ * Just same as `result as unknown as Result<T, F>`.
575
+ *
576
+ * @typeParam F - The new type for the error result.
577
+ * @returns `this` but the error result type is `F`.
578
+ */
579
+ asOk<F>(): Result<T, F>;
580
+
581
+ /**
582
+ * Transforms the current Result into a new Result where the type of the success result is replaced with a new type `U`.
583
+ * The type of the error result remains unchanged.
584
+ * Useful where you need to return an Error chained to another type.
585
+ * Just same as `result as unknown as Result<U, E>`.
586
+ *
587
+ * @typeParam U - The new type for the success result.
588
+ * @returns `this` but the success result type is `U`.
589
+ */
590
+ asErr<U>(): Result<U, E>;
570
591
  }
571
592
 
572
593
  /**
@@ -828,6 +849,13 @@ export function Ok<T, E>(value: T): Result<T, E> {
828
849
  assertResult(other);
829
850
  return other.isOk() && other.unwrap() === value;
830
851
  },
852
+
853
+ asOk<F>(): Result<T, F> {
854
+ return ok as unknown as Result<T, F>;
855
+ },
856
+ asErr(): never {
857
+ throw new TypeError('Called `Result::asErr()` on an `Ok` value');
858
+ },
831
859
  } as const;
832
860
 
833
861
  return ok;
@@ -899,6 +927,13 @@ export function Err<T, E>(error: E): Result<T, E> {
899
927
  assertResult(other);
900
928
  return other.isErr() && other.unwrapErr() === error;
901
929
  },
930
+
931
+ asOk(): never {
932
+ throw new TypeError('Called `Result::asOk()` on an `Err` value');
933
+ },
934
+ asErr<U>(): Result<U, E> {
935
+ return err as unknown as Result<U, E>;
936
+ },
902
937
  } as const;
903
938
 
904
939
  return err;