happy-rusty 1.5.0 → 1.6.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/CHANGELOG.md +206 -0
- package/README.cn.md +265 -19
- package/README.md +261 -21
- package/dist/main.cjs +382 -32
- package/dist/main.cjs.map +1 -1
- package/dist/main.mjs +374 -33
- package/dist/main.mjs.map +1 -1
- package/dist/types.d.ts +2002 -52
- package/package.json +37 -24
- package/dist/types.d.ts.map +0 -1
- package/docs/README.md +0 -47
- package/docs/functions/Err.md +0 -46
- package/docs/functions/Ok.md +0 -70
- package/docs/functions/Some.md +0 -45
- package/docs/functions/isOption.md +0 -35
- package/docs/functions/isResult.md +0 -36
- package/docs/functions/promiseToAsyncResult.md +0 -50
- package/docs/interfaces/None.md +0 -979
- package/docs/interfaces/Option.md +0 -857
- package/docs/interfaces/Result.md +0 -903
- package/docs/type-aliases/AsyncIOResult.md +0 -24
- package/docs/type-aliases/AsyncOption.md +0 -24
- package/docs/type-aliases/AsyncResult.md +0 -25
- package/docs/type-aliases/AsyncVoidIOResult.md +0 -17
- package/docs/type-aliases/AsyncVoidResult.md +0 -23
- package/docs/type-aliases/IOResult.md +0 -24
- package/docs/type-aliases/VoidIOResult.md +0 -17
- package/docs/type-aliases/VoidResult.md +0 -23
- package/docs/variables/None.md +0 -18
- package/docs/variables/RESULT_FALSE.md +0 -18
- package/docs/variables/RESULT_TRUE.md +0 -18
- package/docs/variables/RESULT_VOID.md +0 -17
- package/docs/variables/RESULT_ZERO.md +0 -18
- package/src/enum/constants.ts +0 -30
- package/src/enum/core.ts +0 -635
- package/src/enum/defines.ts +0 -45
- package/src/enum/extensions.ts +0 -31
- package/src/enum/mod.ts +0 -6
- package/src/enum/prelude.ts +0 -619
- package/src/enum/symbols.ts +0 -9
- package/src/enum/utils.ts +0 -27
- package/src/mod.ts +0 -1
|
@@ -1,903 +0,0 @@
|
|
|
1
|
-
[**happy-rusty**](../README.md) • **Docs**
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[happy-rusty](../README.md) / Result
|
|
6
|
-
|
|
7
|
-
# Interface: Result\<T, E\>
|
|
8
|
-
|
|
9
|
-
The `Result` type is used for returning and propagating errors.
|
|
10
|
-
It is an enum with the variants, `Ok(T)`, representing success and containing a value, and `Err(E)`, representing error and containing an error value.
|
|
11
|
-
This interface includes methods that act on the `Result` type, similar to Rust's `Result` enum.
|
|
12
|
-
|
|
13
|
-
As Rust Code:
|
|
14
|
-
```rust
|
|
15
|
-
pub enum Result<T, E> {
|
|
16
|
-
Ok(T),
|
|
17
|
-
Err(E),
|
|
18
|
-
}
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## Type Parameters
|
|
22
|
-
|
|
23
|
-
| Type Parameter | Description |
|
|
24
|
-
| ------ | ------ |
|
|
25
|
-
| `T` | The type of the value contained in a successful `Result`. |
|
|
26
|
-
| `E` | The type of the error contained in an unsuccessful `Result`. |
|
|
27
|
-
|
|
28
|
-
## Methods
|
|
29
|
-
|
|
30
|
-
### and()
|
|
31
|
-
|
|
32
|
-
```ts
|
|
33
|
-
and<U>(other): Result<U, E>
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
Returns `this` if the result is `Err`, otherwise returns the passed `Result`.
|
|
37
|
-
|
|
38
|
-
#### Type Parameters
|
|
39
|
-
|
|
40
|
-
| Type Parameter | Description |
|
|
41
|
-
| ------ | ------ |
|
|
42
|
-
| `U` | The type of the value in the other `Result`. |
|
|
43
|
-
|
|
44
|
-
#### Parameters
|
|
45
|
-
|
|
46
|
-
| Parameter | Type | Description |
|
|
47
|
-
| ------ | ------ | ------ |
|
|
48
|
-
| `other` | [`Result`](Result.md)\<`U`, `E`\> | The `Result` to return if `this` is `Ok`. |
|
|
49
|
-
|
|
50
|
-
#### Returns
|
|
51
|
-
|
|
52
|
-
[`Result`](Result.md)\<`U`, `E`\>
|
|
53
|
-
|
|
54
|
-
The passed `Result` if `this` is `Ok`, otherwise returns `this` (which is `Err`).
|
|
55
|
-
|
|
56
|
-
#### Defined in
|
|
57
|
-
|
|
58
|
-
[core.ts:528](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L528)
|
|
59
|
-
|
|
60
|
-
***
|
|
61
|
-
|
|
62
|
-
### andThen()
|
|
63
|
-
|
|
64
|
-
```ts
|
|
65
|
-
andThen<U>(fn): Result<U, E>
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
Calls the provided function with the contained value if `this` is `Ok`, otherwise returns `this` as `Err`.
|
|
69
|
-
|
|
70
|
-
#### Type Parameters
|
|
71
|
-
|
|
72
|
-
| Type Parameter | Description |
|
|
73
|
-
| ------ | ------ |
|
|
74
|
-
| `U` | The type of the value returned by the function. |
|
|
75
|
-
|
|
76
|
-
#### Parameters
|
|
77
|
-
|
|
78
|
-
| Parameter | Type | Description |
|
|
79
|
-
| ------ | ------ | ------ |
|
|
80
|
-
| `fn` | (`value`) => [`Result`](Result.md)\<`U`, `E`\> | A function that takes the `Ok` value and returns a `Result`. |
|
|
81
|
-
|
|
82
|
-
#### Returns
|
|
83
|
-
|
|
84
|
-
[`Result`](Result.md)\<`U`, `E`\>
|
|
85
|
-
|
|
86
|
-
The result of `fn` if `this` is `Ok`, otherwise `this` as `Err`.
|
|
87
|
-
|
|
88
|
-
#### Defined in
|
|
89
|
-
|
|
90
|
-
[core.ts:544](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L544)
|
|
91
|
-
|
|
92
|
-
***
|
|
93
|
-
|
|
94
|
-
### andThenAsync()
|
|
95
|
-
|
|
96
|
-
```ts
|
|
97
|
-
andThenAsync<U>(fn): AsyncResult<U, E>
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
Asynchronous version of `andThen`.
|
|
101
|
-
|
|
102
|
-
#### Type Parameters
|
|
103
|
-
|
|
104
|
-
| Type Parameter |
|
|
105
|
-
| ------ |
|
|
106
|
-
| `U` |
|
|
107
|
-
|
|
108
|
-
#### Parameters
|
|
109
|
-
|
|
110
|
-
| Parameter | Type |
|
|
111
|
-
| ------ | ------ |
|
|
112
|
-
| `fn` | (`value`) => [`AsyncResult`](../type-aliases/AsyncResult.md)\<`U`, `E`\> |
|
|
113
|
-
|
|
114
|
-
#### Returns
|
|
115
|
-
|
|
116
|
-
[`AsyncResult`](../type-aliases/AsyncResult.md)\<`U`, `E`\>
|
|
117
|
-
|
|
118
|
-
#### Defined in
|
|
119
|
-
|
|
120
|
-
[core.ts:549](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L549)
|
|
121
|
-
|
|
122
|
-
***
|
|
123
|
-
|
|
124
|
-
### asErr()
|
|
125
|
-
|
|
126
|
-
```ts
|
|
127
|
-
asErr<U>(): Result<U, E>
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
Transforms the current Result into a new Result where the type of the success result is replaced with a new type `U`.
|
|
131
|
-
The type of the error result remains unchanged.
|
|
132
|
-
Useful where you need to return an Error chained to another type.
|
|
133
|
-
Just same as `result as unknown as Result<U, E>`.
|
|
134
|
-
|
|
135
|
-
#### Type Parameters
|
|
136
|
-
|
|
137
|
-
| Type Parameter | Description |
|
|
138
|
-
| ------ | ------ |
|
|
139
|
-
| `U` | The new type for the success result. |
|
|
140
|
-
|
|
141
|
-
#### Returns
|
|
142
|
-
|
|
143
|
-
[`Result`](Result.md)\<`U`, `E`\>
|
|
144
|
-
|
|
145
|
-
`this` but the success result type is `U`.
|
|
146
|
-
|
|
147
|
-
#### Defined in
|
|
148
|
-
|
|
149
|
-
[core.ts:612](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L612)
|
|
150
|
-
|
|
151
|
-
***
|
|
152
|
-
|
|
153
|
-
### asOk()
|
|
154
|
-
|
|
155
|
-
```ts
|
|
156
|
-
asOk<F>(): Result<T, F>
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
Transforms the current Result into a new Result where the type of the error result is replaced with a new type `F`.
|
|
160
|
-
The type of the success result remains unchanged.
|
|
161
|
-
Just same as `result as unknown as Result<T, F>`.
|
|
162
|
-
|
|
163
|
-
#### Type Parameters
|
|
164
|
-
|
|
165
|
-
| Type Parameter | Description |
|
|
166
|
-
| ------ | ------ |
|
|
167
|
-
| `F` | The new type for the error result. |
|
|
168
|
-
|
|
169
|
-
#### Returns
|
|
170
|
-
|
|
171
|
-
[`Result`](Result.md)\<`T`, `F`\>
|
|
172
|
-
|
|
173
|
-
`this` but the error result type is `F`.
|
|
174
|
-
|
|
175
|
-
#### Defined in
|
|
176
|
-
|
|
177
|
-
[core.ts:601](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L601)
|
|
178
|
-
|
|
179
|
-
***
|
|
180
|
-
|
|
181
|
-
### eq()
|
|
182
|
-
|
|
183
|
-
```ts
|
|
184
|
-
eq(other): boolean
|
|
185
|
-
```
|
|
186
|
-
|
|
187
|
-
Tests whether `this` and `other` are both `Ok` containing equal values, or both are `Err` containing equal errors.
|
|
188
|
-
|
|
189
|
-
#### Parameters
|
|
190
|
-
|
|
191
|
-
| Parameter | Type | Description |
|
|
192
|
-
| ------ | ------ | ------ |
|
|
193
|
-
| `other` | [`Result`](Result.md)\<`T`, `E`\> | The other `Result` to compare with. |
|
|
194
|
-
|
|
195
|
-
#### Returns
|
|
196
|
-
|
|
197
|
-
`boolean`
|
|
198
|
-
|
|
199
|
-
`true` if `this` and `other` are both `Ok` with equal values, or both are `Err` with equal errors, otherwise `false`.
|
|
200
|
-
|
|
201
|
-
#### Defined in
|
|
202
|
-
|
|
203
|
-
[core.ts:589](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L589)
|
|
204
|
-
|
|
205
|
-
***
|
|
206
|
-
|
|
207
|
-
### err()
|
|
208
|
-
|
|
209
|
-
```ts
|
|
210
|
-
err(): Option<E>
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
Converts from `Result<T, E>` to `Option<E>`.
|
|
214
|
-
If the result is `Err`, returns `Some(E)`.
|
|
215
|
-
If the result is `Ok`, returns `None`.
|
|
216
|
-
|
|
217
|
-
#### Returns
|
|
218
|
-
|
|
219
|
-
[`Option`](Option.md)\<`E`\>
|
|
220
|
-
|
|
221
|
-
#### Defined in
|
|
222
|
-
|
|
223
|
-
[core.ts:452](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L452)
|
|
224
|
-
|
|
225
|
-
***
|
|
226
|
-
|
|
227
|
-
### expect()
|
|
228
|
-
|
|
229
|
-
```ts
|
|
230
|
-
expect(msg): T
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
Returns the contained `Ok` value, with a provided error message if the result is `Err`.
|
|
234
|
-
|
|
235
|
-
#### Parameters
|
|
236
|
-
|
|
237
|
-
| Parameter | Type | Description |
|
|
238
|
-
| ------ | ------ | ------ |
|
|
239
|
-
| `msg` | `string` | The error message to provide if the result is an `Err`. |
|
|
240
|
-
|
|
241
|
-
#### Returns
|
|
242
|
-
|
|
243
|
-
`T`
|
|
244
|
-
|
|
245
|
-
#### Throws
|
|
246
|
-
|
|
247
|
-
Throws an error with the provided message if the result is an `Err`.
|
|
248
|
-
|
|
249
|
-
#### Defined in
|
|
250
|
-
|
|
251
|
-
[core.ts:390](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L390)
|
|
252
|
-
|
|
253
|
-
***
|
|
254
|
-
|
|
255
|
-
### expectErr()
|
|
256
|
-
|
|
257
|
-
```ts
|
|
258
|
-
expectErr(msg): E
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
Returns the contained `Err` value, with a provided error message if the result is `Ok`.
|
|
262
|
-
|
|
263
|
-
#### Parameters
|
|
264
|
-
|
|
265
|
-
| Parameter | Type | Description |
|
|
266
|
-
| ------ | ------ | ------ |
|
|
267
|
-
| `msg` | `string` | The error message to provide if the result is an `Ok`. |
|
|
268
|
-
|
|
269
|
-
#### Returns
|
|
270
|
-
|
|
271
|
-
`E`
|
|
272
|
-
|
|
273
|
-
#### Throws
|
|
274
|
-
|
|
275
|
-
Throws an error with the provided message if the result is an `Ok`.
|
|
276
|
-
|
|
277
|
-
#### Defined in
|
|
278
|
-
|
|
279
|
-
[core.ts:424](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L424)
|
|
280
|
-
|
|
281
|
-
***
|
|
282
|
-
|
|
283
|
-
### flatten()
|
|
284
|
-
|
|
285
|
-
```ts
|
|
286
|
-
flatten<T>(this): Result<T, E>
|
|
287
|
-
```
|
|
288
|
-
|
|
289
|
-
Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
|
|
290
|
-
If the result is `Ok(Ok(T))`, returns `Ok(T)`.
|
|
291
|
-
If the result is `Ok(Err(E))` or `Err(E)`, returns `Err(E)`.
|
|
292
|
-
|
|
293
|
-
#### Type Parameters
|
|
294
|
-
|
|
295
|
-
| Type Parameter |
|
|
296
|
-
| ------ |
|
|
297
|
-
| `T` |
|
|
298
|
-
|
|
299
|
-
#### Parameters
|
|
300
|
-
|
|
301
|
-
| Parameter | Type |
|
|
302
|
-
| ------ | ------ |
|
|
303
|
-
| `this` | [`Result`](Result.md)\<[`Result`](Result.md)\<`T`, `E`\>, `E`\> |
|
|
304
|
-
|
|
305
|
-
#### Returns
|
|
306
|
-
|
|
307
|
-
[`Result`](Result.md)\<`T`, `E`\>
|
|
308
|
-
|
|
309
|
-
#### Defined in
|
|
310
|
-
|
|
311
|
-
[core.ts:512](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L512)
|
|
312
|
-
|
|
313
|
-
***
|
|
314
|
-
|
|
315
|
-
### inspect()
|
|
316
|
-
|
|
317
|
-
```ts
|
|
318
|
-
inspect(fn): this
|
|
319
|
-
```
|
|
320
|
-
|
|
321
|
-
Calls the provided function with the contained value if `this` is `Ok`, for side effects only.
|
|
322
|
-
Does not modify the `Result`.
|
|
323
|
-
|
|
324
|
-
#### Parameters
|
|
325
|
-
|
|
326
|
-
| Parameter | Type | Description |
|
|
327
|
-
| ------ | ------ | ------ |
|
|
328
|
-
| `fn` | (`value`) => `void` | A function to call with the `Ok` value. |
|
|
329
|
-
|
|
330
|
-
#### Returns
|
|
331
|
-
|
|
332
|
-
`this`
|
|
333
|
-
|
|
334
|
-
`this`, unmodified.
|
|
335
|
-
|
|
336
|
-
#### Defined in
|
|
337
|
-
|
|
338
|
-
[core.ts:572](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L572)
|
|
339
|
-
|
|
340
|
-
***
|
|
341
|
-
|
|
342
|
-
### inspectErr()
|
|
343
|
-
|
|
344
|
-
```ts
|
|
345
|
-
inspectErr(fn): this
|
|
346
|
-
```
|
|
347
|
-
|
|
348
|
-
Calls the provided function with the contained error if `this` is `Err`, for side effects only.
|
|
349
|
-
Does not modify the `Result`.
|
|
350
|
-
|
|
351
|
-
#### Parameters
|
|
352
|
-
|
|
353
|
-
| Parameter | Type | Description |
|
|
354
|
-
| ------ | ------ | ------ |
|
|
355
|
-
| `fn` | (`error`) => `void` | A function to call with the `Err` value. |
|
|
356
|
-
|
|
357
|
-
#### Returns
|
|
358
|
-
|
|
359
|
-
`this`
|
|
360
|
-
|
|
361
|
-
`this`, unmodified.
|
|
362
|
-
|
|
363
|
-
#### Defined in
|
|
364
|
-
|
|
365
|
-
[core.ts:580](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L580)
|
|
366
|
-
|
|
367
|
-
***
|
|
368
|
-
|
|
369
|
-
### isErr()
|
|
370
|
-
|
|
371
|
-
```ts
|
|
372
|
-
isErr(): boolean
|
|
373
|
-
```
|
|
374
|
-
|
|
375
|
-
Returns `true` if the result is `Err`.
|
|
376
|
-
|
|
377
|
-
#### Returns
|
|
378
|
-
|
|
379
|
-
`boolean`
|
|
380
|
-
|
|
381
|
-
#### Defined in
|
|
382
|
-
|
|
383
|
-
[core.ts:353](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L353)
|
|
384
|
-
|
|
385
|
-
***
|
|
386
|
-
|
|
387
|
-
### isErrAnd()
|
|
388
|
-
|
|
389
|
-
```ts
|
|
390
|
-
isErrAnd(predicate): boolean
|
|
391
|
-
```
|
|
392
|
-
|
|
393
|
-
Returns `true` if the result is `Err` and the provided predicate returns `true` for the contained error.
|
|
394
|
-
|
|
395
|
-
#### Parameters
|
|
396
|
-
|
|
397
|
-
| Parameter | Type | Description |
|
|
398
|
-
| ------ | ------ | ------ |
|
|
399
|
-
| `predicate` | (`error`) => `boolean` | A function that takes the `Err` value and returns a boolean. |
|
|
400
|
-
|
|
401
|
-
#### Returns
|
|
402
|
-
|
|
403
|
-
`boolean`
|
|
404
|
-
|
|
405
|
-
#### Defined in
|
|
406
|
-
|
|
407
|
-
[core.ts:370](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L370)
|
|
408
|
-
|
|
409
|
-
***
|
|
410
|
-
|
|
411
|
-
### isErrAndAsync()
|
|
412
|
-
|
|
413
|
-
```ts
|
|
414
|
-
isErrAndAsync(predicate): Promise<boolean>
|
|
415
|
-
```
|
|
416
|
-
|
|
417
|
-
Asynchronous version of `isErrAnd`.
|
|
418
|
-
|
|
419
|
-
#### Parameters
|
|
420
|
-
|
|
421
|
-
| Parameter | Type |
|
|
422
|
-
| ------ | ------ |
|
|
423
|
-
| `predicate` | (`error`) => `Promise`\<`boolean`\> |
|
|
424
|
-
|
|
425
|
-
#### Returns
|
|
426
|
-
|
|
427
|
-
`Promise`\<`boolean`\>
|
|
428
|
-
|
|
429
|
-
#### Defined in
|
|
430
|
-
|
|
431
|
-
[core.ts:375](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L375)
|
|
432
|
-
|
|
433
|
-
***
|
|
434
|
-
|
|
435
|
-
### isOk()
|
|
436
|
-
|
|
437
|
-
```ts
|
|
438
|
-
isOk(): boolean
|
|
439
|
-
```
|
|
440
|
-
|
|
441
|
-
Returns `true` if the result is `Ok`.
|
|
442
|
-
|
|
443
|
-
#### Returns
|
|
444
|
-
|
|
445
|
-
`boolean`
|
|
446
|
-
|
|
447
|
-
#### Defined in
|
|
448
|
-
|
|
449
|
-
[core.ts:348](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L348)
|
|
450
|
-
|
|
451
|
-
***
|
|
452
|
-
|
|
453
|
-
### isOkAnd()
|
|
454
|
-
|
|
455
|
-
```ts
|
|
456
|
-
isOkAnd(predicate): boolean
|
|
457
|
-
```
|
|
458
|
-
|
|
459
|
-
Returns `true` if the result is `Ok` and the provided predicate returns `true` for the contained value.
|
|
460
|
-
|
|
461
|
-
#### Parameters
|
|
462
|
-
|
|
463
|
-
| Parameter | Type | Description |
|
|
464
|
-
| ------ | ------ | ------ |
|
|
465
|
-
| `predicate` | (`value`) => `boolean` | A function that takes the `Ok` value and returns a boolean. |
|
|
466
|
-
|
|
467
|
-
#### Returns
|
|
468
|
-
|
|
469
|
-
`boolean`
|
|
470
|
-
|
|
471
|
-
#### Defined in
|
|
472
|
-
|
|
473
|
-
[core.ts:359](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L359)
|
|
474
|
-
|
|
475
|
-
***
|
|
476
|
-
|
|
477
|
-
### isOkAndAsync()
|
|
478
|
-
|
|
479
|
-
```ts
|
|
480
|
-
isOkAndAsync(predicate): Promise<boolean>
|
|
481
|
-
```
|
|
482
|
-
|
|
483
|
-
Asynchronous version of `isOkAnd`.
|
|
484
|
-
|
|
485
|
-
#### Parameters
|
|
486
|
-
|
|
487
|
-
| Parameter | Type |
|
|
488
|
-
| ------ | ------ |
|
|
489
|
-
| `predicate` | (`value`) => `Promise`\<`boolean`\> |
|
|
490
|
-
|
|
491
|
-
#### Returns
|
|
492
|
-
|
|
493
|
-
`Promise`\<`boolean`\>
|
|
494
|
-
|
|
495
|
-
#### Defined in
|
|
496
|
-
|
|
497
|
-
[core.ts:364](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L364)
|
|
498
|
-
|
|
499
|
-
***
|
|
500
|
-
|
|
501
|
-
### map()
|
|
502
|
-
|
|
503
|
-
```ts
|
|
504
|
-
map<U>(fn): Result<U, E>
|
|
505
|
-
```
|
|
506
|
-
|
|
507
|
-
Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value,
|
|
508
|
-
leaving an `Err` value untouched.
|
|
509
|
-
|
|
510
|
-
#### Type Parameters
|
|
511
|
-
|
|
512
|
-
| Type Parameter | Description |
|
|
513
|
-
| ------ | ------ |
|
|
514
|
-
| `U` | The type of the value returned by the map function. |
|
|
515
|
-
|
|
516
|
-
#### Parameters
|
|
517
|
-
|
|
518
|
-
| Parameter | Type | Description |
|
|
519
|
-
| ------ | ------ | ------ |
|
|
520
|
-
| `fn` | (`value`) => `U` | A function that takes the `Ok` value and returns a new value. |
|
|
521
|
-
|
|
522
|
-
#### Returns
|
|
523
|
-
|
|
524
|
-
[`Result`](Result.md)\<`U`, `E`\>
|
|
525
|
-
|
|
526
|
-
#### Defined in
|
|
527
|
-
|
|
528
|
-
[core.ts:473](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L473)
|
|
529
|
-
|
|
530
|
-
***
|
|
531
|
-
|
|
532
|
-
### mapErr()
|
|
533
|
-
|
|
534
|
-
```ts
|
|
535
|
-
mapErr<F>(fn): Result<T, F>
|
|
536
|
-
```
|
|
537
|
-
|
|
538
|
-
Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value,
|
|
539
|
-
leaving an `Ok` value untouched.
|
|
540
|
-
|
|
541
|
-
#### Type Parameters
|
|
542
|
-
|
|
543
|
-
| Type Parameter | Description |
|
|
544
|
-
| ------ | ------ |
|
|
545
|
-
| `F` | The type of the error returned by the map function. |
|
|
546
|
-
|
|
547
|
-
#### Parameters
|
|
548
|
-
|
|
549
|
-
| Parameter | Type | Description |
|
|
550
|
-
| ------ | ------ | ------ |
|
|
551
|
-
| `fn` | (`error`) => `F` | A function that takes the `Err` value and returns a new error value. |
|
|
552
|
-
|
|
553
|
-
#### Returns
|
|
554
|
-
|
|
555
|
-
[`Result`](Result.md)\<`T`, `F`\>
|
|
556
|
-
|
|
557
|
-
#### Defined in
|
|
558
|
-
|
|
559
|
-
[core.ts:485](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L485)
|
|
560
|
-
|
|
561
|
-
***
|
|
562
|
-
|
|
563
|
-
### mapOr()
|
|
564
|
-
|
|
565
|
-
```ts
|
|
566
|
-
mapOr<U>(defaultValue, fn): U
|
|
567
|
-
```
|
|
568
|
-
|
|
569
|
-
Maps a `Result<T, E>` to `U` by applying a function to the contained `Ok` value (if `Ok`), or returns the provided default (if `Err`).
|
|
570
|
-
|
|
571
|
-
#### Type Parameters
|
|
572
|
-
|
|
573
|
-
| Type Parameter | Description |
|
|
574
|
-
| ------ | ------ |
|
|
575
|
-
| `U` | The type of the value returned by the map function or the default value. |
|
|
576
|
-
|
|
577
|
-
#### Parameters
|
|
578
|
-
|
|
579
|
-
| Parameter | Type | Description |
|
|
580
|
-
| ------ | ------ | ------ |
|
|
581
|
-
| `defaultValue` | `U` | The value to return if the result is `Err`. |
|
|
582
|
-
| `fn` | (`value`) => `U` | A function that takes the `Ok` value and returns a new value. |
|
|
583
|
-
|
|
584
|
-
#### Returns
|
|
585
|
-
|
|
586
|
-
`U`
|
|
587
|
-
|
|
588
|
-
#### Defined in
|
|
589
|
-
|
|
590
|
-
[core.ts:497](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L497)
|
|
591
|
-
|
|
592
|
-
***
|
|
593
|
-
|
|
594
|
-
### mapOrElse()
|
|
595
|
-
|
|
596
|
-
```ts
|
|
597
|
-
mapOrElse<U>(defaultFn, fn): U
|
|
598
|
-
```
|
|
599
|
-
|
|
600
|
-
Maps a `Result<T, E>` to `U` by applying a function to the contained `Ok` value (if `Ok`), or computes a default (if `Err`).
|
|
601
|
-
|
|
602
|
-
#### Type Parameters
|
|
603
|
-
|
|
604
|
-
| Type Parameter | Description |
|
|
605
|
-
| ------ | ------ |
|
|
606
|
-
| `U` | The type of the value returned by the map function or the default function. |
|
|
607
|
-
|
|
608
|
-
#### Parameters
|
|
609
|
-
|
|
610
|
-
| Parameter | Type | Description |
|
|
611
|
-
| ------ | ------ | ------ |
|
|
612
|
-
| `defaultFn` | (`error`) => `U` | A function that returns the default value. |
|
|
613
|
-
| `fn` | (`value`) => `U` | A function that takes the `Ok` value and returns a new value. |
|
|
614
|
-
|
|
615
|
-
#### Returns
|
|
616
|
-
|
|
617
|
-
`U`
|
|
618
|
-
|
|
619
|
-
#### Defined in
|
|
620
|
-
|
|
621
|
-
[core.ts:505](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L505)
|
|
622
|
-
|
|
623
|
-
***
|
|
624
|
-
|
|
625
|
-
### ok()
|
|
626
|
-
|
|
627
|
-
```ts
|
|
628
|
-
ok(): Option<T>
|
|
629
|
-
```
|
|
630
|
-
|
|
631
|
-
Converts from `Result<T, E>` to `Option<T>`.
|
|
632
|
-
If the result is `Ok`, returns `Some(T)`.
|
|
633
|
-
If the result is `Err`, returns `None`.
|
|
634
|
-
|
|
635
|
-
#### Returns
|
|
636
|
-
|
|
637
|
-
[`Option`](Option.md)\<`T`\>
|
|
638
|
-
|
|
639
|
-
#### Defined in
|
|
640
|
-
|
|
641
|
-
[core.ts:445](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L445)
|
|
642
|
-
|
|
643
|
-
***
|
|
644
|
-
|
|
645
|
-
### or()
|
|
646
|
-
|
|
647
|
-
```ts
|
|
648
|
-
or<F>(other): Result<T, F>
|
|
649
|
-
```
|
|
650
|
-
|
|
651
|
-
Returns `this` if it is `Ok`, otherwise returns the passed `Result`.
|
|
652
|
-
|
|
653
|
-
#### Type Parameters
|
|
654
|
-
|
|
655
|
-
| Type Parameter | Description |
|
|
656
|
-
| ------ | ------ |
|
|
657
|
-
| `F` | The type of the error in the other `Result`. |
|
|
658
|
-
|
|
659
|
-
#### Parameters
|
|
660
|
-
|
|
661
|
-
| Parameter | Type | Description |
|
|
662
|
-
| ------ | ------ | ------ |
|
|
663
|
-
| `other` | [`Result`](Result.md)\<`T`, `F`\> | The `Result` to return if `this` is `Err`. |
|
|
664
|
-
|
|
665
|
-
#### Returns
|
|
666
|
-
|
|
667
|
-
[`Result`](Result.md)\<`T`, `F`\>
|
|
668
|
-
|
|
669
|
-
`this` if it is `Ok`, otherwise returns `other`.
|
|
670
|
-
|
|
671
|
-
#### Defined in
|
|
672
|
-
|
|
673
|
-
[core.ts:536](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L536)
|
|
674
|
-
|
|
675
|
-
***
|
|
676
|
-
|
|
677
|
-
### orElse()
|
|
678
|
-
|
|
679
|
-
```ts
|
|
680
|
-
orElse<F>(fn): Result<T, F>
|
|
681
|
-
```
|
|
682
|
-
|
|
683
|
-
Calls the provided function with the contained error if `this` is `Err`, otherwise returns `this` as `Ok`.
|
|
684
|
-
|
|
685
|
-
#### Type Parameters
|
|
686
|
-
|
|
687
|
-
| Type Parameter | Description |
|
|
688
|
-
| ------ | ------ |
|
|
689
|
-
| `F` | The type of the error returned by the function. |
|
|
690
|
-
|
|
691
|
-
#### Parameters
|
|
692
|
-
|
|
693
|
-
| Parameter | Type | Description |
|
|
694
|
-
| ------ | ------ | ------ |
|
|
695
|
-
| `fn` | (`error`) => [`Result`](Result.md)\<`T`, `F`\> | A function that takes the `Err` value and returns a `Result`. |
|
|
696
|
-
|
|
697
|
-
#### Returns
|
|
698
|
-
|
|
699
|
-
[`Result`](Result.md)\<`T`, `F`\>
|
|
700
|
-
|
|
701
|
-
The result of `fn` if `this` is `Err`, otherwise `this` as `Ok`.
|
|
702
|
-
|
|
703
|
-
#### Defined in
|
|
704
|
-
|
|
705
|
-
[core.ts:557](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L557)
|
|
706
|
-
|
|
707
|
-
***
|
|
708
|
-
|
|
709
|
-
### orElseAsync()
|
|
710
|
-
|
|
711
|
-
```ts
|
|
712
|
-
orElseAsync<F>(fn): AsyncResult<T, F>
|
|
713
|
-
```
|
|
714
|
-
|
|
715
|
-
Asynchronous version of `orElse`.
|
|
716
|
-
|
|
717
|
-
#### Type Parameters
|
|
718
|
-
|
|
719
|
-
| Type Parameter |
|
|
720
|
-
| ------ |
|
|
721
|
-
| `F` |
|
|
722
|
-
|
|
723
|
-
#### Parameters
|
|
724
|
-
|
|
725
|
-
| Parameter | Type |
|
|
726
|
-
| ------ | ------ |
|
|
727
|
-
| `fn` | (`error`) => [`AsyncResult`](../type-aliases/AsyncResult.md)\<`T`, `F`\> |
|
|
728
|
-
|
|
729
|
-
#### Returns
|
|
730
|
-
|
|
731
|
-
[`AsyncResult`](../type-aliases/AsyncResult.md)\<`T`, `F`\>
|
|
732
|
-
|
|
733
|
-
#### Defined in
|
|
734
|
-
|
|
735
|
-
[core.ts:562](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L562)
|
|
736
|
-
|
|
737
|
-
***
|
|
738
|
-
|
|
739
|
-
### toString()
|
|
740
|
-
|
|
741
|
-
```ts
|
|
742
|
-
toString(): string
|
|
743
|
-
```
|
|
744
|
-
|
|
745
|
-
Custom `toString` implementation that uses the `Result`'s contained value.
|
|
746
|
-
|
|
747
|
-
#### Returns
|
|
748
|
-
|
|
749
|
-
`string`
|
|
750
|
-
|
|
751
|
-
#### Defined in
|
|
752
|
-
|
|
753
|
-
[core.ts:617](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L617)
|
|
754
|
-
|
|
755
|
-
***
|
|
756
|
-
|
|
757
|
-
### transpose()
|
|
758
|
-
|
|
759
|
-
```ts
|
|
760
|
-
transpose<T>(this): Option<Result<T, E>>
|
|
761
|
-
```
|
|
762
|
-
|
|
763
|
-
Transposes a `Result` of an `Option` into an `Option` of a `Result`.
|
|
764
|
-
|
|
765
|
-
#### Type Parameters
|
|
766
|
-
|
|
767
|
-
| Type Parameter | Description |
|
|
768
|
-
| ------ | ------ |
|
|
769
|
-
| `T` | The type of the success value in the `Ok` variant of the `Option`. |
|
|
770
|
-
|
|
771
|
-
#### Parameters
|
|
772
|
-
|
|
773
|
-
| Parameter | Type |
|
|
774
|
-
| ------ | ------ |
|
|
775
|
-
| `this` | [`Result`](Result.md)\<[`Option`](Option.md)\<`T`\>, `E`\> |
|
|
776
|
-
|
|
777
|
-
#### Returns
|
|
778
|
-
|
|
779
|
-
[`Option`](Option.md)\<[`Result`](Result.md)\<`T`, `E`\>\>
|
|
780
|
-
|
|
781
|
-
`Some` containing `Ok` if the result is `Ok` containing `Some`,
|
|
782
|
-
`Some` containing `Err` if the result is `Err`,
|
|
783
|
-
`None` if the result is `Ok` containing `None`.
|
|
784
|
-
|
|
785
|
-
#### Defined in
|
|
786
|
-
|
|
787
|
-
[core.ts:461](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L461)
|
|
788
|
-
|
|
789
|
-
***
|
|
790
|
-
|
|
791
|
-
### unwrap()
|
|
792
|
-
|
|
793
|
-
```ts
|
|
794
|
-
unwrap(): T
|
|
795
|
-
```
|
|
796
|
-
|
|
797
|
-
Returns the contained `Ok` value.
|
|
798
|
-
|
|
799
|
-
#### Returns
|
|
800
|
-
|
|
801
|
-
`T`
|
|
802
|
-
|
|
803
|
-
#### Throws
|
|
804
|
-
|
|
805
|
-
Throws an error if the result is an `Err`.
|
|
806
|
-
|
|
807
|
-
#### Defined in
|
|
808
|
-
|
|
809
|
-
[core.ts:396](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L396)
|
|
810
|
-
|
|
811
|
-
***
|
|
812
|
-
|
|
813
|
-
### unwrapErr()
|
|
814
|
-
|
|
815
|
-
```ts
|
|
816
|
-
unwrapErr(): E
|
|
817
|
-
```
|
|
818
|
-
|
|
819
|
-
Returns the contained `Err` value.
|
|
820
|
-
|
|
821
|
-
#### Returns
|
|
822
|
-
|
|
823
|
-
`E`
|
|
824
|
-
|
|
825
|
-
#### Throws
|
|
826
|
-
|
|
827
|
-
Throws an error if the result is an `Ok`.
|
|
828
|
-
|
|
829
|
-
#### Defined in
|
|
830
|
-
|
|
831
|
-
[core.ts:430](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L430)
|
|
832
|
-
|
|
833
|
-
***
|
|
834
|
-
|
|
835
|
-
### unwrapOr()
|
|
836
|
-
|
|
837
|
-
```ts
|
|
838
|
-
unwrapOr(defaultValue): T
|
|
839
|
-
```
|
|
840
|
-
|
|
841
|
-
Returns the contained `Ok` value or a provided default.
|
|
842
|
-
|
|
843
|
-
#### Parameters
|
|
844
|
-
|
|
845
|
-
| Parameter | Type | Description |
|
|
846
|
-
| ------ | ------ | ------ |
|
|
847
|
-
| `defaultValue` | `T` | The value to return if the result is an `Err`. |
|
|
848
|
-
|
|
849
|
-
#### Returns
|
|
850
|
-
|
|
851
|
-
`T`
|
|
852
|
-
|
|
853
|
-
#### Defined in
|
|
854
|
-
|
|
855
|
-
[core.ts:402](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L402)
|
|
856
|
-
|
|
857
|
-
***
|
|
858
|
-
|
|
859
|
-
### unwrapOrElse()
|
|
860
|
-
|
|
861
|
-
```ts
|
|
862
|
-
unwrapOrElse(fn): T
|
|
863
|
-
```
|
|
864
|
-
|
|
865
|
-
Returns the contained `Ok` value or computes it from a closure if the result is `Err`.
|
|
866
|
-
|
|
867
|
-
#### Parameters
|
|
868
|
-
|
|
869
|
-
| Parameter | Type | Description |
|
|
870
|
-
| ------ | ------ | ------ |
|
|
871
|
-
| `fn` | (`error`) => `T` | A function that takes the `Err` value and returns an `Ok` value. |
|
|
872
|
-
|
|
873
|
-
#### Returns
|
|
874
|
-
|
|
875
|
-
`T`
|
|
876
|
-
|
|
877
|
-
#### Defined in
|
|
878
|
-
|
|
879
|
-
[core.ts:408](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L408)
|
|
880
|
-
|
|
881
|
-
***
|
|
882
|
-
|
|
883
|
-
### unwrapOrElseAsync()
|
|
884
|
-
|
|
885
|
-
```ts
|
|
886
|
-
unwrapOrElseAsync(fn): Promise<T>
|
|
887
|
-
```
|
|
888
|
-
|
|
889
|
-
Asynchronous version of `unwrapOrElse`.
|
|
890
|
-
|
|
891
|
-
#### Parameters
|
|
892
|
-
|
|
893
|
-
| Parameter | Type |
|
|
894
|
-
| ------ | ------ |
|
|
895
|
-
| `fn` | (`error`) => `Promise`\<`T`\> |
|
|
896
|
-
|
|
897
|
-
#### Returns
|
|
898
|
-
|
|
899
|
-
`Promise`\<`T`\>
|
|
900
|
-
|
|
901
|
-
#### Defined in
|
|
902
|
-
|
|
903
|
-
[core.ts:413](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L413)
|