happy-rusty 1.0.0 → 1.0.2

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/dist/main.cjs CHANGED
@@ -3,18 +3,16 @@ function $parcel$export(e, n, v, s) {
3
3
  Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
4
4
  }
5
5
 
6
- $parcel$export(module.exports, "Some", () => $49294c54424499ce$export$9f9d0139d032da4f);
7
6
  $parcel$export(module.exports, "None", () => $49294c54424499ce$export$57ca7e07b341709d);
8
- $parcel$export(module.exports, "Ok", () => $0d2eaee7439556da$export$8146e38189b4f4dc);
7
+ $parcel$export(module.exports, "Some", () => $49294c54424499ce$export$9f9d0139d032da4f);
9
8
  $parcel$export(module.exports, "Err", () => $0d2eaee7439556da$export$3659d3f2d3dfceb8);
9
+ $parcel$export(module.exports, "Ok", () => $0d2eaee7439556da$export$8146e38189b4f4dc);
10
10
  /**
11
11
  * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,
12
12
  * 用于替代null和undefined的使用。
13
13
  */ /**
14
- * kinds of Option
15
- * @internal
16
- */ var $49294c54424499ce$var$OptionKind;
17
- function $49294c54424499ce$export$9f9d0139d032da4f(value) {
14
+ * option::Some type
15
+ */ function $49294c54424499ce$export$9f9d0139d032da4f(value) {
18
16
  if (value == null) throw new Error("Some value can not be null or undefined");
19
17
  return {
20
18
  kind: "Some",
@@ -37,10 +35,8 @@ const $49294c54424499ce$export$57ca7e07b341709d = {
37
35
  * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,
38
36
  * 用于错误处理。
39
37
  */ /**
40
- * kinds of Result
41
- * @internal
42
- */ var $0d2eaee7439556da$var$ResultKind;
43
- function $0d2eaee7439556da$export$8146e38189b4f4dc(value) {
38
+ * result::Ok type
39
+ */ function $0d2eaee7439556da$export$8146e38189b4f4dc(value) {
44
40
  return {
45
41
  kind: "Ok",
46
42
  isOk: ()=>true,
package/dist/main.cjs.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;ACAA;;;CAGC,GAED;;;CAGC;AAmCM,SAAS,0CAAQ,KAAQ;IAC5B,IAAI,SAAS,MACT,MAAM,IAAI,MAAM;IAGpB,OAAO;QACH,IAAI;QACJ,QAAQ,IAAM;QACd,QAAQ,IAAM;QACd,QAAQ,IAAM;IAClB;AACJ;AAKO,MAAM,4CAAa;IACtB,IAAI;IACJ,QAAQ,IAAM;IACd,QAAQ,IAAM;IACd,QAAQ;QACJ,MAAM,IAAI,MAAM;IACpB;AACJ;;;AClEA;;;CAGC,GAED;;;CAGC;AA0DM,SAAS,0CAAS,KAAQ;IAC7B,OAAO;QACH,IAAI;QACJ,MAAM,IAAM;QACZ,OAAO,IAAM;QACb,QAAQ,IAAM;IAClB;AACJ;AAeO,SAAS,0CAAU,KAAQ;IAC9B,OAAO;QACH,IAAI;QACJ,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 { Some, None, type Option } from './enum/option.ts';\nexport { Ok, Err, type Result } from './enum/result.ts';","/**\n * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,\n * 用于替代null和undefined的使用。\n */\n\n/**\n * kinds of Option\n * @internal\n */\nconst enum OptionKind {\n Some = 'Some',\n None = 'None',\n}\n\ninterface Some<T> {\n readonly kind: OptionKind.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: OptionKind.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: OptionKind.Some,\n isSome: () => true,\n isNone: () => false,\n unwrap: () => value,\n };\n}\n\n/**\n * `None`值是固定的\n */\nexport const None: None = {\n kind: OptionKind.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\n/**\n * kinds of Result\n * @internal\n */\nconst enum ResultKind {\n Ok = 'Ok',\n Err = 'Err',\n}\n\ninterface Ok<T, E> {\n readonly kind: ResultKind.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: ResultKind.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: ResultKind.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: ResultKind.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,GAED;;CAEC,GA2DM,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 };\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};","/**\n * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,\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.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"}
package/dist/main.mjs CHANGED
@@ -2,10 +2,8 @@
2
2
  * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,
3
3
  * 用于替代null和undefined的使用。
4
4
  */ /**
5
- * kinds of Option
6
- * @internal
7
- */ var $a89c3c570c820c73$var$OptionKind;
8
- function $a89c3c570c820c73$export$9f9d0139d032da4f(value) {
5
+ * option::Some type
6
+ */ function $a89c3c570c820c73$export$9f9d0139d032da4f(value) {
9
7
  if (value == null) throw new Error("Some value can not be null or undefined");
10
8
  return {
11
9
  kind: "Some",
@@ -28,10 +26,8 @@ const $a89c3c570c820c73$export$57ca7e07b341709d = {
28
26
  * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,
29
27
  * 用于错误处理。
30
28
  */ /**
31
- * kinds of Result
32
- * @internal
33
- */ var $e04e55a8aca8db85$var$ResultKind;
34
- function $e04e55a8aca8db85$export$8146e38189b4f4dc(value) {
29
+ * result::Ok type
30
+ */ function $e04e55a8aca8db85$export$8146e38189b4f4dc(value) {
35
31
  return {
36
32
  kind: "Ok",
37
33
  isOk: ()=>true,
@@ -54,5 +50,5 @@ function $e04e55a8aca8db85$export$3659d3f2d3dfceb8(error) {
54
50
 
55
51
 
56
52
 
57
- export {$a89c3c570c820c73$export$9f9d0139d032da4f as Some, $a89c3c570c820c73$export$57ca7e07b341709d as None, $e04e55a8aca8db85$export$8146e38189b4f4dc as Ok, $e04e55a8aca8db85$export$3659d3f2d3dfceb8 as Err};
53
+ export {$a89c3c570c820c73$export$57ca7e07b341709d as None, $a89c3c570c820c73$export$9f9d0139d032da4f as Some, $e04e55a8aca8db85$export$3659d3f2d3dfceb8 as Err, $e04e55a8aca8db85$export$8146e38189b4f4dc as Ok};
58
54
  //# sourceMappingURL=main.mjs.map
package/dist/main.mjs.map CHANGED
@@ -1 +1 @@
1
- {"mappings":"ACAA;;;CAGC,GAED;;;CAGC;AAmCM,SAAS,0CAAQ,KAAQ;IAC5B,IAAI,SAAS,MACT,MAAM,IAAI,MAAM;IAGpB,OAAO;QACH,IAAI;QACJ,QAAQ,IAAM;QACd,QAAQ,IAAM;QACd,QAAQ,IAAM;IAClB;AACJ;AAKO,MAAM,4CAAa;IACtB,IAAI;IACJ,QAAQ,IAAM;IACd,QAAQ,IAAM;IACd,QAAQ;QACJ,MAAM,IAAI,MAAM;IACpB;AACJ;;;AClEA;;;CAGC,GAED;;;CAGC;AA0DM,SAAS,0CAAS,KAAQ;IAC7B,OAAO;QACH,IAAI;QACJ,MAAM,IAAM;QACZ,OAAO,IAAM;QACb,QAAQ,IAAM;IAClB;AACJ;AAeO,SAAS,0CAAU,KAAQ;IAC9B,OAAO;QACH,IAAI;QACJ,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 { Some, None, type Option } from './enum/option.ts';\nexport { Ok, Err, type Result } from './enum/result.ts';","/**\n * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,\n * 用于替代null和undefined的使用。\n */\n\n/**\n * kinds of Option\n * @internal\n */\nconst enum OptionKind {\n Some = 'Some',\n None = 'None',\n}\n\ninterface Some<T> {\n readonly kind: OptionKind.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: OptionKind.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: OptionKind.Some,\n isSome: () => true,\n isNone: () => false,\n unwrap: () => value,\n };\n}\n\n/**\n * `None`值是固定的\n */\nexport const None: None = {\n kind: OptionKind.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\n/**\n * kinds of Result\n * @internal\n */\nconst enum ResultKind {\n Ok = 'Ok',\n Err = 'Err',\n}\n\ninterface Ok<T, E> {\n readonly kind: ResultKind.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: ResultKind.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: ResultKind.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: ResultKind.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,GAED;;CAEC,GA2DM,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 };\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};","/**\n * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,\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.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"}
package/dist/types.d.ts CHANGED
@@ -3,25 +3,26 @@
3
3
  * 用于替代null和undefined的使用。
4
4
  */
5
5
  /**
6
- * kinds of Option
7
- * @internal
6
+ * option::Some type
8
7
  */
9
- const enum OptionKind {
10
- Some = "Some",
11
- None = "None"
12
- }
13
8
  export interface Some<T> {
14
- readonly kind: OptionKind.Some;
9
+ readonly kind: 'Some';
15
10
  readonly isSome: (this: Option<T>) => this is Some<T>;
16
11
  readonly isNone: (this: Option<T>) => this is None;
17
12
  readonly unwrap: () => T;
18
13
  }
14
+ /**
15
+ * option::None type
16
+ */
19
17
  export interface None {
20
- readonly kind: OptionKind.None;
18
+ readonly kind: 'None';
21
19
  readonly isSome: <T>(this: Option<T>) => this is Some<T>;
22
20
  readonly isNone: <T>(this: Option<T>) => this is None;
23
21
  readonly unwrap: () => never;
24
22
  }
23
+ /**
24
+ * option::Option type
25
+ */
25
26
  export type Option<T> = Some<T> | None;
26
27
  /**
27
28
  * 创建一个`Some`对象
@@ -34,11 +35,12 @@ export type Option<T> = Some<T> | None;
34
35
  * ```
35
36
  *
36
37
  * @param value 被包裹的值,不能为null和undefined
37
- * @returns Some
38
+ * @returns {Some}
38
39
  */
39
40
  export function Some<T>(value: T): Option<T>;
40
41
  /**
41
42
  * `None`值是固定的
43
+ * @constant {None}
42
44
  */
43
45
  export const None: None;
44
46
  /**
@@ -46,26 +48,27 @@ export const None: None;
46
48
  * 用于错误处理。
47
49
  */
48
50
  /**
49
- * kinds of Result
50
- * @internal
51
+ * result::Ok type
51
52
  */
52
- const enum ResultKind {
53
- Ok = "Ok",
54
- Err = "Err"
55
- }
56
53
  export interface Ok<T, E> {
57
- readonly kind: ResultKind.Ok;
54
+ readonly kind: 'Ok';
58
55
  readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;
59
56
  readonly isErr: (this: Result<T, E>) => this is Err<T, E>;
60
57
  readonly unwrap: () => T;
61
58
  }
59
+ /**
60
+ * result::Err type
61
+ */
62
62
  export interface Err<T, E> {
63
- readonly kind: ResultKind.Err;
63
+ readonly kind: 'Err';
64
64
  readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;
65
65
  readonly isErr: (this: Result<T, E>) => this is Err<T, E>;
66
66
  readonly unwrap: () => never;
67
67
  readonly err: () => E;
68
68
  }
69
+ /**
70
+ * result::Result type
71
+ */
69
72
  export type Result<T, E> = Ok<T, E> | Err<T, E>;
70
73
  /**
71
74
  * 创建一个`Ok`对象
@@ -100,7 +103,7 @@ if (res.isSome()) {
100
103
  * ```
101
104
  *
102
105
  * @param value 被包裹的值
103
- * @returns Ok
106
+ * @returns {Ok}
104
107
  */
105
108
  export function Ok<T, E>(value: T): Result<T, E>;
106
109
  /**
@@ -114,7 +117,7 @@ export function Ok<T, E>(value: T): Result<T, E>;
114
117
  * ```
115
118
  *
116
119
  * @param error 被包裹的错误
117
- * @returns Err
120
+ * @returns {Err}
118
121
  */
119
122
  export function Err<T, E>(error: E): Result<T, E>;
120
123
 
@@ -1 +1 @@
1
- {"mappings":"AAAA;;;GAGG;AAEH;;;GAGG;AACH,KAAK;IACD,aAAa;IACb,aAAa;CAChB;AAED,sBAAe,CAAC;IACZ,QAAQ,CAAC,IAAI,EAAE,WAAW,IAAI,CAAC;IAC/B,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,OAAO,CAAC,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC;IACnD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC5B;AAED;IACI,QAAQ,CAAC,IAAI,EAAE,WAAW,IAAI,CAAC;IAC/B,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC;IACzD,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC;IACtD,QAAQ,CAAC,MAAM,EAAE,MAAM,KAAK,CAAC;CAChC;AAED,mBAAmB,CAAC,IAAI,KAAK,CAAC,CAAC,GAAG,IAAI,CAAC;AAEvC;;;;;;;;;;;;GAYG;AACH,qBAAqB,CAAC,EAAE,KAAK,EAAE,CAA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AAO,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 { Some, None, type Option } from './enum/option';\nexport { Ok, Err, type Result } from './enum/result';"],"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,OAAO,CAAC,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC;IACnD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC5B;AAED;;GAEG;AACH;IACI,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC;IACzD,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC;IACtD,QAAQ,CAAC,MAAM,EAAE,MAAM,KAAK,CAAC;CAChC;AAED;;GAEG;AACH,mBAAmB,CAAC,IAAI,KAAK,CAAC,CAAC,GAAG,IAAI,CAAC;AAEvC;;;;;;;;;;;;GAYG;AACH,qBAAqB,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAW3C;AAED;;;GAGG;AACH,OAAO,MAAM,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,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.0",
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.2",
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,11 +18,12 @@
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
  ],
26
+ "sideEffects": false,
22
27
  "scripts": {
23
28
  "clean": "npx rimraf .parcel-cache dist",
24
29
  "check": "npx tsc --noEmit",
@@ -43,9 +48,6 @@
43
48
  "Ok",
44
49
  "Err"
45
50
  ],
46
- "author": "jiang115jie@gmail.com",
47
- "license": "GPL-3.0",
48
- "description": "Porting some excellent design implementations from Rust to JavaScript.",
49
51
  "devDependencies": {
50
52
  "@jest/globals": "^29.7.0",
51
53
  "@parcel/packager-ts": "^2.12.0",