happy-rusty 1.5.0 → 1.6.1
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 +214 -0
- package/LICENSE +21 -674
- 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 +38 -25
- 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,857 +0,0 @@
|
|
|
1
|
-
[**happy-rusty**](../README.md) • **Docs**
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[happy-rusty](../README.md) / Option
|
|
6
|
-
|
|
7
|
-
# Interface: Option\<T\>
|
|
8
|
-
|
|
9
|
-
Type `Option` represents an optional value: every `Option` is either `Some` and contains a value, or `None`, and does not.
|
|
10
|
-
This interface includes methods that act on the `Option` type, similar to Rust's `Option` enum.
|
|
11
|
-
|
|
12
|
-
As Rust Code:
|
|
13
|
-
```rust
|
|
14
|
-
pub enum Option<T> {
|
|
15
|
-
None,
|
|
16
|
-
Some(T),
|
|
17
|
-
}
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
## Extended by
|
|
21
|
-
|
|
22
|
-
- [`None`](None.md)
|
|
23
|
-
|
|
24
|
-
## Type Parameters
|
|
25
|
-
|
|
26
|
-
| Type Parameter | Description |
|
|
27
|
-
| ------ | ------ |
|
|
28
|
-
| `T` | The type of the value contained in the `Some` variant. |
|
|
29
|
-
|
|
30
|
-
## Methods
|
|
31
|
-
|
|
32
|
-
### and()
|
|
33
|
-
|
|
34
|
-
```ts
|
|
35
|
-
and<U>(other): Option<U>
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
Returns `None` if the Option is `None`, otherwise returns `other`.
|
|
39
|
-
This is sometimes called "and then" because it is similar to a logical AND operation.
|
|
40
|
-
|
|
41
|
-
#### Type Parameters
|
|
42
|
-
|
|
43
|
-
| Type Parameter | Description |
|
|
44
|
-
| ------ | ------ |
|
|
45
|
-
| `U` | The type of the value in the other `Option`. |
|
|
46
|
-
|
|
47
|
-
#### Parameters
|
|
48
|
-
|
|
49
|
-
| Parameter | Type | Description |
|
|
50
|
-
| ------ | ------ | ------ |
|
|
51
|
-
| `other` | [`Option`](Option.md)\<`U`\> | The `Option` to return if `this` is `Some`. |
|
|
52
|
-
|
|
53
|
-
#### Returns
|
|
54
|
-
|
|
55
|
-
[`Option`](Option.md)\<`U`\>
|
|
56
|
-
|
|
57
|
-
`None` if `this` is `None`, otherwise returns `other`.
|
|
58
|
-
|
|
59
|
-
#### Defined in
|
|
60
|
-
|
|
61
|
-
[core.ts:232](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L232)
|
|
62
|
-
|
|
63
|
-
***
|
|
64
|
-
|
|
65
|
-
### andThen()
|
|
66
|
-
|
|
67
|
-
```ts
|
|
68
|
-
andThen<U>(fn): Option<U>
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
Returns `None` if the Option is `None`, otherwise calls `fn` with the wrapped value and returns the result.
|
|
72
|
-
This function can be used for control flow based on `Option` values.
|
|
73
|
-
|
|
74
|
-
#### Type Parameters
|
|
75
|
-
|
|
76
|
-
| Type Parameter | Description |
|
|
77
|
-
| ------ | ------ |
|
|
78
|
-
| `U` | The type of the value returned by the function. |
|
|
79
|
-
|
|
80
|
-
#### Parameters
|
|
81
|
-
|
|
82
|
-
| Parameter | Type | Description |
|
|
83
|
-
| ------ | ------ | ------ |
|
|
84
|
-
| `fn` | (`value`) => [`Option`](Option.md)\<`U`\> | A function that takes the contained value and returns an `Option`. |
|
|
85
|
-
|
|
86
|
-
#### Returns
|
|
87
|
-
|
|
88
|
-
[`Option`](Option.md)\<`U`\>
|
|
89
|
-
|
|
90
|
-
The result of `fn` if `this` is `Some`, otherwise `None`.
|
|
91
|
-
|
|
92
|
-
#### Defined in
|
|
93
|
-
|
|
94
|
-
[core.ts:241](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L241)
|
|
95
|
-
|
|
96
|
-
***
|
|
97
|
-
|
|
98
|
-
### andThenAsync()
|
|
99
|
-
|
|
100
|
-
```ts
|
|
101
|
-
andThenAsync<U>(fn): AsyncOption<U>
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
Asynchronous version of `andThen`.
|
|
105
|
-
|
|
106
|
-
#### Type Parameters
|
|
107
|
-
|
|
108
|
-
| Type Parameter |
|
|
109
|
-
| ------ |
|
|
110
|
-
| `U` |
|
|
111
|
-
|
|
112
|
-
#### Parameters
|
|
113
|
-
|
|
114
|
-
| Parameter | Type |
|
|
115
|
-
| ------ | ------ |
|
|
116
|
-
| `fn` | (`value`) => [`AsyncOption`](../type-aliases/AsyncOption.md)\<`U`\> |
|
|
117
|
-
|
|
118
|
-
#### Returns
|
|
119
|
-
|
|
120
|
-
[`AsyncOption`](../type-aliases/AsyncOption.md)\<`U`\>
|
|
121
|
-
|
|
122
|
-
#### Defined in
|
|
123
|
-
|
|
124
|
-
[core.ts:246](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L246)
|
|
125
|
-
|
|
126
|
-
***
|
|
127
|
-
|
|
128
|
-
### eq()
|
|
129
|
-
|
|
130
|
-
```ts
|
|
131
|
-
eq(other): boolean
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
Tests whether `this` and `other` are both `Some` containing equal values, or both are `None`.
|
|
135
|
-
This method can be used for comparing `Option` instances in a value-sensitive manner.
|
|
136
|
-
|
|
137
|
-
#### Parameters
|
|
138
|
-
|
|
139
|
-
| Parameter | Type | Description |
|
|
140
|
-
| ------ | ------ | ------ |
|
|
141
|
-
| `other` | [`Option`](Option.md)\<`T`\> | The other `Option` to compare with. |
|
|
142
|
-
|
|
143
|
-
#### Returns
|
|
144
|
-
|
|
145
|
-
`boolean`
|
|
146
|
-
|
|
147
|
-
`true` if `this` and `other` are both `Some` with equal values, or both are `None`, otherwise `false`.
|
|
148
|
-
|
|
149
|
-
#### Defined in
|
|
150
|
-
|
|
151
|
-
[core.ts:295](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L295)
|
|
152
|
-
|
|
153
|
-
***
|
|
154
|
-
|
|
155
|
-
### expect()
|
|
156
|
-
|
|
157
|
-
```ts
|
|
158
|
-
expect(msg): T
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
Returns the contained `Some` value, with a provided error message if the value is a `None`.
|
|
162
|
-
|
|
163
|
-
#### Parameters
|
|
164
|
-
|
|
165
|
-
| Parameter | Type | Description |
|
|
166
|
-
| ------ | ------ | ------ |
|
|
167
|
-
| `msg` | `string` | The error message to provide if the value is a `None`. |
|
|
168
|
-
|
|
169
|
-
#### Returns
|
|
170
|
-
|
|
171
|
-
`T`
|
|
172
|
-
|
|
173
|
-
#### Throws
|
|
174
|
-
|
|
175
|
-
Throws an error with the provided message if the Option is a `None`.
|
|
176
|
-
|
|
177
|
-
#### Defined in
|
|
178
|
-
|
|
179
|
-
[core.ts:83](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L83)
|
|
180
|
-
|
|
181
|
-
***
|
|
182
|
-
|
|
183
|
-
### filter()
|
|
184
|
-
|
|
185
|
-
```ts
|
|
186
|
-
filter(predicate): Option<T>
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
Returns `None` if the Option is `None`, otherwise calls predicate with the wrapped value and returns:
|
|
190
|
-
- `Some(t)` if predicate returns `true` (where `t` is the wrapped value), and
|
|
191
|
-
- `None` if predicate returns `false`.
|
|
192
|
-
|
|
193
|
-
#### Parameters
|
|
194
|
-
|
|
195
|
-
| Parameter | Type | Description |
|
|
196
|
-
| ------ | ------ | ------ |
|
|
197
|
-
| `predicate` | (`value`) => `boolean` | A function that takes the contained value and returns a boolean. |
|
|
198
|
-
|
|
199
|
-
#### Returns
|
|
200
|
-
|
|
201
|
-
[`Option`](Option.md)\<`T`\>
|
|
202
|
-
|
|
203
|
-
#### Defined in
|
|
204
|
-
|
|
205
|
-
[core.ts:150](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L150)
|
|
206
|
-
|
|
207
|
-
***
|
|
208
|
-
|
|
209
|
-
### flatten()
|
|
210
|
-
|
|
211
|
-
```ts
|
|
212
|
-
flatten<T>(this): Option<T>
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
Converts from `Option<Option<T>>` to `Option<T>`.
|
|
216
|
-
|
|
217
|
-
#### Type Parameters
|
|
218
|
-
|
|
219
|
-
| Type Parameter |
|
|
220
|
-
| ------ |
|
|
221
|
-
| `T` |
|
|
222
|
-
|
|
223
|
-
#### Parameters
|
|
224
|
-
|
|
225
|
-
| Parameter | Type |
|
|
226
|
-
| ------ | ------ |
|
|
227
|
-
| `this` | [`Option`](Option.md)\<[`Option`](Option.md)\<`T`\>\> |
|
|
228
|
-
|
|
229
|
-
#### Returns
|
|
230
|
-
|
|
231
|
-
[`Option`](Option.md)\<`T`\>
|
|
232
|
-
|
|
233
|
-
`None` if the Option is `None`, otherwise returns the contained `Option`.
|
|
234
|
-
|
|
235
|
-
#### Defined in
|
|
236
|
-
|
|
237
|
-
[core.ts:156](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L156)
|
|
238
|
-
|
|
239
|
-
***
|
|
240
|
-
|
|
241
|
-
### inspect()
|
|
242
|
-
|
|
243
|
-
```ts
|
|
244
|
-
inspect(fn): this
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
Calls the provided function with the contained value if `this` is `Some`.
|
|
248
|
-
This is primarily for side effects and does not transform the `Option`.
|
|
249
|
-
|
|
250
|
-
#### Parameters
|
|
251
|
-
|
|
252
|
-
| Parameter | Type | Description |
|
|
253
|
-
| ------ | ------ | ------ |
|
|
254
|
-
| `fn` | (`value`) => `void` | A function to call with the contained value. |
|
|
255
|
-
|
|
256
|
-
#### Returns
|
|
257
|
-
|
|
258
|
-
`this`
|
|
259
|
-
|
|
260
|
-
`this`, unmodified, for chaining additional methods.
|
|
261
|
-
|
|
262
|
-
#### Defined in
|
|
263
|
-
|
|
264
|
-
[core.ts:285](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L285)
|
|
265
|
-
|
|
266
|
-
***
|
|
267
|
-
|
|
268
|
-
### isNone()
|
|
269
|
-
|
|
270
|
-
```ts
|
|
271
|
-
isNone(): boolean
|
|
272
|
-
```
|
|
273
|
-
|
|
274
|
-
Returns `true` if the Option is a `None` value.
|
|
275
|
-
|
|
276
|
-
#### Returns
|
|
277
|
-
|
|
278
|
-
`boolean`
|
|
279
|
-
|
|
280
|
-
#### Defined in
|
|
281
|
-
|
|
282
|
-
[core.ts:57](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L57)
|
|
283
|
-
|
|
284
|
-
***
|
|
285
|
-
|
|
286
|
-
### isSome()
|
|
287
|
-
|
|
288
|
-
```ts
|
|
289
|
-
isSome(): boolean
|
|
290
|
-
```
|
|
291
|
-
|
|
292
|
-
Returns `true` if the Option is a `Some` value.
|
|
293
|
-
|
|
294
|
-
#### Returns
|
|
295
|
-
|
|
296
|
-
`boolean`
|
|
297
|
-
|
|
298
|
-
#### Defined in
|
|
299
|
-
|
|
300
|
-
[core.ts:52](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L52)
|
|
301
|
-
|
|
302
|
-
***
|
|
303
|
-
|
|
304
|
-
### isSomeAnd()
|
|
305
|
-
|
|
306
|
-
```ts
|
|
307
|
-
isSomeAnd(predicate): boolean
|
|
308
|
-
```
|
|
309
|
-
|
|
310
|
-
Returns `true` if the Option is a `Some` value and the predicate returns `true` for the contained value.
|
|
311
|
-
|
|
312
|
-
#### Parameters
|
|
313
|
-
|
|
314
|
-
| Parameter | Type | Description |
|
|
315
|
-
| ------ | ------ | ------ |
|
|
316
|
-
| `predicate` | (`value`) => `boolean` | A function that takes the contained value and returns a boolean. |
|
|
317
|
-
|
|
318
|
-
#### Returns
|
|
319
|
-
|
|
320
|
-
`boolean`
|
|
321
|
-
|
|
322
|
-
#### Defined in
|
|
323
|
-
|
|
324
|
-
[core.ts:63](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L63)
|
|
325
|
-
|
|
326
|
-
***
|
|
327
|
-
|
|
328
|
-
### isSomeAndAsync()
|
|
329
|
-
|
|
330
|
-
```ts
|
|
331
|
-
isSomeAndAsync(predicate): Promise<boolean>
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
Asynchronous version of `isSomeAnd`.
|
|
335
|
-
|
|
336
|
-
#### Parameters
|
|
337
|
-
|
|
338
|
-
| Parameter | Type |
|
|
339
|
-
| ------ | ------ |
|
|
340
|
-
| `predicate` | (`value`) => `Promise`\<`boolean`\> |
|
|
341
|
-
|
|
342
|
-
#### Returns
|
|
343
|
-
|
|
344
|
-
`Promise`\<`boolean`\>
|
|
345
|
-
|
|
346
|
-
#### Defined in
|
|
347
|
-
|
|
348
|
-
[core.ts:68](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L68)
|
|
349
|
-
|
|
350
|
-
***
|
|
351
|
-
|
|
352
|
-
### map()
|
|
353
|
-
|
|
354
|
-
```ts
|
|
355
|
-
map<U>(fn): Option<U>
|
|
356
|
-
```
|
|
357
|
-
|
|
358
|
-
Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
|
|
359
|
-
|
|
360
|
-
#### Type Parameters
|
|
361
|
-
|
|
362
|
-
| Type Parameter | Description |
|
|
363
|
-
| ------ | ------ |
|
|
364
|
-
| `U` | The type of the value returned by the map function. |
|
|
365
|
-
|
|
366
|
-
#### Parameters
|
|
367
|
-
|
|
368
|
-
| Parameter | Type | Description |
|
|
369
|
-
| ------ | ------ | ------ |
|
|
370
|
-
| `fn` | (`value`) => `U` | A function that takes the contained value and returns a new value. |
|
|
371
|
-
|
|
372
|
-
#### Returns
|
|
373
|
-
|
|
374
|
-
[`Option`](Option.md)\<`U`\>
|
|
375
|
-
|
|
376
|
-
#### Defined in
|
|
377
|
-
|
|
378
|
-
[core.ts:163](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L163)
|
|
379
|
-
|
|
380
|
-
***
|
|
381
|
-
|
|
382
|
-
### mapOr()
|
|
383
|
-
|
|
384
|
-
```ts
|
|
385
|
-
mapOr<U>(defaultValue, fn): U
|
|
386
|
-
```
|
|
387
|
-
|
|
388
|
-
Maps an `Option<T>` to `U` by applying a function to the contained value (if any), or returns the provided default (if not).
|
|
389
|
-
|
|
390
|
-
#### Type Parameters
|
|
391
|
-
|
|
392
|
-
| Type Parameter | Description |
|
|
393
|
-
| ------ | ------ |
|
|
394
|
-
| `U` | The type of the value returned by the map function or the default value. |
|
|
395
|
-
|
|
396
|
-
#### Parameters
|
|
397
|
-
|
|
398
|
-
| Parameter | Type | Description |
|
|
399
|
-
| ------ | ------ | ------ |
|
|
400
|
-
| `defaultValue` | `U` | The value to return if the Option is `None`. |
|
|
401
|
-
| `fn` | (`value`) => `U` | A function that takes the contained value and returns a new value. |
|
|
402
|
-
|
|
403
|
-
#### Returns
|
|
404
|
-
|
|
405
|
-
`U`
|
|
406
|
-
|
|
407
|
-
#### Defined in
|
|
408
|
-
|
|
409
|
-
[core.ts:171](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L171)
|
|
410
|
-
|
|
411
|
-
***
|
|
412
|
-
|
|
413
|
-
### mapOrElse()
|
|
414
|
-
|
|
415
|
-
```ts
|
|
416
|
-
mapOrElse<U>(defaultFn, fn): U
|
|
417
|
-
```
|
|
418
|
-
|
|
419
|
-
Maps an `Option<T>` to `U` by applying a function to a contained value (if any), or computes a default (if not).
|
|
420
|
-
|
|
421
|
-
#### Type Parameters
|
|
422
|
-
|
|
423
|
-
| Type Parameter | Description |
|
|
424
|
-
| ------ | ------ |
|
|
425
|
-
| `U` | The type of the value returned by the map function or the default function. |
|
|
426
|
-
|
|
427
|
-
#### Parameters
|
|
428
|
-
|
|
429
|
-
| Parameter | Type | Description |
|
|
430
|
-
| ------ | ------ | ------ |
|
|
431
|
-
| `defaultFn` | () => `U` | A function that returns the default value. |
|
|
432
|
-
| `fn` | (`value`) => `U` | A function that takes the contained value and returns a new value. |
|
|
433
|
-
|
|
434
|
-
#### Returns
|
|
435
|
-
|
|
436
|
-
`U`
|
|
437
|
-
|
|
438
|
-
#### Defined in
|
|
439
|
-
|
|
440
|
-
[core.ts:179](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L179)
|
|
441
|
-
|
|
442
|
-
***
|
|
443
|
-
|
|
444
|
-
### okOr()
|
|
445
|
-
|
|
446
|
-
```ts
|
|
447
|
-
okOr<E>(error): Result<T, E>
|
|
448
|
-
```
|
|
449
|
-
|
|
450
|
-
Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
|
|
451
|
-
|
|
452
|
-
#### Type Parameters
|
|
453
|
-
|
|
454
|
-
| Type Parameter | Description |
|
|
455
|
-
| ------ | ------ |
|
|
456
|
-
| `E` | The type of the error value in the `Err` variant of the resulting `Result`. |
|
|
457
|
-
|
|
458
|
-
#### Parameters
|
|
459
|
-
|
|
460
|
-
| Parameter | Type | Description |
|
|
461
|
-
| ------ | ------ | ------ |
|
|
462
|
-
| `error` | `E` | The error value to use if the Option is a `None`. |
|
|
463
|
-
|
|
464
|
-
#### Returns
|
|
465
|
-
|
|
466
|
-
[`Result`](Result.md)\<`T`, `E`\>
|
|
467
|
-
|
|
468
|
-
#### Defined in
|
|
469
|
-
|
|
470
|
-
[core.ts:121](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L121)
|
|
471
|
-
|
|
472
|
-
***
|
|
473
|
-
|
|
474
|
-
### okOrElse()
|
|
475
|
-
|
|
476
|
-
```ts
|
|
477
|
-
okOrElse<E>(err): Result<T, E>
|
|
478
|
-
```
|
|
479
|
-
|
|
480
|
-
Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
|
|
481
|
-
|
|
482
|
-
#### Type Parameters
|
|
483
|
-
|
|
484
|
-
| Type Parameter | Description |
|
|
485
|
-
| ------ | ------ |
|
|
486
|
-
| `E` | The type of the error value in the `Err` variant of the resulting `Result`. |
|
|
487
|
-
|
|
488
|
-
#### Parameters
|
|
489
|
-
|
|
490
|
-
| Parameter | Type | Description |
|
|
491
|
-
| ------ | ------ | ------ |
|
|
492
|
-
| `err` | () => `E` | A function that returns the error value. |
|
|
493
|
-
|
|
494
|
-
#### Returns
|
|
495
|
-
|
|
496
|
-
[`Result`](Result.md)\<`T`, `E`\>
|
|
497
|
-
|
|
498
|
-
#### Defined in
|
|
499
|
-
|
|
500
|
-
[core.ts:128](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L128)
|
|
501
|
-
|
|
502
|
-
***
|
|
503
|
-
|
|
504
|
-
### or()
|
|
505
|
-
|
|
506
|
-
```ts
|
|
507
|
-
or(other): Option<T>
|
|
508
|
-
```
|
|
509
|
-
|
|
510
|
-
Returns the Option if it contains a value, otherwise returns `other`.
|
|
511
|
-
This can be used for providing a fallback `Option`.
|
|
512
|
-
|
|
513
|
-
#### Parameters
|
|
514
|
-
|
|
515
|
-
| Parameter | Type | Description |
|
|
516
|
-
| ------ | ------ | ------ |
|
|
517
|
-
| `other` | [`Option`](Option.md)\<`T`\> | The fallback `Option` to use if `this` is `None`. |
|
|
518
|
-
|
|
519
|
-
#### Returns
|
|
520
|
-
|
|
521
|
-
[`Option`](Option.md)\<`T`\>
|
|
522
|
-
|
|
523
|
-
`this` if it is `Some`, otherwise `other`.
|
|
524
|
-
|
|
525
|
-
#### Defined in
|
|
526
|
-
|
|
527
|
-
[core.ts:254](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L254)
|
|
528
|
-
|
|
529
|
-
***
|
|
530
|
-
|
|
531
|
-
### orElse()
|
|
532
|
-
|
|
533
|
-
```ts
|
|
534
|
-
orElse(fn): Option<T>
|
|
535
|
-
```
|
|
536
|
-
|
|
537
|
-
Returns the Option if it contains a value, otherwise calls `fn` and returns the result.
|
|
538
|
-
This method can be used for lazy fallbacks, as `fn` is only evaluated if `this` is `None`.
|
|
539
|
-
|
|
540
|
-
#### Parameters
|
|
541
|
-
|
|
542
|
-
| Parameter | Type | Description |
|
|
543
|
-
| ------ | ------ | ------ |
|
|
544
|
-
| `fn` | () => [`Option`](Option.md)\<`T`\> | A function that produces an `Option`. |
|
|
545
|
-
|
|
546
|
-
#### Returns
|
|
547
|
-
|
|
548
|
-
[`Option`](Option.md)\<`T`\>
|
|
549
|
-
|
|
550
|
-
`this` if it is `Some`, otherwise the result of `fn`.
|
|
551
|
-
|
|
552
|
-
#### Defined in
|
|
553
|
-
|
|
554
|
-
[core.ts:262](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L262)
|
|
555
|
-
|
|
556
|
-
***
|
|
557
|
-
|
|
558
|
-
### orElseAsync()
|
|
559
|
-
|
|
560
|
-
```ts
|
|
561
|
-
orElseAsync(fn): AsyncOption<T>
|
|
562
|
-
```
|
|
563
|
-
|
|
564
|
-
Asynchronous version of `orElse`.
|
|
565
|
-
|
|
566
|
-
#### Parameters
|
|
567
|
-
|
|
568
|
-
| Parameter | Type |
|
|
569
|
-
| ------ | ------ |
|
|
570
|
-
| `fn` | () => [`AsyncOption`](../type-aliases/AsyncOption.md)\<`T`\> |
|
|
571
|
-
|
|
572
|
-
#### Returns
|
|
573
|
-
|
|
574
|
-
[`AsyncOption`](../type-aliases/AsyncOption.md)\<`T`\>
|
|
575
|
-
|
|
576
|
-
#### Defined in
|
|
577
|
-
|
|
578
|
-
[core.ts:267](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L267)
|
|
579
|
-
|
|
580
|
-
***
|
|
581
|
-
|
|
582
|
-
### toString()
|
|
583
|
-
|
|
584
|
-
```ts
|
|
585
|
-
toString(): string
|
|
586
|
-
```
|
|
587
|
-
|
|
588
|
-
Custom `toString` implementation that uses the `Option`'s contained value.
|
|
589
|
-
|
|
590
|
-
#### Returns
|
|
591
|
-
|
|
592
|
-
`string`
|
|
593
|
-
|
|
594
|
-
#### Defined in
|
|
595
|
-
|
|
596
|
-
[core.ts:302](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L302)
|
|
597
|
-
|
|
598
|
-
***
|
|
599
|
-
|
|
600
|
-
### transpose()
|
|
601
|
-
|
|
602
|
-
```ts
|
|
603
|
-
transpose<T, E>(this): Result<Option<T>, E>
|
|
604
|
-
```
|
|
605
|
-
|
|
606
|
-
Transposes an `Option` of a `Result` into a `Result` of an `Option`.
|
|
607
|
-
|
|
608
|
-
#### Type Parameters
|
|
609
|
-
|
|
610
|
-
| Type Parameter | Description |
|
|
611
|
-
| ------ | ------ |
|
|
612
|
-
| `T` | The type of the success value in the `Ok` variant of the `Result`. |
|
|
613
|
-
| `E` | The type of the error value in the `Err` variant of the `Result`. |
|
|
614
|
-
|
|
615
|
-
#### Parameters
|
|
616
|
-
|
|
617
|
-
| Parameter | Type |
|
|
618
|
-
| ------ | ------ |
|
|
619
|
-
| `this` | [`Option`](Option.md)\<[`Result`](Result.md)\<`T`, `E`\>\> |
|
|
620
|
-
|
|
621
|
-
#### Returns
|
|
622
|
-
|
|
623
|
-
[`Result`](Result.md)\<[`Option`](Option.md)\<`T`\>, `E`\>
|
|
624
|
-
|
|
625
|
-
`Ok` containing `Some` if the Option is a `Some` containing `Ok`,
|
|
626
|
-
`Err` containing the error if the Option is a `Some` containing `Err`,
|
|
627
|
-
`Ok` containing `None` if the Option is `None`.
|
|
628
|
-
|
|
629
|
-
#### Defined in
|
|
630
|
-
|
|
631
|
-
[core.ts:138](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L138)
|
|
632
|
-
|
|
633
|
-
***
|
|
634
|
-
|
|
635
|
-
### unwrap()
|
|
636
|
-
|
|
637
|
-
```ts
|
|
638
|
-
unwrap(): T
|
|
639
|
-
```
|
|
640
|
-
|
|
641
|
-
Returns the contained `Some` value.
|
|
642
|
-
|
|
643
|
-
#### Returns
|
|
644
|
-
|
|
645
|
-
`T`
|
|
646
|
-
|
|
647
|
-
#### Throws
|
|
648
|
-
|
|
649
|
-
Throws an error if the value is a `None`.
|
|
650
|
-
|
|
651
|
-
#### Defined in
|
|
652
|
-
|
|
653
|
-
[core.ts:89](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L89)
|
|
654
|
-
|
|
655
|
-
***
|
|
656
|
-
|
|
657
|
-
### unwrapOr()
|
|
658
|
-
|
|
659
|
-
```ts
|
|
660
|
-
unwrapOr(defaultValue): T
|
|
661
|
-
```
|
|
662
|
-
|
|
663
|
-
Returns the contained `Some` value or a provided default.
|
|
664
|
-
|
|
665
|
-
#### Parameters
|
|
666
|
-
|
|
667
|
-
| Parameter | Type | Description |
|
|
668
|
-
| ------ | ------ | ------ |
|
|
669
|
-
| `defaultValue` | `T` | The value to return if the Option is a `None`. |
|
|
670
|
-
|
|
671
|
-
#### Returns
|
|
672
|
-
|
|
673
|
-
`T`
|
|
674
|
-
|
|
675
|
-
#### Defined in
|
|
676
|
-
|
|
677
|
-
[core.ts:95](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L95)
|
|
678
|
-
|
|
679
|
-
***
|
|
680
|
-
|
|
681
|
-
### unwrapOrElse()
|
|
682
|
-
|
|
683
|
-
```ts
|
|
684
|
-
unwrapOrElse(fn): T
|
|
685
|
-
```
|
|
686
|
-
|
|
687
|
-
Returns the contained `Some` value or computes it from a closure.
|
|
688
|
-
|
|
689
|
-
#### Parameters
|
|
690
|
-
|
|
691
|
-
| Parameter | Type | Description |
|
|
692
|
-
| ------ | ------ | ------ |
|
|
693
|
-
| `fn` | () => `T` | A function that returns the default value. |
|
|
694
|
-
|
|
695
|
-
#### Returns
|
|
696
|
-
|
|
697
|
-
`T`
|
|
698
|
-
|
|
699
|
-
#### Defined in
|
|
700
|
-
|
|
701
|
-
[core.ts:101](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L101)
|
|
702
|
-
|
|
703
|
-
***
|
|
704
|
-
|
|
705
|
-
### unwrapOrElseAsync()
|
|
706
|
-
|
|
707
|
-
```ts
|
|
708
|
-
unwrapOrElseAsync(fn): Promise<T>
|
|
709
|
-
```
|
|
710
|
-
|
|
711
|
-
Asynchronous version of `unwrapOrElse`.
|
|
712
|
-
|
|
713
|
-
#### Parameters
|
|
714
|
-
|
|
715
|
-
| Parameter | Type |
|
|
716
|
-
| ------ | ------ |
|
|
717
|
-
| `fn` | () => `Promise`\<`T`\> |
|
|
718
|
-
|
|
719
|
-
#### Returns
|
|
720
|
-
|
|
721
|
-
`Promise`\<`T`\>
|
|
722
|
-
|
|
723
|
-
#### Defined in
|
|
724
|
-
|
|
725
|
-
[core.ts:106](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L106)
|
|
726
|
-
|
|
727
|
-
***
|
|
728
|
-
|
|
729
|
-
### unzip()
|
|
730
|
-
|
|
731
|
-
```ts
|
|
732
|
-
unzip<T, U>(this): [Option<T>, Option<U>]
|
|
733
|
-
```
|
|
734
|
-
|
|
735
|
-
Converts from `Option<[T, U]>` to `[Option<T>, Option<U>]`.
|
|
736
|
-
If `this` is `Some([a, b])`, returns `[Some(a), Some(b)]`.
|
|
737
|
-
If `this` is `None`, returns `[None, None]`.
|
|
738
|
-
|
|
739
|
-
#### Type Parameters
|
|
740
|
-
|
|
741
|
-
| Type Parameter | Description |
|
|
742
|
-
| ------ | ------ |
|
|
743
|
-
| `T` | The type of the first value in the tuple. |
|
|
744
|
-
| `U` | The type of the second value in the tuple. |
|
|
745
|
-
|
|
746
|
-
#### Parameters
|
|
747
|
-
|
|
748
|
-
| Parameter | Type |
|
|
749
|
-
| ------ | ------ |
|
|
750
|
-
| `this` | [`Option`](Option.md)\<[`T`, `U`]\> |
|
|
751
|
-
|
|
752
|
-
#### Returns
|
|
753
|
-
|
|
754
|
-
[[`Option`](Option.md)\<`T`\>, [`Option`](Option.md)\<`U`\>]
|
|
755
|
-
|
|
756
|
-
A tuple of `Options`, one for each element in the original `Option` of a tuple.
|
|
757
|
-
|
|
758
|
-
#### Defined in
|
|
759
|
-
|
|
760
|
-
[core.ts:215](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L215)
|
|
761
|
-
|
|
762
|
-
***
|
|
763
|
-
|
|
764
|
-
### xor()
|
|
765
|
-
|
|
766
|
-
```ts
|
|
767
|
-
xor(other): Option<T>
|
|
768
|
-
```
|
|
769
|
-
|
|
770
|
-
Returns `Some` if exactly one of `this`, `other` is `Some`, otherwise returns `None`.
|
|
771
|
-
This can be thought of as an exclusive or operation on `Option` values.
|
|
772
|
-
|
|
773
|
-
#### Parameters
|
|
774
|
-
|
|
775
|
-
| Parameter | Type | Description |
|
|
776
|
-
| ------ | ------ | ------ |
|
|
777
|
-
| `other` | [`Option`](Option.md)\<`T`\> | The other `Option` to compare with. |
|
|
778
|
-
|
|
779
|
-
#### Returns
|
|
780
|
-
|
|
781
|
-
[`Option`](Option.md)\<`T`\>
|
|
782
|
-
|
|
783
|
-
`Some` if exactly one of `this` and `other` is `Some`, otherwise `None`.
|
|
784
|
-
|
|
785
|
-
#### Defined in
|
|
786
|
-
|
|
787
|
-
[core.ts:275](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L275)
|
|
788
|
-
|
|
789
|
-
***
|
|
790
|
-
|
|
791
|
-
### zip()
|
|
792
|
-
|
|
793
|
-
```ts
|
|
794
|
-
zip<U>(other): Option<[T, U]>
|
|
795
|
-
```
|
|
796
|
-
|
|
797
|
-
Combines `this` with another `Option` by zipping their contained values.
|
|
798
|
-
If `this` is `Some(s)` and `other` is `Some(o)`, returns `Some([s, o])`.
|
|
799
|
-
If either `this` or `other` is `None`, returns `None`.
|
|
800
|
-
|
|
801
|
-
#### Type Parameters
|
|
802
|
-
|
|
803
|
-
| Type Parameter | Description |
|
|
804
|
-
| ------ | ------ |
|
|
805
|
-
| `U` | The type of the value in the other `Option`. |
|
|
806
|
-
|
|
807
|
-
#### Parameters
|
|
808
|
-
|
|
809
|
-
| Parameter | Type | Description |
|
|
810
|
-
| ------ | ------ | ------ |
|
|
811
|
-
| `other` | [`Option`](Option.md)\<`U`\> | The other `Option` to zip with. |
|
|
812
|
-
|
|
813
|
-
#### Returns
|
|
814
|
-
|
|
815
|
-
[`Option`](Option.md)\<[`T`, `U`]\>
|
|
816
|
-
|
|
817
|
-
An `Option` containing a tuple of the values if both are `Some`, otherwise `None`.
|
|
818
|
-
|
|
819
|
-
#### Defined in
|
|
820
|
-
|
|
821
|
-
[core.ts:193](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L193)
|
|
822
|
-
|
|
823
|
-
***
|
|
824
|
-
|
|
825
|
-
### zipWith()
|
|
826
|
-
|
|
827
|
-
```ts
|
|
828
|
-
zipWith<U, R>(other, fn): Option<R>
|
|
829
|
-
```
|
|
830
|
-
|
|
831
|
-
Zips `this` with another `Option` using a provided function to combine their contained values.
|
|
832
|
-
If `this` is `Some(s)` and `other` is `Some(o)`, returns `Some(fn(s, o))`.
|
|
833
|
-
If either `this` or `other` is `None`, returns `None`.
|
|
834
|
-
|
|
835
|
-
#### Type Parameters
|
|
836
|
-
|
|
837
|
-
| Type Parameter | Description |
|
|
838
|
-
| ------ | ------ |
|
|
839
|
-
| `U` | The type of the value in the other `Option`. |
|
|
840
|
-
| `R` | The return type of the combining function. |
|
|
841
|
-
|
|
842
|
-
#### Parameters
|
|
843
|
-
|
|
844
|
-
| Parameter | Type | Description |
|
|
845
|
-
| ------ | ------ | ------ |
|
|
846
|
-
| `other` | [`Option`](Option.md)\<`U`\> | The other `Option` to zip with. |
|
|
847
|
-
| `fn` | (`value`, `otherValue`) => `R` | The function to combine the values from both `Options`. |
|
|
848
|
-
|
|
849
|
-
#### Returns
|
|
850
|
-
|
|
851
|
-
[`Option`](Option.md)\<`R`\>
|
|
852
|
-
|
|
853
|
-
An `Option` containing the result of `fn` if both `Options` are `Some`, otherwise `None`.
|
|
854
|
-
|
|
855
|
-
#### Defined in
|
|
856
|
-
|
|
857
|
-
[core.ts:205](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/core.ts#L205)
|