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.
Files changed (43) hide show
  1. package/CHANGELOG.md +214 -0
  2. package/LICENSE +21 -674
  3. package/README.cn.md +265 -19
  4. package/README.md +261 -21
  5. package/dist/main.cjs +382 -32
  6. package/dist/main.cjs.map +1 -1
  7. package/dist/main.mjs +374 -33
  8. package/dist/main.mjs.map +1 -1
  9. package/dist/types.d.ts +2002 -52
  10. package/package.json +38 -25
  11. package/dist/types.d.ts.map +0 -1
  12. package/docs/README.md +0 -47
  13. package/docs/functions/Err.md +0 -46
  14. package/docs/functions/Ok.md +0 -70
  15. package/docs/functions/Some.md +0 -45
  16. package/docs/functions/isOption.md +0 -35
  17. package/docs/functions/isResult.md +0 -36
  18. package/docs/functions/promiseToAsyncResult.md +0 -50
  19. package/docs/interfaces/None.md +0 -979
  20. package/docs/interfaces/Option.md +0 -857
  21. package/docs/interfaces/Result.md +0 -903
  22. package/docs/type-aliases/AsyncIOResult.md +0 -24
  23. package/docs/type-aliases/AsyncOption.md +0 -24
  24. package/docs/type-aliases/AsyncResult.md +0 -25
  25. package/docs/type-aliases/AsyncVoidIOResult.md +0 -17
  26. package/docs/type-aliases/AsyncVoidResult.md +0 -23
  27. package/docs/type-aliases/IOResult.md +0 -24
  28. package/docs/type-aliases/VoidIOResult.md +0 -17
  29. package/docs/type-aliases/VoidResult.md +0 -23
  30. package/docs/variables/None.md +0 -18
  31. package/docs/variables/RESULT_FALSE.md +0 -18
  32. package/docs/variables/RESULT_TRUE.md +0 -18
  33. package/docs/variables/RESULT_VOID.md +0 -17
  34. package/docs/variables/RESULT_ZERO.md +0 -18
  35. package/src/enum/constants.ts +0 -30
  36. package/src/enum/core.ts +0 -635
  37. package/src/enum/defines.ts +0 -45
  38. package/src/enum/extensions.ts +0 -31
  39. package/src/enum/mod.ts +0 -6
  40. package/src/enum/prelude.ts +0 -619
  41. package/src/enum/symbols.ts +0 -9
  42. package/src/enum/utils.ts +0 -27
  43. package/src/mod.ts +0 -1
package/package.json CHANGED
@@ -1,33 +1,39 @@
1
1
  {
2
2
  "name": "happy-rusty",
3
- "description": "Porting some excellent design implementations from Rust to JavaScript.",
3
+ "description": "Rust's Option, Result, and sync primitives for JavaScript/TypeScript - Better error handling and null-safety patterns.",
4
4
  "author": "jiang115jie@gmail.com",
5
- "license": "GPL-3.0",
6
- "version": "1.5.0",
5
+ "license": "MIT",
6
+ "version": "1.6.1",
7
7
  "type": "module",
8
8
  "source": "src/mod.ts",
9
9
  "main": "dist/main.cjs",
10
10
  "module": "dist/main.mjs",
11
11
  "types": "dist/types.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/types.d.ts",
15
+ "import": "./dist/main.mjs",
16
+ "require": "./dist/main.cjs",
17
+ "default": "./dist/main.mjs"
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
12
21
  "files": [
13
22
  "LICENSE",
14
23
  "README.md",
15
24
  "README.cn.md",
16
- "package.json",
17
- "docs",
18
- "src",
25
+ "CHANGELOG.md",
19
26
  "dist"
20
27
  ],
21
28
  "sideEffects": false,
22
29
  "scripts": {
23
30
  "check": "pnpm exec tsc --noEmit",
24
31
  "lint": "pnpm exec eslint .",
25
- "prebuild": "pnpm dlx rimraf dist && pnpm run check && pnpm run lint",
26
- "build": "pnpm exec rollup --config rollup.config.mjs",
27
- "pretest": "pnpm dlx rimraf coverage",
28
- "test": "deno test --coverage && deno coverage coverage && deno coverage coverage --lcov --output=coverage/cov_profile.lcov",
29
- "pretest:html": "pnpm run pretest",
30
- "test:html": "deno test --coverage && deno coverage coverage && deno coverage coverage --html",
32
+ "prebuild": "pnpm run check && pnpm run lint",
33
+ "build": "pnpm exec vite build && pnpm exec rollup --config rollup.config.ts",
34
+ "test": "pnpm exec vitest run --coverage",
35
+ "test:watch": "pnpm exec vitest",
36
+ "test:ui": "pnpm exec vitest --ui",
31
37
  "predocs": "pnpm dlx rimraf docs",
32
38
  "docs": "pnpm exec typedoc",
33
39
  "eg": "deno run -A examples/main.ts",
@@ -39,25 +45,32 @@
39
45
  },
40
46
  "keywords": [
41
47
  "rust",
48
+ "typescript",
42
49
  "enum",
43
50
  "Option",
44
51
  "Some",
45
52
  "None",
46
53
  "Result",
47
54
  "Ok",
48
- "Err"
55
+ "Err",
56
+ "Once",
57
+ "Lazy",
58
+ "Mutex",
59
+ "ControlFlow",
60
+ "error-handling",
61
+ "null-safety"
49
62
  ],
50
63
  "devDependencies": {
51
- "@eslint/js": "^9.9.0",
52
- "@types/eslint__js": "^8.42.3",
53
- "eslint": "^9.9.0",
54
- "rollup": "^4.20.0",
55
- "rollup-plugin-dts": "^6.1.1",
56
- "rollup-plugin-esbuild": "^6.1.1",
57
- "typedoc": "^0.26.5",
58
- "typedoc-plugin-markdown": "^4.2.3",
59
- "typescript": "^5.5.4",
60
- "typescript-eslint": "^8.0.1"
61
- },
62
- "packageManager": "pnpm@9.7.0"
64
+ "@eslint/js": "^9.39.2",
65
+ "@stylistic/eslint-plugin": "^5.6.1",
66
+ "@vitest/coverage-v8": "^4.0.16",
67
+ "eslint": "^9.39.2",
68
+ "rollup": "^4.53.5",
69
+ "rollup-plugin-dts": "^6.3.0",
70
+ "typedoc": "^0.28.15",
71
+ "typescript": "^5.9.3",
72
+ "typescript-eslint": "^8.50.0",
73
+ "vite": "^7.3.0",
74
+ "vitest": "^4.0.16"
75
+ }
63
76
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/docs/README.md DELETED
@@ -1,47 +0,0 @@
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
- | [AsyncVoidIOResult](type-aliases/AsyncVoidIOResult.md) | `VoidIOResult` wrapped by `Promise`. |
23
- | [AsyncVoidResult](type-aliases/AsyncVoidResult.md) | `VoidResult<E>` wrapped by `Promise`. |
24
- | [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. |
25
- | [VoidIOResult](type-aliases/VoidIOResult.md) | Similar to Rust's `Result<(), Error>`. |
26
- | [VoidResult](type-aliases/VoidResult.md) | Similar to Rust's `Result<(), E>`. |
27
-
28
- ## Variables
29
-
30
- | Variable | Description |
31
- | ------ | ------ |
32
- | [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. |
33
- | [RESULT\_FALSE](variables/RESULT_FALSE.md) | Result constant for `false`. Can be used anywhere due to immutability. |
34
- | [RESULT\_TRUE](variables/RESULT_TRUE.md) | Result constant for `true`. Can be used anywhere due to immutability. |
35
- | [RESULT\_VOID](variables/RESULT_VOID.md) | Result constant for `void` or `()`. |
36
- | [RESULT\_ZERO](variables/RESULT_ZERO.md) | Result constant for `0`. Can be used anywhere due to immutability. |
37
-
38
- ## Functions
39
-
40
- | Function | Description |
41
- | ------ | ------ |
42
- | [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`. |
43
- | [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`. |
44
- | [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. |
45
- | [isOption](functions/isOption.md) | Checks if a value is an `Option`. |
46
- | [isResult](functions/isResult.md) | Checks if a value is a `Result`. |
47
- | [promiseToAsyncResult](functions/promiseToAsyncResult.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. |
@@ -1,46 +0,0 @@
1
- [**happy-rusty**](../README.md) • **Docs**
2
-
3
- ***
4
-
5
- [happy-rusty](../README.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
- ## Defined in
45
-
46
- [prelude.ts:473](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/prelude.ts#L473)
@@ -1,70 +0,0 @@
1
- [**happy-rusty**](../README.md) • **Docs**
2
-
3
- ***
4
-
5
- [happy-rusty](../README.md) / Ok
6
-
7
- # Function: Ok()
8
-
9
- ## Ok(value)
10
-
11
- ```ts
12
- function Ok<T, E>(value): Result<T, E>
13
- ```
14
-
15
- Creates a `Result<T, E>` representing a successful outcome containing a value.
16
- This function is used to construct a `Result` that signifies the operation was successful by containing the value `T`.
17
-
18
- ### Type Parameters
19
-
20
- | Type Parameter | Description |
21
- | ------ | ------ |
22
- | `T` | The type of the value to be contained in the `Ok` result. |
23
- | `E` | The type of the error that the result could potentially contain (not used in this case). |
24
-
25
- ### Parameters
26
-
27
- | Parameter | Type | Description |
28
- | ------ | ------ | ------ |
29
- | `value` | `T` | The value to wrap as an `Ok` result. |
30
-
31
- ### Returns
32
-
33
- [`Result`](../interfaces/Result.md)\<`T`, `E`\>
34
-
35
- A `Result<T, E>` that contains the provided value, representing the `Ok` case.
36
-
37
- ### Example
38
-
39
- ```ts
40
- const goodResult = Ok<number, Error>(1); // Result<number, Error> with a value
41
- if (goodResult.isOk()) {
42
- console.log(goodResult.unwrap()); // Outputs: 1
43
- }
44
- ```
45
-
46
- ### Defined in
47
-
48
- [prelude.ts:326](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/prelude.ts#L326)
49
-
50
- ## Ok()
51
-
52
- ```ts
53
- function Ok<E>(): Result<void, E>
54
- ```
55
-
56
- Because javascript does not have a `()` type, use `void` instead.
57
-
58
- ### Type Parameters
59
-
60
- | Type Parameter |
61
- | ------ |
62
- | `E` |
63
-
64
- ### Returns
65
-
66
- [`Result`](../interfaces/Result.md)\<`void`, `E`\>
67
-
68
- ### Defined in
69
-
70
- [prelude.ts:330](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/prelude.ts#L330)
@@ -1,45 +0,0 @@
1
- [**happy-rusty**](../README.md) • **Docs**
2
-
3
- ***
4
-
5
- [happy-rusty](../README.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
- ## Defined in
44
-
45
- [prelude.ts:71](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/prelude.ts#L71)
@@ -1,35 +0,0 @@
1
- [**happy-rusty**](../README.md) • **Docs**
2
-
3
- ***
4
-
5
- [happy-rusty](../README.md) / isOption
6
-
7
- # Function: isOption()
8
-
9
- ```ts
10
- function isOption<T>(o): o is Option<T>
11
- ```
12
-
13
- Checks if a value is an `Option`.
14
-
15
- ## Type Parameters
16
-
17
- | Type Parameter | Description |
18
- | ------ | ------ |
19
- | `T` | The expected type of the value contained within the `Option`. |
20
-
21
- ## Parameters
22
-
23
- | Parameter | Type | Description |
24
- | ------ | ------ | ------ |
25
- | `o` | `unknown` | The value to be checked as an `Option`. |
26
-
27
- ## Returns
28
-
29
- `o is Option<T>`
30
-
31
- `true` if the value is an `Option`, otherwise `false`.
32
-
33
- ## Defined in
34
-
35
- [utils.ts:11](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/utils.ts#L11)
@@ -1,36 +0,0 @@
1
- [**happy-rusty**](../README.md) • **Docs**
2
-
3
- ***
4
-
5
- [happy-rusty](../README.md) / isResult
6
-
7
- # Function: isResult()
8
-
9
- ```ts
10
- function isResult<T, E>(r): r is Result<T, E>
11
- ```
12
-
13
- Checks if a value is a `Result`.
14
-
15
- ## Type Parameters
16
-
17
- | Type Parameter | Description |
18
- | ------ | ------ |
19
- | `T` | The expected type of the success value contained within the `Result`. |
20
- | `E` | The expected type of the error value contained within the `Result`. |
21
-
22
- ## Parameters
23
-
24
- | Parameter | Type | Description |
25
- | ------ | ------ | ------ |
26
- | `r` | `unknown` | The value to be checked as a `Result`. |
27
-
28
- ## Returns
29
-
30
- `r is Result<T, E>`
31
-
32
- `true` if the value is a `Result`, otherwise `false`.
33
-
34
- ## Defined in
35
-
36
- [utils.ts:24](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/utils.ts#L24)
@@ -1,50 +0,0 @@
1
- [**happy-rusty**](../README.md) • **Docs**
2
-
3
- ***
4
-
5
- [happy-rusty](../README.md) / promiseToAsyncResult
6
-
7
- # Function: promiseToAsyncResult()
8
-
9
- ```ts
10
- function promiseToAsyncResult<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 | Default type | 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 promiseToAsyncResult(fetchData());
40
- result.inspect(x => {
41
- console.log('Data:', x);
42
- }).inspectErr(err => {
43
- console.error('Error:', err);
44
- });
45
- }
46
- ```
47
-
48
- ## Defined in
49
-
50
- [extensions.ts:25](https://github.com/JiangJie/happy-rusty/blob/6efe20969984552f52d79aee092bb6925a077fe7/src/enum/extensions.ts#L25)