happy-rusty 1.0.6 → 1.0.8

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 CHANGED
@@ -1,3 +1,8 @@
1
+ [![NPM version](http://img.shields.io/npm/v/happy-rusty.svg)](https://npmjs.org/package/happy-rusty)
2
+ [![JSR Version](https://jsr.io/badges/@happy-js/happy-rusty)](https://jsr.io/@happy-js/happy-rusty)
3
+ [![JSR Score](https://jsr.io/badges/@happy-js/happy-rusty/score)](https://jsr.io/@happy-js/happy-rusty/score)
4
+ [![Build Status](https://github.com/jiangjie/happy-rusty/actions/workflows/test.yml/badge.svg)](https://github.com/jiangjie/happy-rusty/actions/workflows/test.yml)
5
+
1
6
  # 在JavaScript中使用Rust特性
2
7
 
3
8
  ## 部分支持的特性
@@ -9,16 +14,26 @@
9
14
 
10
15
  ## 安装
11
16
 
12
- 通过 [JSR](https://jsr.io/@happy-js/happy-rusty) (**推荐**)
17
+ pnpm
18
+ ```
19
+ pnpm add happy-rusty
20
+ ```
21
+
22
+ yarn
13
23
  ```
14
- npx jsr add @happy-js/happy-rusty
24
+ yarn add happy-rusty
15
25
  ```
16
26
 
17
- 或者直接使用 npm
27
+ npm
18
28
  ```
19
29
  npm install --save happy-rusty
20
30
  ```
21
31
 
32
+ 通过 JSR
33
+ ```
34
+ jsr add @happy-js/happy-rusty
35
+ ```
36
+
22
37
  通过 deno
23
38
  ```
24
39
  deno add @happy-js/happy-rusty
package/README.md CHANGED
@@ -2,6 +2,11 @@
2
2
  <a href="README.cn.md">[中文]</a>
3
3
  </p>
4
4
 
5
+ [![NPM version](http://img.shields.io/npm/v/happy-rusty.svg)](https://npmjs.org/package/happy-rusty)
6
+ [![JSR Version](https://jsr.io/badges/@happy-js/happy-rusty)](https://jsr.io/@happy-js/happy-rusty)
7
+ [![JSR Score](https://jsr.io/badges/@happy-js/happy-rusty/score)](https://jsr.io/@happy-js/happy-rusty/score)
8
+ [![Build Status](https://github.com/jiangjie/happy-rusty/actions/workflows/test.yml/badge.svg)](https://github.com/jiangjie/happy-rusty/actions/workflows/test.yml)
9
+
5
10
  # Use Rust features in JavaScript happily
6
11
 
7
12
  ## Partial supported
@@ -9,13 +14,18 @@
9
14
  * [option](https://doc.rust-lang.org/core/option/index.html)
10
15
  * [result](https://doc.rust-lang.org/core/result/index.html)
11
16
 
12
- ## More is coming
17
+ ### More is coming
13
18
 
14
19
  ## Installation
15
20
 
16
- Via [JSR](https://jsr.io/@happy-js/happy-rusty) (**recommand**)
21
+ via pnpm
22
+ ```
23
+ pnpm add happy-rusty
24
+ ```
25
+
26
+ or via yarn
17
27
  ```
18
- npx jsr add @happy-js/happy-rusty
28
+ yarn add happy-rusty
19
29
  ```
20
30
 
21
31
  or just from npm
@@ -23,6 +33,11 @@ or just from npm
23
33
  npm install --save happy-rusty
24
34
  ```
25
35
 
36
+ via JSR
37
+ ```
38
+ jsr add @happy-js/happy-rusty
39
+ ```
40
+
26
41
  for deno
27
42
  ```
28
43
  deno add @happy-js/happy-rusty
package/dist/main.cjs CHANGED
@@ -25,7 +25,10 @@ function Ok(value) {
25
25
  kind: "Ok",
26
26
  isOk: () => true,
27
27
  isErr: () => false,
28
- unwrap: () => value
28
+ unwrap: () => value,
29
+ err: () => {
30
+ throw new TypeError("Ok is not Err");
31
+ }
29
32
  };
30
33
  }
31
34
  function Err(error) {
package/dist/main.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"main.cjs","sources":["../src/enum/option.ts","../src/enum/result.ts"],"sourcesContent":["/**\n * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,\n * 用于替代nullundefined的使用。\n */\n\n/**\n * option::Some type\n */\ninterface Some<T> {\n readonly kind: 'Some';\n readonly isSome: (this: Option<T>) => this is Some<T>;\n readonly isNone: (this: Option<T>) => this is None;\n readonly unwrap: () => T;\n}\n\n/**\n * option::None type\n */\ninterface None {\n readonly kind: 'None';\n readonly isSome: <T>(this: Option<T>) => this is Some<T>;\n readonly isNone: <T>(this: Option<T>) => this is None;\n readonly unwrap: () => never;\n}\n\n/**\n * option::Option type\n */\nexport type Option<T> = Some<T> | None;\n\n/**\n * 创建一个`Some`对象\n *\n * # Examples\n *\n * ```\n * const v = Some(10);\n * console.assert(v.unwrap() === 10);\n * ```\n *\n * @param value 被包裹的值,不能为nullundefined\n * @returns {Some}\n */\nexport function Some<T>(value: T): Option<T> {\n if (value == null) {\n throw new TypeError('Some value can not be null or undefined');\n }\n\n return {\n kind: 'Some',\n isSome: () => true,\n isNone: () => false,\n unwrap: () => value,\n } as const;\n}\n\n/**\n * `None`值是固定的\n * @constant {None}\n */\nexport const None: None = {\n kind: 'None',\n isSome: () => false,\n isNone: () => true,\n unwrap: () => {\n throw new TypeError('None can not unwrap');\n },\n} as const;","/**\n * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,\n * 用于错误处理。\n */\n\n\n/**\n * result::Ok type\n */\ninterface Ok<T, E> {\n readonly kind: 'Ok';\n readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;\n readonly isErr: (this: Result<T, E>) => this is Err<T, E>;\n readonly unwrap: () => T;\n}\n\n/**\n * result::Err type\n */\ninterface Err<T, E> {\n readonly kind: 'Err';\n readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;\n readonly isErr: (this: Result<T, E>) => this is Err<T, E>;\n readonly unwrap: () => never;\n readonly err: () => E;\n}\n\n/**\n * result::Result type\n */\nexport type Result<T, E> = Ok<T, E> | Err<T, E>;\n\n/**\n * 创建一个`Ok`对象\n *\n * # Examples\n *\n * ```\n * const v = Ok(10);\n * console.assert(v.unwrap() === 10);\n *\nfunction judge(n: number): Option<Promise<Result<number, Error>>> {\n if (n < 0 || n >= 1) {\n return None;\n }\n\n return Some(new Promise(resolve => {\n const r = Math.random();\n resolve(r > n ? Ok(r) : Err(new Error('lose')));\n }));\n}\n\nconst res = judge(0.8);\nif (res.isNone()) {\n console.error('invalid number');\n} else {\n const result = await res.unwrap();\n if (result.isErr()) {\n console.assert(result.err().message === 'lose');\n } else {\n console.log(result.unwrap()); // must greater than 0.8\n }\n}\n *\n * ```\n *\n * @param value 被包裹的值\n * @returns {Ok}\n */\nexport function Ok<T, E>(value: T): Result<T, E> {\n return {\n kind: 'Ok',\n isOk: () => true,\n isErr: () => false,\n unwrap: () => value,\n } as const;\n}\n\n/**\n * 创建一个`Err`对象\n *\n * # Examples\n *\n * ```\n * const e = Err(new Error('unknown error'));\n * console.assert(e.err().message === 'unknown error');\n * ```\n *\n * @param error 被包裹的错误\n * @returns {Err}\n */\nexport function Err<T, E>(error: E): Result<T, E> {\n return {\n kind: 'Err',\n isOk: () => false,\n isErr: () => true,\n unwrap: () => {\n throw error;\n },\n err: () => error,\n } as const;\n}"],"names":[],"mappings":";;AA2CO,SAAS,KAAQ,KAAqB,EAAA;AACzC,EAAA,IAAI,SAAS,IAAM,EAAA;AACf,IAAM,MAAA,IAAI,UAAU,yCAAyC,CAAA,CAAA;AAAA,GACjE;AAEA,EAAO,OAAA;AAAA,IACH,IAAM,EAAA,MAAA;AAAA,IACN,QAAQ,MAAM,IAAA;AAAA,IACd,QAAQ,MAAM,KAAA;AAAA,IACd,QAAQ,MAAM,KAAA;AAAA,GAClB,CAAA;AACJ,CAAA;AAMO,MAAM,IAAa,GAAA;AAAA,EACtB,IAAM,EAAA,MAAA;AAAA,EACN,QAAQ,MAAM,KAAA;AAAA,EACd,QAAQ,MAAM,IAAA;AAAA,EACd,QAAQ,MAAM;AACV,IAAM,MAAA,IAAI,UAAU,qBAAqB,CAAA,CAAA;AAAA,GAC7C;AACJ;;ACEO,SAAS,GAAS,KAAwB,EAAA;AAC7C,EAAO,OAAA;AAAA,IACH,IAAM,EAAA,IAAA;AAAA,IACN,MAAM,MAAM,IAAA;AAAA,IACZ,OAAO,MAAM,KAAA;AAAA,IACb,QAAQ,MAAM,KAAA;AAAA,GAClB,CAAA;AACJ,CAAA;AAeO,SAAS,IAAU,KAAwB,EAAA;AAC9C,EAAO,OAAA;AAAA,IACH,IAAM,EAAA,KAAA;AAAA,IACN,MAAM,MAAM,KAAA;AAAA,IACZ,OAAO,MAAM,IAAA;AAAA,IACb,QAAQ,MAAM;AACV,MAAM,MAAA,KAAA,CAAA;AAAA,KACV;AAAA,IACA,KAAK,MAAM,KAAA;AAAA,GACf,CAAA;AACJ;;;;;;;"}
1
+ {"version":3,"file":"main.cjs","sources":["../src/enum/option.ts","../src/enum/result.ts"],"sourcesContent":["/**\n * @fileoverview A Rust-inspired [Option](https://doc.rust-lang.org/core/option/index.html) enum, used as an alternative to the use of null and undefined.\n */\n\n/**\n * option::Some type\n */\ninterface Some<T> {\n readonly kind: 'Some';\n readonly isSome: (this: Option<T>) => this is Some<T>;\n readonly isNone: (this: Option<T>) => this is None;\n readonly unwrap: () => T;\n}\n\n/**\n * option::None type\n */\ninterface None {\n readonly kind: 'None';\n readonly isSome: <T>(this: Option<T>) => this is Some<T>;\n readonly isNone: <T>(this: Option<T>) => this is None;\n readonly unwrap: () => never;\n}\n\n/**\n * option::Option type\n */\nexport type Option<T> = Some<T> | None;\n\n/**\n * Create a `Some` object.\n *\n * # Examples\n *\n * ```\n * const v = Some(10);\n * console.assert(v.unwrap() === 10);\n * ```\n *\n * @param value The wrapped value which can not be null or undefined.\n * @returns {Some}\n */\nexport function Some<T>(value: NonNullable<T>): Option<T> {\n if (value == null) {\n throw new TypeError('Some value can not be null or undefined');\n }\n\n return {\n kind: 'Some',\n isSome: () => true,\n isNone: () => false,\n unwrap: () => value,\n } as const;\n}\n\n/**\n * `None` value is freeze.\n *\n * @constant {None}\n */\nexport const None: None = {\n kind: 'None',\n isSome: () => false,\n isNone: () => true,\n unwrap: () => {\n throw new TypeError('None can not unwrap');\n },\n} as const;","/**\n * @fileoverview A Rust-inspired [Result](https://doc.rust-lang.org/core/result/index.html) enum, used for better error handling.\n */\n\n/**\n * result::Ok type\n */\ninterface Ok<T, E> {\n readonly kind: 'Ok';\n readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;\n readonly isErr: (this: Result<T, E>) => this is Err<T, E>;\n readonly unwrap: () => T;\n readonly err: () => never;\n}\n\n/**\n * result::Err type\n */\ninterface Err<T, E> {\n readonly kind: 'Err';\n readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;\n readonly isErr: (this: Result<T, E>) => this is Err<T, E>;\n readonly unwrap: () => never;\n readonly err: () => E;\n}\n\n/**\n * result::Result type\n */\nexport type Result<T, E> = Ok<T, E> | Err<T, E>;\n\n/**\n * Create an `Ok` object.\n *\n * # Examples\n *\n * ```\n * const v = Ok(10);\n * console.assert(v.unwrap() === 10);\n *\nfunction judge(n: number): Option<Promise<Result<number, Error>>> {\n if (n < 0 || n >= 1) {\n return None;\n }\n\n return Some(new Promise(resolve => {\n const r = Math.random();\n resolve(r > n ? Ok(r) : Err(new Error('lose')));\n }));\n}\n\nconst res = judge(0.8);\nif (res.isNone()) {\n console.error('invalid number');\n} else {\n const result = await res.unwrap();\n if (result.isErr()) {\n console.assert(result.err().message === 'lose');\n } else {\n console.log(result.unwrap()); // must greater than 0.8\n }\n}\n *\n * ```\n *\n * @param value The wrapped value.\n * @returns {Ok}\n */\nexport function Ok<T, E>(value: T): Result<T, E> {\n return {\n kind: 'Ok',\n isOk: () => true,\n isErr: () => false,\n unwrap: () => value,\n err: () => {\n throw new TypeError('Ok is not Err');\n },\n } as const;\n}\n\n/**\n * Create an `Err` object.\n *\n * # Examples\n *\n * ```\n * const e = Err(new Error('unknown error'));\n * console.assert(e.err().message === 'unknown error');\n * ```\n *\n * @param error The wrapped error value.\n * @returns {Err}\n */\nexport function Err<T, E>(error: E): Result<T, E> {\n return {\n kind: 'Err',\n isOk: () => false,\n isErr: () => true,\n unwrap: () => {\n throw error;\n },\n err: () => error,\n } as const;\n}"],"names":[],"mappings":";;AA0CO,SAAS,KAAQ,KAAkC,EAAA;AACtD,EAAA,IAAI,SAAS,IAAM,EAAA;AACf,IAAM,MAAA,IAAI,UAAU,yCAAyC,CAAA,CAAA;AAAA,GACjE;AAEA,EAAO,OAAA;AAAA,IACH,IAAM,EAAA,MAAA;AAAA,IACN,QAAQ,MAAM,IAAA;AAAA,IACd,QAAQ,MAAM,KAAA;AAAA,IACd,QAAQ,MAAM,KAAA;AAAA,GAClB,CAAA;AACJ,CAAA;AAOO,MAAM,IAAa,GAAA;AAAA,EACtB,IAAM,EAAA,MAAA;AAAA,EACN,QAAQ,MAAM,KAAA;AAAA,EACd,QAAQ,MAAM,IAAA;AAAA,EACd,QAAQ,MAAM;AACV,IAAM,MAAA,IAAI,UAAU,qBAAqB,CAAA,CAAA;AAAA,GAC7C;AACJ;;ACCO,SAAS,GAAS,KAAwB,EAAA;AAC7C,EAAO,OAAA;AAAA,IACH,IAAM,EAAA,IAAA;AAAA,IACN,MAAM,MAAM,IAAA;AAAA,IACZ,OAAO,MAAM,KAAA;AAAA,IACb,QAAQ,MAAM,KAAA;AAAA,IACd,KAAK,MAAM;AACP,MAAM,MAAA,IAAI,UAAU,eAAe,CAAA,CAAA;AAAA,KACvC;AAAA,GACJ,CAAA;AACJ,CAAA;AAeO,SAAS,IAAU,KAAwB,EAAA;AAC9C,EAAO,OAAA;AAAA,IACH,IAAM,EAAA,KAAA;AAAA,IACN,MAAM,MAAM,KAAA;AAAA,IACZ,OAAO,MAAM,IAAA;AAAA,IACb,QAAQ,MAAM;AACV,MAAM,MAAA,KAAA,CAAA;AAAA,KACV;AAAA,IACA,KAAK,MAAM,KAAA;AAAA,GACf,CAAA;AACJ;;;;;;;"}
package/dist/main.mjs CHANGED
@@ -23,7 +23,10 @@ function Ok(value) {
23
23
  kind: "Ok",
24
24
  isOk: () => true,
25
25
  isErr: () => false,
26
- unwrap: () => value
26
+ unwrap: () => value,
27
+ err: () => {
28
+ throw new TypeError("Ok is not Err");
29
+ }
27
30
  };
28
31
  }
29
32
  function Err(error) {
package/dist/main.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"main.mjs","sources":["../src/enum/option.ts","../src/enum/result.ts"],"sourcesContent":["/**\n * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,\n * 用于替代nullundefined的使用。\n */\n\n/**\n * option::Some type\n */\ninterface Some<T> {\n readonly kind: 'Some';\n readonly isSome: (this: Option<T>) => this is Some<T>;\n readonly isNone: (this: Option<T>) => this is None;\n readonly unwrap: () => T;\n}\n\n/**\n * option::None type\n */\ninterface None {\n readonly kind: 'None';\n readonly isSome: <T>(this: Option<T>) => this is Some<T>;\n readonly isNone: <T>(this: Option<T>) => this is None;\n readonly unwrap: () => never;\n}\n\n/**\n * option::Option type\n */\nexport type Option<T> = Some<T> | None;\n\n/**\n * 创建一个`Some`对象\n *\n * # Examples\n *\n * ```\n * const v = Some(10);\n * console.assert(v.unwrap() === 10);\n * ```\n *\n * @param value 被包裹的值,不能为nullundefined\n * @returns {Some}\n */\nexport function Some<T>(value: T): Option<T> {\n if (value == null) {\n throw new TypeError('Some value can not be null or undefined');\n }\n\n return {\n kind: 'Some',\n isSome: () => true,\n isNone: () => false,\n unwrap: () => value,\n } as const;\n}\n\n/**\n * `None`值是固定的\n * @constant {None}\n */\nexport const None: None = {\n kind: 'None',\n isSome: () => false,\n isNone: () => true,\n unwrap: () => {\n throw new TypeError('None can not unwrap');\n },\n} as const;","/**\n * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,\n * 用于错误处理。\n */\n\n\n/**\n * result::Ok type\n */\ninterface Ok<T, E> {\n readonly kind: 'Ok';\n readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;\n readonly isErr: (this: Result<T, E>) => this is Err<T, E>;\n readonly unwrap: () => T;\n}\n\n/**\n * result::Err type\n */\ninterface Err<T, E> {\n readonly kind: 'Err';\n readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;\n readonly isErr: (this: Result<T, E>) => this is Err<T, E>;\n readonly unwrap: () => never;\n readonly err: () => E;\n}\n\n/**\n * result::Result type\n */\nexport type Result<T, E> = Ok<T, E> | Err<T, E>;\n\n/**\n * 创建一个`Ok`对象\n *\n * # Examples\n *\n * ```\n * const v = Ok(10);\n * console.assert(v.unwrap() === 10);\n *\nfunction judge(n: number): Option<Promise<Result<number, Error>>> {\n if (n < 0 || n >= 1) {\n return None;\n }\n\n return Some(new Promise(resolve => {\n const r = Math.random();\n resolve(r > n ? Ok(r) : Err(new Error('lose')));\n }));\n}\n\nconst res = judge(0.8);\nif (res.isNone()) {\n console.error('invalid number');\n} else {\n const result = await res.unwrap();\n if (result.isErr()) {\n console.assert(result.err().message === 'lose');\n } else {\n console.log(result.unwrap()); // must greater than 0.8\n }\n}\n *\n * ```\n *\n * @param value 被包裹的值\n * @returns {Ok}\n */\nexport function Ok<T, E>(value: T): Result<T, E> {\n return {\n kind: 'Ok',\n isOk: () => true,\n isErr: () => false,\n unwrap: () => value,\n } as const;\n}\n\n/**\n * 创建一个`Err`对象\n *\n * # Examples\n *\n * ```\n * const e = Err(new Error('unknown error'));\n * console.assert(e.err().message === 'unknown error');\n * ```\n *\n * @param error 被包裹的错误\n * @returns {Err}\n */\nexport function Err<T, E>(error: E): Result<T, E> {\n return {\n kind: 'Err',\n isOk: () => false,\n isErr: () => true,\n unwrap: () => {\n throw error;\n },\n err: () => error,\n } as const;\n}"],"names":[],"mappings":"AA2CO,SAAS,KAAQ,KAAqB,EAAA;AACzC,EAAA,IAAI,SAAS,IAAM,EAAA;AACf,IAAM,MAAA,IAAI,UAAU,yCAAyC,CAAA,CAAA;AAAA,GACjE;AAEA,EAAO,OAAA;AAAA,IACH,IAAM,EAAA,MAAA;AAAA,IACN,QAAQ,MAAM,IAAA;AAAA,IACd,QAAQ,MAAM,KAAA;AAAA,IACd,QAAQ,MAAM,KAAA;AAAA,GAClB,CAAA;AACJ,CAAA;AAMO,MAAM,IAAa,GAAA;AAAA,EACtB,IAAM,EAAA,MAAA;AAAA,EACN,QAAQ,MAAM,KAAA;AAAA,EACd,QAAQ,MAAM,IAAA;AAAA,EACd,QAAQ,MAAM;AACV,IAAM,MAAA,IAAI,UAAU,qBAAqB,CAAA,CAAA;AAAA,GAC7C;AACJ;;ACEO,SAAS,GAAS,KAAwB,EAAA;AAC7C,EAAO,OAAA;AAAA,IACH,IAAM,EAAA,IAAA;AAAA,IACN,MAAM,MAAM,IAAA;AAAA,IACZ,OAAO,MAAM,KAAA;AAAA,IACb,QAAQ,MAAM,KAAA;AAAA,GAClB,CAAA;AACJ,CAAA;AAeO,SAAS,IAAU,KAAwB,EAAA;AAC9C,EAAO,OAAA;AAAA,IACH,IAAM,EAAA,KAAA;AAAA,IACN,MAAM,MAAM,KAAA;AAAA,IACZ,OAAO,MAAM,IAAA;AAAA,IACb,QAAQ,MAAM;AACV,MAAM,MAAA,KAAA,CAAA;AAAA,KACV;AAAA,IACA,KAAK,MAAM,KAAA;AAAA,GACf,CAAA;AACJ;;;;"}
1
+ {"version":3,"file":"main.mjs","sources":["../src/enum/option.ts","../src/enum/result.ts"],"sourcesContent":["/**\n * @fileoverview A Rust-inspired [Option](https://doc.rust-lang.org/core/option/index.html) enum, used as an alternative to the use of null and undefined.\n */\n\n/**\n * option::Some type\n */\ninterface Some<T> {\n readonly kind: 'Some';\n readonly isSome: (this: Option<T>) => this is Some<T>;\n readonly isNone: (this: Option<T>) => this is None;\n readonly unwrap: () => T;\n}\n\n/**\n * option::None type\n */\ninterface None {\n readonly kind: 'None';\n readonly isSome: <T>(this: Option<T>) => this is Some<T>;\n readonly isNone: <T>(this: Option<T>) => this is None;\n readonly unwrap: () => never;\n}\n\n/**\n * option::Option type\n */\nexport type Option<T> = Some<T> | None;\n\n/**\n * Create a `Some` object.\n *\n * # Examples\n *\n * ```\n * const v = Some(10);\n * console.assert(v.unwrap() === 10);\n * ```\n *\n * @param value The wrapped value which can not be null or undefined.\n * @returns {Some}\n */\nexport function Some<T>(value: NonNullable<T>): Option<T> {\n if (value == null) {\n throw new TypeError('Some value can not be null or undefined');\n }\n\n return {\n kind: 'Some',\n isSome: () => true,\n isNone: () => false,\n unwrap: () => value,\n } as const;\n}\n\n/**\n * `None` value is freeze.\n *\n * @constant {None}\n */\nexport const None: None = {\n kind: 'None',\n isSome: () => false,\n isNone: () => true,\n unwrap: () => {\n throw new TypeError('None can not unwrap');\n },\n} as const;","/**\n * @fileoverview A Rust-inspired [Result](https://doc.rust-lang.org/core/result/index.html) enum, used for better error handling.\n */\n\n/**\n * result::Ok type\n */\ninterface Ok<T, E> {\n readonly kind: 'Ok';\n readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;\n readonly isErr: (this: Result<T, E>) => this is Err<T, E>;\n readonly unwrap: () => T;\n readonly err: () => never;\n}\n\n/**\n * result::Err type\n */\ninterface Err<T, E> {\n readonly kind: 'Err';\n readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;\n readonly isErr: (this: Result<T, E>) => this is Err<T, E>;\n readonly unwrap: () => never;\n readonly err: () => E;\n}\n\n/**\n * result::Result type\n */\nexport type Result<T, E> = Ok<T, E> | Err<T, E>;\n\n/**\n * Create an `Ok` object.\n *\n * # Examples\n *\n * ```\n * const v = Ok(10);\n * console.assert(v.unwrap() === 10);\n *\nfunction judge(n: number): Option<Promise<Result<number, Error>>> {\n if (n < 0 || n >= 1) {\n return None;\n }\n\n return Some(new Promise(resolve => {\n const r = Math.random();\n resolve(r > n ? Ok(r) : Err(new Error('lose')));\n }));\n}\n\nconst res = judge(0.8);\nif (res.isNone()) {\n console.error('invalid number');\n} else {\n const result = await res.unwrap();\n if (result.isErr()) {\n console.assert(result.err().message === 'lose');\n } else {\n console.log(result.unwrap()); // must greater than 0.8\n }\n}\n *\n * ```\n *\n * @param value The wrapped value.\n * @returns {Ok}\n */\nexport function Ok<T, E>(value: T): Result<T, E> {\n return {\n kind: 'Ok',\n isOk: () => true,\n isErr: () => false,\n unwrap: () => value,\n err: () => {\n throw new TypeError('Ok is not Err');\n },\n } as const;\n}\n\n/**\n * Create an `Err` object.\n *\n * # Examples\n *\n * ```\n * const e = Err(new Error('unknown error'));\n * console.assert(e.err().message === 'unknown error');\n * ```\n *\n * @param error The wrapped error value.\n * @returns {Err}\n */\nexport function Err<T, E>(error: E): Result<T, E> {\n return {\n kind: 'Err',\n isOk: () => false,\n isErr: () => true,\n unwrap: () => {\n throw error;\n },\n err: () => error,\n } as const;\n}"],"names":[],"mappings":"AA0CO,SAAS,KAAQ,KAAkC,EAAA;AACtD,EAAA,IAAI,SAAS,IAAM,EAAA;AACf,IAAM,MAAA,IAAI,UAAU,yCAAyC,CAAA,CAAA;AAAA,GACjE;AAEA,EAAO,OAAA;AAAA,IACH,IAAM,EAAA,MAAA;AAAA,IACN,QAAQ,MAAM,IAAA;AAAA,IACd,QAAQ,MAAM,KAAA;AAAA,IACd,QAAQ,MAAM,KAAA;AAAA,GAClB,CAAA;AACJ,CAAA;AAOO,MAAM,IAAa,GAAA;AAAA,EACtB,IAAM,EAAA,MAAA;AAAA,EACN,QAAQ,MAAM,KAAA;AAAA,EACd,QAAQ,MAAM,IAAA;AAAA,EACd,QAAQ,MAAM;AACV,IAAM,MAAA,IAAI,UAAU,qBAAqB,CAAA,CAAA;AAAA,GAC7C;AACJ;;ACCO,SAAS,GAAS,KAAwB,EAAA;AAC7C,EAAO,OAAA;AAAA,IACH,IAAM,EAAA,IAAA;AAAA,IACN,MAAM,MAAM,IAAA;AAAA,IACZ,OAAO,MAAM,KAAA;AAAA,IACb,QAAQ,MAAM,KAAA;AAAA,IACd,KAAK,MAAM;AACP,MAAM,MAAA,IAAI,UAAU,eAAe,CAAA,CAAA;AAAA,KACvC;AAAA,GACJ,CAAA;AACJ,CAAA;AAeO,SAAS,IAAU,KAAwB,EAAA;AAC9C,EAAO,OAAA;AAAA,IACH,IAAM,EAAA,KAAA;AAAA,IACN,MAAM,MAAM,KAAA;AAAA,IACZ,OAAO,MAAM,IAAA;AAAA,IACb,QAAQ,MAAM;AACV,MAAM,MAAA,KAAA,CAAA;AAAA,KACV;AAAA,IACA,KAAK,MAAM,KAAA;AAAA,GACf,CAAA;AACJ;;;;"}
package/dist/types.d.ts CHANGED
@@ -3,8 +3,7 @@
3
3
  */
4
4
  type Option<T> = Some<T> | None;
5
5
  /**
6
- * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,
7
- * 用于替代null和undefined的使用。
6
+ * @fileoverview A Rust-inspired [Option](https://doc.rust-lang.org/core/option/index.html) enum, used as an alternative to the use of null and undefined.
8
7
  */
9
8
  /**
10
9
  * option::Some type
@@ -16,7 +15,7 @@ interface Some<T> {
16
15
  readonly unwrap: () => T;
17
16
  }
18
17
  /**
19
- * 创建一个`Some`对象
18
+ * Create a `Some` object.
20
19
  *
21
20
  * # Examples
22
21
  *
@@ -25,10 +24,10 @@ interface Some<T> {
25
24
  * console.assert(v.unwrap() === 10);
26
25
  * ```
27
26
  *
28
- * @param value 被包裹的值,不能为nullundefined
27
+ * @param value The wrapped value which can not be null or undefined.
29
28
  * @returns {Some}
30
29
  */
31
- declare function Some<T>(value: T): Option<T>;
30
+ declare function Some<T>(value: NonNullable<T>): Option<T>;
32
31
  /**
33
32
  * option::None type
34
33
  */
@@ -39,7 +38,8 @@ interface None {
39
38
  readonly unwrap: () => never;
40
39
  }
41
40
  /**
42
- * `None`值是固定的
41
+ * `None` value is freeze.
42
+ *
43
43
  * @constant {None}
44
44
  */
45
45
  declare const None: None;
@@ -49,8 +49,7 @@ declare const None: None;
49
49
  */
50
50
  type Result<T, E> = Ok<T, E> | Err<T, E>;
51
51
  /**
52
- * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,
53
- * 用于错误处理。
52
+ * @fileoverview A Rust-inspired [Result](https://doc.rust-lang.org/core/result/index.html) enum, used for better error handling.
54
53
  */
55
54
  /**
56
55
  * result::Ok type
@@ -60,9 +59,10 @@ interface Ok<T, E> {
60
59
  readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;
61
60
  readonly isErr: (this: Result<T, E>) => this is Err<T, E>;
62
61
  readonly unwrap: () => T;
62
+ readonly err: () => never;
63
63
  }
64
64
  /**
65
- * 创建一个`Ok`对象
65
+ * Create an `Ok` object.
66
66
  *
67
67
  * # Examples
68
68
  *
@@ -95,7 +95,7 @@ if (res.isNone()) {
95
95
  *
96
96
  * ```
97
97
  *
98
- * @param value 被包裹的值
98
+ * @param value The wrapped value.
99
99
  * @returns {Ok}
100
100
  */
101
101
  declare function Ok<T, E>(value: T): Result<T, E>;
@@ -110,7 +110,7 @@ interface Err<T, E> {
110
110
  readonly err: () => E;
111
111
  }
112
112
  /**
113
- * 创建一个`Err`对象
113
+ * Create an `Err` object.
114
114
  *
115
115
  * # Examples
116
116
  *
@@ -119,7 +119,7 @@ interface Err<T, E> {
119
119
  * console.assert(e.err().message === 'unknown error');
120
120
  * ```
121
121
  *
122
- * @param error 被包裹的错误
122
+ * @param error The wrapped error value.
123
123
  * @returns {Err}
124
124
  */
125
125
  declare function Err<T, E>(error: E): Result<T, E>;
package/package.json CHANGED
@@ -3,13 +3,12 @@
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.0.6",
6
+ "version": "1.0.8",
7
7
  "type": "module",
8
- "source": "./src/mod.ts",
9
- "main": "./dist/main.cjs",
10
- "module": "./dist/main.mjs",
11
- "types": "./dist/types.d.ts",
12
- "exports": "./src/mod.ts",
8
+ "source": "src/mod.ts",
9
+ "main": "dist/main.cjs",
10
+ "module": "dist/main.mjs",
11
+ "types": "dist/types.d.ts",
13
12
  "files": [
14
13
  "LICENSE",
15
14
  "README.md",
@@ -20,12 +19,12 @@
20
19
  ],
21
20
  "sideEffects": false,
22
21
  "scripts": {
23
- "check": "npx tsc --noEmit",
24
- "lint": "npx eslint src/",
25
- "prebuild": "npx rimraf dist && npm run check && npm run lint",
26
- "build": "npx rollup --config rollup.config.mjs",
27
- "test": "bun test",
28
- "prepublishOnly": "npm run build"
22
+ "check": "pnpm exec tsc --noEmit",
23
+ "lint": "pnpm exec eslint src/",
24
+ "prebuild": "pnpm exec rimraf dist && pnpm run check && pnpm run lint",
25
+ "build": "pnpm exec rollup --config rollup.config.mjs",
26
+ "test": "bun test --coverage",
27
+ "prepublishOnly": "pnpm run build"
29
28
  },
30
29
  "repository": {
31
30
  "type": "git",
@@ -51,5 +50,6 @@
51
50
  "rollup-plugin-dts": "^6.1.0",
52
51
  "rollup-plugin-esbuild": "^6.1.1",
53
52
  "typescript": "^5.4.5"
54
- }
53
+ },
54
+ "packageManager": "pnpm@9.1.1+sha512.14e915759c11f77eac07faba4d019c193ec8637229e62ec99eefb7cf3c3b75c64447882b7c485142451ee3a6b408059cdfb7b7fa0341b975f12d0f7629c71195"
55
55
  }
@@ -1,6 +1,5 @@
1
1
  /**
2
- * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,
3
- * 用于替代null和undefined的使用。
2
+ * @fileoverview A Rust-inspired [Option](https://doc.rust-lang.org/core/option/index.html) enum, used as an alternative to the use of null and undefined.
4
3
  */
5
4
 
6
5
  /**
@@ -29,7 +28,7 @@ interface None {
29
28
  export type Option<T> = Some<T> | None;
30
29
 
31
30
  /**
32
- * 创建一个`Some`对象
31
+ * Create a `Some` object.
33
32
  *
34
33
  * # Examples
35
34
  *
@@ -38,10 +37,10 @@ export type Option<T> = Some<T> | None;
38
37
  * console.assert(v.unwrap() === 10);
39
38
  * ```
40
39
  *
41
- * @param value 被包裹的值,不能为nullundefined
40
+ * @param value The wrapped value which can not be null or undefined.
42
41
  * @returns {Some}
43
42
  */
44
- export function Some<T>(value: T): Option<T> {
43
+ export function Some<T>(value: NonNullable<T>): Option<T> {
45
44
  if (value == null) {
46
45
  throw new TypeError('Some value can not be null or undefined');
47
46
  }
@@ -55,7 +54,8 @@ export function Some<T>(value: T): Option<T> {
55
54
  }
56
55
 
57
56
  /**
58
- * `None`值是固定的
57
+ * `None` value is freeze.
58
+ *
59
59
  * @constant {None}
60
60
  */
61
61
  export const None: None = {
@@ -1,9 +1,7 @@
1
1
  /**
2
- * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,
3
- * 用于错误处理。
2
+ * @fileoverview A Rust-inspired [Result](https://doc.rust-lang.org/core/result/index.html) enum, used for better error handling.
4
3
  */
5
4
 
6
-
7
5
  /**
8
6
  * result::Ok type
9
7
  */
@@ -12,6 +10,7 @@ interface Ok<T, E> {
12
10
  readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;
13
11
  readonly isErr: (this: Result<T, E>) => this is Err<T, E>;
14
12
  readonly unwrap: () => T;
13
+ readonly err: () => never;
15
14
  }
16
15
 
17
16
  /**
@@ -31,7 +30,7 @@ interface Err<T, E> {
31
30
  export type Result<T, E> = Ok<T, E> | Err<T, E>;
32
31
 
33
32
  /**
34
- * 创建一个`Ok`对象
33
+ * Create an `Ok` object.
35
34
  *
36
35
  * # Examples
37
36
  *
@@ -64,7 +63,7 @@ if (res.isNone()) {
64
63
  *
65
64
  * ```
66
65
  *
67
- * @param value 被包裹的值
66
+ * @param value The wrapped value.
68
67
  * @returns {Ok}
69
68
  */
70
69
  export function Ok<T, E>(value: T): Result<T, E> {
@@ -73,11 +72,14 @@ export function Ok<T, E>(value: T): Result<T, E> {
73
72
  isOk: () => true,
74
73
  isErr: () => false,
75
74
  unwrap: () => value,
75
+ err: () => {
76
+ throw new TypeError('Ok is not Err');
77
+ },
76
78
  } as const;
77
79
  }
78
80
 
79
81
  /**
80
- * 创建一个`Err`对象
82
+ * Create an `Err` object.
81
83
  *
82
84
  * # Examples
83
85
  *
@@ -86,7 +88,7 @@ export function Ok<T, E>(value: T): Result<T, E> {
86
88
  * console.assert(e.err().message === 'unknown error');
87
89
  * ```
88
90
  *
89
- * @param error 被包裹的错误
91
+ * @param error The wrapped error value.
90
92
  * @returns {Err}
91
93
  */
92
94
  export function Err<T, E>(error: E): Result<T, E> {