happy-rusty 1.0.0 → 1.0.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.
package/dist/main.cjs CHANGED
@@ -3,18 +3,14 @@ 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
- */ /**
14
- * kinds of Option
15
- * @internal
16
- */ var $49294c54424499ce$var$OptionKind;
17
- function $49294c54424499ce$export$9f9d0139d032da4f(value) {
13
+ */ function $49294c54424499ce$export$9f9d0139d032da4f(value) {
18
14
  if (value == null) throw new Error("Some value can not be null or undefined");
19
15
  return {
20
16
  kind: "Some",
@@ -36,11 +32,7 @@ const $49294c54424499ce$export$57ca7e07b341709d = {
36
32
  /**
37
33
  * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,
38
34
  * 用于错误处理。
39
- */ /**
40
- * kinds of Result
41
- * @internal
42
- */ var $0d2eaee7439556da$var$ResultKind;
43
- function $0d2eaee7439556da$export$8146e38189b4f4dc(value) {
35
+ */ function $0d2eaee7439556da$export$8146e38189b4f4dc(value) {
44
36
  return {
45
37
  kind: "Ok",
46
38
  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,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"}
package/dist/main.mjs CHANGED
@@ -1,11 +1,7 @@
1
1
  /**
2
2
  * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,
3
3
  * 用于替代null和undefined的使用。
4
- */ /**
5
- * kinds of Option
6
- * @internal
7
- */ var $a89c3c570c820c73$var$OptionKind;
8
- function $a89c3c570c820c73$export$9f9d0139d032da4f(value) {
4
+ */ function $a89c3c570c820c73$export$9f9d0139d032da4f(value) {
9
5
  if (value == null) throw new Error("Some value can not be null or undefined");
10
6
  return {
11
7
  kind: "Some",
@@ -27,11 +23,7 @@ const $a89c3c570c820c73$export$57ca7e07b341709d = {
27
23
  /**
28
24
  * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,
29
25
  * 用于错误处理。
30
- */ /**
31
- * kinds of Result
32
- * @internal
33
- */ var $e04e55a8aca8db85$var$ResultKind;
34
- function $e04e55a8aca8db85$export$8146e38189b4f4dc(value) {
26
+ */ function $e04e55a8aca8db85$export$8146e38189b4f4dc(value) {
35
27
  return {
36
28
  kind: "Ok",
37
29
  isOk: ()=>true,
@@ -54,5 +46,5 @@ function $e04e55a8aca8db85$export$3659d3f2d3dfceb8(error) {
54
46
 
55
47
 
56
48
 
57
- export {$a89c3c570c820c73$export$9f9d0139d032da4f as Some, $a89c3c570c820c73$export$57ca7e07b341709d as None, $e04e55a8aca8db85$export$8146e38189b4f4dc as Ok, $e04e55a8aca8db85$export$3659d3f2d3dfceb8 as Err};
49
+ export {$a89c3c570c820c73$export$57ca7e07b341709d as None, $a89c3c570c820c73$export$9f9d0139d032da4f as Some, $e04e55a8aca8db85$export$3659d3f2d3dfceb8 as Err, $e04e55a8aca8db85$export$8146e38189b4f4dc as Ok};
58
50
  //# 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,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"}
package/dist/types.d.ts CHANGED
@@ -2,22 +2,14 @@
2
2
  * @fileoverview 仿rust的[Option](https://doc.rust-lang.org/core/option/index.html)枚举,
3
3
  * 用于替代null和undefined的使用。
4
4
  */
5
- /**
6
- * kinds of Option
7
- * @internal
8
- */
9
- const enum OptionKind {
10
- Some = "Some",
11
- None = "None"
12
- }
13
5
  export interface Some<T> {
14
- readonly kind: OptionKind.Some;
6
+ readonly kind: 'Some';
15
7
  readonly isSome: (this: Option<T>) => this is Some<T>;
16
8
  readonly isNone: (this: Option<T>) => this is None;
17
9
  readonly unwrap: () => T;
18
10
  }
19
11
  export interface None {
20
- readonly kind: OptionKind.None;
12
+ readonly kind: 'None';
21
13
  readonly isSome: <T>(this: Option<T>) => this is Some<T>;
22
14
  readonly isNone: <T>(this: Option<T>) => this is None;
23
15
  readonly unwrap: () => never;
@@ -45,22 +37,14 @@ export const None: None;
45
37
  * @fileoverview 仿rust的[Result](https://doc.rust-lang.org/core/result/index.html)枚举,
46
38
  * 用于错误处理。
47
39
  */
48
- /**
49
- * kinds of Result
50
- * @internal
51
- */
52
- const enum ResultKind {
53
- Ok = "Ok",
54
- Err = "Err"
55
- }
56
40
  export interface Ok<T, E> {
57
- readonly kind: ResultKind.Ok;
41
+ readonly kind: 'Ok';
58
42
  readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;
59
43
  readonly isErr: (this: Result<T, E>) => this is Err<T, E>;
60
44
  readonly unwrap: () => T;
61
45
  }
62
46
  export interface Err<T, E> {
63
- readonly kind: ResultKind.Err;
47
+ readonly kind: 'Err';
64
48
  readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;
65
49
  readonly isErr: (this: Result<T, E>) => this is Err<T, E>;
66
50
  readonly unwrap: () => never;
@@ -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,KAA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C,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 { 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,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;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,mBAAmB,CAAC,IAAI,KAAK,CAAC,CAAC,GAAG,IAAI,CAAC;AAEvC;;;;;;;;;;;;GAY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C,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,6 +1,6 @@
1
1
  {
2
2
  "name": "happy-rusty",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "source": "src/mod.ts",
5
5
  "main": "dist/main.cjs",
6
6
  "module": "dist/main.mjs",
@@ -19,6 +19,7 @@
19
19
  "README.md",
20
20
  "package.json"
21
21
  ],
22
+ "sideEffects": false,
22
23
  "scripts": {
23
24
  "clean": "npx rimraf .parcel-cache dist",
24
25
  "check": "npx tsc --noEmit",