happy-rusty 1.0.1 → 1.0.3

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.md CHANGED
@@ -5,4 +5,62 @@
5
5
  * [option](https://doc.rust-lang.org/core/option/index.html)
6
6
  * [result](https://doc.rust-lang.org/core/result/index.html)
7
7
 
8
- ## More is coming
8
+ ## More is coming
9
+
10
+ ## Installation
11
+
12
+ Via [JSR](https://jsr.io/@happy-js/happy-rusty)(**recommand**)
13
+ ```
14
+ npx jsr add @happy-js/happy-rusty
15
+ ```
16
+
17
+ or just from npm
18
+ ```
19
+ npm install --save happy-rusty
20
+ ```
21
+
22
+ for deno
23
+ ```
24
+ deno add @happy-js/happy-rusty
25
+ ```
26
+
27
+ for bun
28
+ ```
29
+ bunx jsr add @happy-js/happy-rusty
30
+ ```
31
+
32
+ then import to your code.
33
+ ```ts
34
+ import { Some, None, Ok, Err } from '@happy-js/happy-rusty';
35
+ ```
36
+
37
+ Enjoy the happiness.
38
+
39
+ ## Examples
40
+
41
+ ```ts
42
+ import { Some, None, Ok, Err } from '@happy-js/happy-rusty';
43
+
44
+ function judge(n: number): Option<Promise<Result<number, Error>>> {
45
+ if (n < 0 || n >= 1) {
46
+ return None;
47
+ }
48
+
49
+ return Some(new Promise(resolve => {
50
+ const r = Math.random();
51
+ resolve(r > n ? Ok(r) : Err(new Error('lose')));
52
+ }));
53
+ }
54
+
55
+ const res = judge(0.8);
56
+ if (res.isNone()) {
57
+ console.error('invalid number');
58
+ } else {
59
+ const result = await res.unwrap();
60
+ if (result.isErr()) {
61
+ console.assert(result.err().message === 'lose');
62
+ } else {
63
+ console.log(result.unwrap()); // must greater than 0.8
64
+ }
65
+ }
66
+ ```
package/dist/main.cjs CHANGED
@@ -10,6 +10,8 @@ $parcel$export(module.exports, "Ok", () => $0d2eaee7439556da$export$8146e38189b4
10
10
  /**
11
11
  * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,
12
12
  * 用于替代null和undefined的使用。
13
+ */ /**
14
+ * option::Some type
13
15
  */ function $49294c54424499ce$export$9f9d0139d032da4f(value) {
14
16
  if (value == null) throw new Error("Some value can not be null or undefined");
15
17
  return {
@@ -32,6 +34,8 @@ const $49294c54424499ce$export$57ca7e07b341709d = {
32
34
  /**
33
35
  * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,
34
36
  * 用于错误处理。
37
+ */ /**
38
+ * result::Ok type
35
39
  */ function $0d2eaee7439556da$export$8146e38189b4f4dc(value) {
36
40
  return {
37
41
  kind: "Ok",
package/dist/main.cjs.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;ACAA;;;CAGC,GA+BM,SAAS,0CAAQ,KAAQ;IAC5B,IAAI,SAAS,MACT,MAAM,IAAI,MAAM;IAGpB,OAAO;QACH,MAAM;QACN,QAAQ,IAAM;QACd,QAAQ,IAAM;QACd,QAAQ,IAAM;IAClB;AACJ;AAKO,MAAM,4CAAa;IACtB,MAAM;IACN,QAAQ,IAAM;IACd,QAAQ,IAAM;IACd,QAAQ;QACJ,MAAM,IAAI,MAAM;IACpB;AACJ;;;ACzDA;;;CAGC,GAsDM,SAAS,0CAAS,KAAQ;IAC7B,OAAO;QACH,MAAM;QACN,MAAM,IAAM;QACZ,OAAO,IAAM;QACb,QAAQ,IAAM;IAClB;AACJ;AAeO,SAAS,0CAAU,KAAQ;IAC9B,OAAO;QACH,MAAM;QACN,MAAM,IAAM;QACZ,OAAO,IAAM;QACb,QAAQ;YACJ,MAAM;QACV;QACA,KAAK,IAAM;IACf;AACJ;;","sources":["src/mod.ts","src/enum/option.ts","src/enum/result.ts"],"sourcesContent":["export { None, Some, type Option } from './enum/option.ts';\nexport { Err, Ok, type Result } from './enum/result.ts';\n","/**\n * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,\n * 用于替代null和undefined的使用。\n */\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\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\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 被包裹的值,不能为null和undefined\n * @returns Some\n */\nexport function Some<T>(value: T): Option<T> {\n if (value == null) {\n throw new Error('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 };\n}\n\n/**\n * `None`值是固定的\n */\nexport const None: None = {\n kind: 'None',\n isSome: () => false,\n isNone: () => true,\n unwrap: () => {\n throw new Error('None can not unwrap');\n },\n};","/**\n * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,\n * 用于错误处理。\n */\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\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\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.isSome()) {\n const result = await res.unwrap();\n if (result.isErr()) {\n console.assert(result.err().message === 'lose');\n } else {\n console.log(result.unwrap());\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 };\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 };\n}"],"names":[],"version":3,"file":"main.cjs.map"}
1
+ {"mappings":";;;;;;;;;ACAA;;;CAGC,GAED;;CAEC,GAoCM,SAAS,0CAAQ,KAAQ;IAC5B,IAAI,SAAS,MACT,MAAM,IAAI,MAAM;IAGpB,OAAO;QACH,MAAM;QACN,QAAQ,IAAM;QACd,QAAQ,IAAM;QACd,QAAQ,IAAM;IAClB;AACJ;AAMO,MAAM,4CAAa;IACtB,MAAM;IACN,QAAQ,IAAM;IACd,QAAQ,IAAM;IACd,QAAQ;QACJ,MAAM,IAAI,MAAM;IACpB;AACJ;;;ACnEA;;;CAGC,GAGD;;CAEC,GA6DM,SAAS,0CAAS,KAAQ;IAC7B,OAAO;QACH,MAAM;QACN,MAAM,IAAM;QACZ,OAAO,IAAM;QACb,QAAQ,IAAM;IAClB;AACJ;AAeO,SAAS,0CAAU,KAAQ;IAC9B,OAAO;QACH,MAAM;QACN,MAAM,IAAM;QACZ,OAAO,IAAM;QACb,QAAQ;YACJ,MAAM;QACV;QACA,KAAK,IAAM;IACf;AACJ;;","sources":["src/mod.ts","src/enum/option.ts","src/enum/result.ts"],"sourcesContent":["export { None, Some, type Option } from './enum/option.ts';\nexport { Err, Ok, type Result } from './enum/result.ts';\n","/**\n * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,\n * 用于替代null和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 * 创建一个`Some`对象\n *\n * # Examples\n *\n * ```\n * const v = Some(10);\n * console.assert(v.unwrap() === 10);\n * ```\n *\n * @param value 被包裹的值,不能为null和undefined\n * @returns {Some}\n */\nexport function Some<T>(value: T): Option<T> {\n if (value == null) {\n throw new Error('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 Error('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":[],"version":3,"file":"main.cjs.map"}
package/dist/main.mjs CHANGED
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,
3
3
  * 用于替代null和undefined的使用。
4
+ */ /**
5
+ * option::Some type
4
6
  */ function $a89c3c570c820c73$export$9f9d0139d032da4f(value) {
5
7
  if (value == null) throw new Error("Some value can not be null or undefined");
6
8
  return {
@@ -23,6 +25,8 @@ const $a89c3c570c820c73$export$57ca7e07b341709d = {
23
25
  /**
24
26
  * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,
25
27
  * 用于错误处理。
28
+ */ /**
29
+ * result::Ok type
26
30
  */ function $e04e55a8aca8db85$export$8146e38189b4f4dc(value) {
27
31
  return {
28
32
  kind: "Ok",
package/dist/main.mjs.map CHANGED
@@ -1 +1 @@
1
- {"mappings":"ACAA;;;CAGC,GA+BM,SAAS,0CAAQ,KAAQ;IAC5B,IAAI,SAAS,MACT,MAAM,IAAI,MAAM;IAGpB,OAAO;QACH,MAAM;QACN,QAAQ,IAAM;QACd,QAAQ,IAAM;QACd,QAAQ,IAAM;IAClB;AACJ;AAKO,MAAM,4CAAa;IACtB,MAAM;IACN,QAAQ,IAAM;IACd,QAAQ,IAAM;IACd,QAAQ;QACJ,MAAM,IAAI,MAAM;IACpB;AACJ;;;ACzDA;;;CAGC,GAsDM,SAAS,0CAAS,KAAQ;IAC7B,OAAO;QACH,MAAM;QACN,MAAM,IAAM;QACZ,OAAO,IAAM;QACb,QAAQ,IAAM;IAClB;AACJ;AAeO,SAAS,0CAAU,KAAQ;IAC9B,OAAO;QACH,MAAM;QACN,MAAM,IAAM;QACZ,OAAO,IAAM;QACb,QAAQ;YACJ,MAAM;QACV;QACA,KAAK,IAAM;IACf;AACJ;;","sources":["src/mod.ts","src/enum/option.ts","src/enum/result.ts"],"sourcesContent":["export { None, Some, type Option } from './enum/option.ts';\nexport { Err, Ok, type Result } from './enum/result.ts';\n","/**\n * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,\n * 用于替代null和undefined的使用。\n */\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\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\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 被包裹的值,不能为null和undefined\n * @returns Some\n */\nexport function Some<T>(value: T): Option<T> {\n if (value == null) {\n throw new Error('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 };\n}\n\n/**\n * `None`值是固定的\n */\nexport const None: None = {\n kind: 'None',\n isSome: () => false,\n isNone: () => true,\n unwrap: () => {\n throw new Error('None can not unwrap');\n },\n};","/**\n * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,\n * 用于错误处理。\n */\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\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\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.isSome()) {\n const result = await res.unwrap();\n if (result.isErr()) {\n console.assert(result.err().message === 'lose');\n } else {\n console.log(result.unwrap());\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 };\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 };\n}"],"names":[],"version":3,"file":"main.mjs.map"}
1
+ {"mappings":"ACAA;;;CAGC,GAED;;CAEC,GAoCM,SAAS,0CAAQ,KAAQ;IAC5B,IAAI,SAAS,MACT,MAAM,IAAI,MAAM;IAGpB,OAAO;QACH,MAAM;QACN,QAAQ,IAAM;QACd,QAAQ,IAAM;QACd,QAAQ,IAAM;IAClB;AACJ;AAMO,MAAM,4CAAa;IACtB,MAAM;IACN,QAAQ,IAAM;IACd,QAAQ,IAAM;IACd,QAAQ;QACJ,MAAM,IAAI,MAAM;IACpB;AACJ;;;ACnEA;;;CAGC,GAGD;;CAEC,GA6DM,SAAS,0CAAS,KAAQ;IAC7B,OAAO;QACH,MAAM;QACN,MAAM,IAAM;QACZ,OAAO,IAAM;QACb,QAAQ,IAAM;IAClB;AACJ;AAeO,SAAS,0CAAU,KAAQ;IAC9B,OAAO;QACH,MAAM;QACN,MAAM,IAAM;QACZ,OAAO,IAAM;QACb,QAAQ;YACJ,MAAM;QACV;QACA,KAAK,IAAM;IACf;AACJ;;","sources":["src/mod.ts","src/enum/option.ts","src/enum/result.ts"],"sourcesContent":["export { None, Some, type Option } from './enum/option.ts';\nexport { Err, Ok, type Result } from './enum/result.ts';\n","/**\n * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,\n * 用于替代null和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 * 创建一个`Some`对象\n *\n * # Examples\n *\n * ```\n * const v = Some(10);\n * console.assert(v.unwrap() === 10);\n * ```\n *\n * @param value 被包裹的值,不能为null和undefined\n * @returns {Some}\n */\nexport function Some<T>(value: T): Option<T> {\n if (value == null) {\n throw new Error('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 Error('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":[],"version":3,"file":"main.mjs.map"}
package/dist/types.d.ts CHANGED
@@ -2,18 +2,27 @@
2
2
  * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,
3
3
  * 用于替代null和undefined的使用。
4
4
  */
5
+ /**
6
+ * option::Some type
7
+ */
5
8
  export interface Some<T> {
6
9
  readonly kind: 'Some';
7
10
  readonly isSome: (this: Option<T>) => this is Some<T>;
8
11
  readonly isNone: (this: Option<T>) => this is None;
9
12
  readonly unwrap: () => T;
10
13
  }
14
+ /**
15
+ * option::None type
16
+ */
11
17
  export interface None {
12
18
  readonly kind: 'None';
13
19
  readonly isSome: <T>(this: Option<T>) => this is Some<T>;
14
20
  readonly isNone: <T>(this: Option<T>) => this is None;
15
21
  readonly unwrap: () => never;
16
22
  }
23
+ /**
24
+ * option::Option type
25
+ */
17
26
  export type Option<T> = Some<T> | None;
18
27
  /**
19
28
  * 创建一个`Some`对象
@@ -26,23 +35,30 @@ export type Option<T> = Some<T> | None;
26
35
  * ```
27
36
  *
28
37
  * @param value 被包裹的值,不能为null和undefined
29
- * @returns Some
38
+ * @returns {Some}
30
39
  */
31
40
  export function Some<T>(value: T): Option<T>;
32
41
  /**
33
42
  * `None`值是固定的
43
+ * @constant {None}
34
44
  */
35
45
  export const None: None;
36
46
  /**
37
47
  * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,
38
48
  * 用于错误处理。
39
49
  */
50
+ /**
51
+ * result::Ok type
52
+ */
40
53
  export interface Ok<T, E> {
41
54
  readonly kind: 'Ok';
42
55
  readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;
43
56
  readonly isErr: (this: Result<T, E>) => this is Err<T, E>;
44
57
  readonly unwrap: () => T;
45
58
  }
59
+ /**
60
+ * result::Err type
61
+ */
46
62
  export interface Err<T, E> {
47
63
  readonly kind: 'Err';
48
64
  readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;
@@ -50,6 +66,9 @@ export interface Err<T, E> {
50
66
  readonly unwrap: () => never;
51
67
  readonly err: () => E;
52
68
  }
69
+ /**
70
+ * result::Result type
71
+ */
53
72
  export type Result<T, E> = Ok<T, E> | Err<T, E>;
54
73
  /**
55
74
  * 创建一个`Ok`对象
@@ -72,19 +91,21 @@ function judge(n: number): Option<Promise<Result<number, Error>>> {
72
91
  }
73
92
 
74
93
  const res = judge(0.8);
75
- if (res.isSome()) {
94
+ if (res.isNone()) {
95
+ console.error('invalid number');
96
+ } else {
76
97
  const result = await res.unwrap();
77
98
  if (result.isErr()) {
78
99
  console.assert(result.err().message === 'lose');
79
100
  } else {
80
- console.log(result.unwrap());
101
+ console.log(result.unwrap()); // must greater than 0.8
81
102
  }
82
103
  }
83
104
  *
84
105
  * ```
85
106
  *
86
107
  * @param value 被包裹的值
87
- * @returns Ok
108
+ * @returns {Ok}
88
109
  */
89
110
  export function Ok<T, E>(value: T): Result<T, E>;
90
111
  /**
@@ -98,7 +119,7 @@ export function Ok<T, E>(value: T): Result<T, E>;
98
119
  * ```
99
120
  *
100
121
  * @param error 被包裹的错误
101
- * @returns Err
122
+ * @returns {Err}
102
123
  */
103
124
  export function Err<T, E>(error: E): Result<T, E>;
104
125
 
@@ -1 +1 @@
1
- {"mappings":"AAAA;;;GAGG;AAEH,sBAAe,CAAC;IACZ,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC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;IACd,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;IACxD,QAAQ,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;IAC1D,QAAQ,CAAC,MAAM,EAAE,MAAM,KAAK,CAAC;IAC7B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;CACzB;AAED,mBAAmB,CAAC,EAAE,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,mBAAmB,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,EAAE,CAAC,CAAC,CAO/C;AAED;;;;;;;;;;;;GAYG;AACH,oBAAoB,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,EAAE,CAAC,CAAC,CAUhD","sources":["src/src/enum/option.ts","src/src/enum/result.ts","src/src/mod.ts","src/mod.ts"],"sourcesContent":[null,null,null,"export { None, Some, type Option } from './enum/option.ts';\nexport { Err, Ok, type Result } from './enum/result.ts';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
1
+ {"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,sBAAe,CAAC;IACZ,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC;IACtD,QAAQ,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,O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AAO,CAAC,EAAE,CAAC,CAAC,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;IACxD,QAAQ,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;IAC1D,QAAQ,CAAC,MAAM,EAAE,MAAM,KAAK,CAAC;IAC7B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;CACzB;AAED;;GAEG;AACH,mBAAmB,CAAC,EAAE,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,mBAAmB,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,EAAE,CAAC,CAAC,CAO/C;AAED;;;;;;;;;;;;GAYG;AACH,oBAAoB,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,EAAE,CAAC,CAAC,CAUhD","sources":["src/src/enum/option.ts","src/src/enum/result.ts","src/src/mod.ts","src/mod.ts"],"sourcesContent":[null,null,null,"export { None, Some, type Option } from './enum/option.ts';\nexport { Err, Ok, type Result } from './enum/result.ts';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "happy-rusty",
3
- "version": "1.0.1",
4
- "source": "src/mod.ts",
5
- "main": "dist/main.cjs",
6
- "module": "dist/main.mjs",
7
- "types": "dist/types.d.ts",
3
+ "description": "Porting some excellent design implementations from Rust to JavaScript.",
4
+ "author": "jiang115jie@gmail.com",
5
+ "license": "GPL-3.0",
6
+ "version": "1.0.3",
7
+ "source": "./src/mod.ts",
8
+ "main": "./dist/main.cjs",
9
+ "module": "./dist/main.mjs",
10
+ "types": "./dist/types.d.ts",
11
+ "exports": "./src/mod.ts",
8
12
  "targets": {
9
13
  "main": {
10
14
  "isLibrary": true
@@ -14,10 +18,10 @@
14
18
  }
15
19
  },
16
20
  "files": [
17
- "dist",
18
21
  "LICENSE",
19
22
  "README.md",
20
- "package.json"
23
+ "package.json",
24
+ "dist"
21
25
  ],
22
26
  "sideEffects": false,
23
27
  "scripts": {
@@ -44,9 +48,6 @@
44
48
  "Ok",
45
49
  "Err"
46
50
  ],
47
- "author": "jiang115jie@gmail.com",
48
- "license": "GPL-3.0",
49
- "description": "Porting some excellent design implementations from Rust to JavaScript.",
50
51
  "devDependencies": {
51
52
  "@jest/globals": "^29.7.0",
52
53
  "@parcel/packager-ts": "^2.12.0",