happy-rusty 1.0.9 → 1.1.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/README.cn.md +15 -35
- package/README.md +15 -35
- package/dist/main.cjs +198 -19
- package/dist/main.cjs.map +1 -1
- package/dist/main.mjs +198 -20
- package/dist/main.mjs.map +1 -1
- package/dist/types.d.ts +521 -104
- package/docs/functions/Err.md +46 -0
- package/docs/functions/Ok.md +46 -0
- package/docs/functions/Some.md +45 -0
- package/docs/functions/promiseToResult.md +50 -0
- package/docs/index.md +37 -0
- package/docs/interfaces/None.md +827 -0
- package/docs/interfaces/Option.md +743 -0
- package/docs/interfaces/Result.md +702 -0
- package/docs/type-aliases/AsyncIOResult.md +24 -0
- package/docs/type-aliases/AsyncOption.md +24 -0
- package/docs/type-aliases/AsyncResult.md +25 -0
- package/docs/type-aliases/IOResult.md +24 -0
- package/docs/variables/None.md +18 -0
- package/package.json +16 -10
- package/src/enum/prelude.ts +965 -0
- package/src/mod.ts +13 -27
- package/src/enum/option.ts +0 -68
- package/src/enum/result.ts +0 -104
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[**happy-rusty**](../index.md) • **Docs**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[happy-rusty](../index.md) / Err
|
|
6
|
+
|
|
7
|
+
# Function: Err()
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
function Err<T, E>(error): Result<T, E>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Creates a `Result<T, E>` representing a failed outcome containing an error.
|
|
14
|
+
This function is used to construct a `Result` that signifies the operation failed by containing the error `E`.
|
|
15
|
+
|
|
16
|
+
## Type parameters
|
|
17
|
+
|
|
18
|
+
| Type parameter | Description |
|
|
19
|
+
| :------ | :------ |
|
|
20
|
+
| `T` | The type of the value that the result could potentially contain (not used in this case). |
|
|
21
|
+
| `E` | The type of the error to be wrapped in the `Err` result. |
|
|
22
|
+
|
|
23
|
+
## Parameters
|
|
24
|
+
|
|
25
|
+
| Parameter | Type | Description |
|
|
26
|
+
| :------ | :------ | :------ |
|
|
27
|
+
| `error` | `E` | The error to wrap as an `Err` result. |
|
|
28
|
+
|
|
29
|
+
## Returns
|
|
30
|
+
|
|
31
|
+
[`Result`](../interfaces/Result.md)\<`T`, `E`\>
|
|
32
|
+
|
|
33
|
+
A `Result<T, E>` that contains the provided error, representing the `Err` case.
|
|
34
|
+
|
|
35
|
+
## Example
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
const badResult = Err<number, Error>(new Error('Something went wrong'));
|
|
39
|
+
if (badResult.isErr()) {
|
|
40
|
+
console.error(badResult.unwrapErr()); // Outputs: Error: Something went wrong
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Source
|
|
45
|
+
|
|
46
|
+
[enum/prelude.ts:855](https://github.com/JiangJie/happy-rusty/blob/15ed105e08c6cc3943e22243c9386336a521d83e/src/enum/prelude.ts#L855)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[**happy-rusty**](../index.md) • **Docs**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[happy-rusty](../index.md) / Ok
|
|
6
|
+
|
|
7
|
+
# Function: Ok()
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
function Ok<T, E>(value): Result<T, E>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Creates a `Result<T, E>` representing a successful outcome containing a value.
|
|
14
|
+
This function is used to construct a `Result` that signifies the operation was successful by containing the value `T`.
|
|
15
|
+
|
|
16
|
+
## Type parameters
|
|
17
|
+
|
|
18
|
+
| Type parameter | Description |
|
|
19
|
+
| :------ | :------ |
|
|
20
|
+
| `T` | The type of the value to be contained in the `Ok` result. |
|
|
21
|
+
| `E` | The type of the error that the result could potentially contain (not used in this case). |
|
|
22
|
+
|
|
23
|
+
## Parameters
|
|
24
|
+
|
|
25
|
+
| Parameter | Type | Description |
|
|
26
|
+
| :------ | :------ | :------ |
|
|
27
|
+
| `value` | `T` | The value to wrap as an `Ok` result. |
|
|
28
|
+
|
|
29
|
+
## Returns
|
|
30
|
+
|
|
31
|
+
[`Result`](../interfaces/Result.md)\<`T`, `E`\>
|
|
32
|
+
|
|
33
|
+
A `Result<T, E>` that contains the provided value, representing the `Ok` case.
|
|
34
|
+
|
|
35
|
+
## Example
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
const goodResult = Ok<number, Error>(1); // Result<number, Error> with a value
|
|
39
|
+
if (goodResult.isOk()) {
|
|
40
|
+
console.log(goodResult.unwrap()); // Outputs: 1
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Source
|
|
45
|
+
|
|
46
|
+
[enum/prelude.ts:776](https://github.com/JiangJie/happy-rusty/blob/15ed105e08c6cc3943e22243c9386336a521d83e/src/enum/prelude.ts#L776)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
[**happy-rusty**](../index.md) • **Docs**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[happy-rusty](../index.md) / Some
|
|
6
|
+
|
|
7
|
+
# Function: Some()
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
function Some<T>(value): Option<T>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Creates an `Option<T>` representing the presence of a value.
|
|
14
|
+
This function is typically used to construct an `Option` that contains a value, indicating that the operation yielding the value was successful.
|
|
15
|
+
|
|
16
|
+
## Type parameters
|
|
17
|
+
|
|
18
|
+
| Type parameter | Description |
|
|
19
|
+
| :------ | :------ |
|
|
20
|
+
| `T` | The type of the value to be wrapped in a `Some`. |
|
|
21
|
+
|
|
22
|
+
## Parameters
|
|
23
|
+
|
|
24
|
+
| Parameter | Type | Description |
|
|
25
|
+
| :------ | :------ | :------ |
|
|
26
|
+
| `value` | `T` | The value to wrap as a `Some` option. |
|
|
27
|
+
|
|
28
|
+
## Returns
|
|
29
|
+
|
|
30
|
+
[`Option`](../interfaces/Option.md)\<`T`\>
|
|
31
|
+
|
|
32
|
+
An `Option<T>` that contains the provided value, representing the `Some` case.
|
|
33
|
+
|
|
34
|
+
## Example
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
const maybeValue = Some(1); // Option<number> with a value
|
|
38
|
+
if (maybeValue.isSome()) {
|
|
39
|
+
console.log(maybeValue.unwrap()); // Outputs: 1
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Source
|
|
44
|
+
|
|
45
|
+
[enum/prelude.ts:627](https://github.com/JiangJie/happy-rusty/blob/15ed105e08c6cc3943e22243c9386336a521d83e/src/enum/prelude.ts#L627)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
[**happy-rusty**](../index.md) • **Docs**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[happy-rusty](../index.md) / promiseToResult
|
|
6
|
+
|
|
7
|
+
# Function: promiseToResult()
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
function promiseToResult<T, E>(p): Promise<Result<T, E>>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Converts a Promise to a Result type, capturing the resolved value in an `Ok`, or the error in an `Err`.
|
|
14
|
+
This allows for promise-based asynchronous operations to be handled in a way that is more in line with the Result pattern.
|
|
15
|
+
|
|
16
|
+
## Type parameters
|
|
17
|
+
|
|
18
|
+
| Type parameter | Value | Description |
|
|
19
|
+
| :------ | :------ | :------ |
|
|
20
|
+
| `T` | - | The type of the value that the promise resolves to. |
|
|
21
|
+
| `E` | `Error` | The type of the error that the promise may reject with, defaults to `Error`. |
|
|
22
|
+
|
|
23
|
+
## Parameters
|
|
24
|
+
|
|
25
|
+
| Parameter | Type | Description |
|
|
26
|
+
| :------ | :------ | :------ |
|
|
27
|
+
| `p` | `Promise`\<`T`\> | The promise to convert into a `Result` type. |
|
|
28
|
+
|
|
29
|
+
## Returns
|
|
30
|
+
|
|
31
|
+
`Promise`\<[`Result`](../interfaces/Result.md)\<`T`, `E`\>\>
|
|
32
|
+
|
|
33
|
+
A promise that resolves to a `Result<T, E>`. If the input promise `p` resolves, the resulting promise will resolve with `Ok<T>`. If the input promise `p` rejects, the resulting promise will resolve with `Err<E>`.
|
|
34
|
+
|
|
35
|
+
## Example
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
async function example() {
|
|
39
|
+
const result = await promiseToResult(fetchData());
|
|
40
|
+
if (result.isOk()) {
|
|
41
|
+
console.log('Data:', result.unwrap());
|
|
42
|
+
} else {
|
|
43
|
+
console.error('Error:', result.unwrapErr());
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Source
|
|
49
|
+
|
|
50
|
+
[enum/prelude.ts:959](https://github.com/JiangJie/happy-rusty/blob/15ed105e08c6cc3943e22243c9386336a521d83e/src/enum/prelude.ts#L959)
|
package/docs/index.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
**happy-rusty** • **Docs**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
# happy-rusty
|
|
6
|
+
|
|
7
|
+
## Interfaces
|
|
8
|
+
|
|
9
|
+
| Interface | Description |
|
|
10
|
+
| :------ | :------ |
|
|
11
|
+
| [None](interfaces/None.md) | Represents the absence of a value, as a specialized `Option` type. The type parameter is set to `never` because `None` does not hold a value. |
|
|
12
|
+
| [Option](interfaces/Option.md) | Type `Option` represents an optional value: every `Option` is either `Some` and contains a value, or `None`, and does not. This interface includes methods that act on the `Option` type, similar to Rust's `Option` enum. |
|
|
13
|
+
| [Result](interfaces/Result.md) | The `Result` type is used for returning and propagating errors. 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. This interface includes methods that act on the `Result` type, similar to Rust's `Result` enum. |
|
|
14
|
+
|
|
15
|
+
## Type Aliases
|
|
16
|
+
|
|
17
|
+
| Type alias | Description |
|
|
18
|
+
| :------ | :------ |
|
|
19
|
+
| [AsyncIOResult](type-aliases/AsyncIOResult.md) | Represents an asynchronous I/O operation that yields a `Result<T, Error>`. This is a promise that resolves to `Ok(T)` if the I/O operation was successful, or `Err(Error)` if there was an error. |
|
|
20
|
+
| [AsyncOption](type-aliases/AsyncOption.md) | Represents an asynchronous operation that yields an `Option<T>`. This is a promise that resolves to either `Some(T)` if the value is present, or `None` if the value is absent. |
|
|
21
|
+
| [AsyncResult](type-aliases/AsyncResult.md) | Represents an asynchronous operation that yields a `Result<T, E>`. This is a promise that resolves to `Ok(T)` if the operation was successful, or `Err(E)` if there was an error. |
|
|
22
|
+
| [IOResult](type-aliases/IOResult.md) | Represents a synchronous operation that yields a `Result<T, Error>`. This is a result that is either `Ok(T)` if the operation was successful, or `Err(Error)` if there was an error. |
|
|
23
|
+
|
|
24
|
+
## Variables
|
|
25
|
+
|
|
26
|
+
| Variable | Description |
|
|
27
|
+
| :------ | :------ |
|
|
28
|
+
| [None](variables/None.md) | A constant representing the `None` case of an `Option`, indicating the absence of a value. This constant is frozen to ensure it is immutable and cannot be altered, preserving the integrity of `None` throughout the application. |
|
|
29
|
+
|
|
30
|
+
## Functions
|
|
31
|
+
|
|
32
|
+
| Function | Description |
|
|
33
|
+
| :------ | :------ |
|
|
34
|
+
| [Err](functions/Err.md) | Creates a `Result<T, E>` representing a failed outcome containing an error. This function is used to construct a `Result` that signifies the operation failed by containing the error `E`. |
|
|
35
|
+
| [Ok](functions/Ok.md) | Creates a `Result<T, E>` representing a successful outcome containing a value. This function is used to construct a `Result` that signifies the operation was successful by containing the value `T`. |
|
|
36
|
+
| [Some](functions/Some.md) | Creates an `Option<T>` representing the presence of a value. This function is typically used to construct an `Option` that contains a value, indicating that the operation yielding the value was successful. |
|
|
37
|
+
| [promiseToResult](functions/promiseToResult.md) | Converts a Promise to a Result type, capturing the resolved value in an `Ok`, or the error in an `Err`. This allows for promise-based asynchronous operations to be handled in a way that is more in line with the Result pattern. |
|