happy-rusty 1.0.4 → 1.0.6
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 +2 -2
- package/dist/main.cjs.map +1 -1
- package/dist/main.mjs +2 -2
- package/dist/main.mjs.map +1 -1
- package/dist/types.d.ts +6 -1
- package/package.json +5 -3
- package/src/enum/option.ts +2 -2
- package/src/mod.ts +9 -0
package/dist/main.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
function Some(value) {
|
|
4
4
|
if (value == null) {
|
|
5
|
-
throw new
|
|
5
|
+
throw new TypeError("Some value can not be null or undefined");
|
|
6
6
|
}
|
|
7
7
|
return {
|
|
8
8
|
kind: "Some",
|
|
@@ -16,7 +16,7 @@ const None = {
|
|
|
16
16
|
isSome: () => false,
|
|
17
17
|
isNone: () => true,
|
|
18
18
|
unwrap: () => {
|
|
19
|
-
throw new
|
|
19
|
+
throw new TypeError("None can not unwrap");
|
|
20
20
|
}
|
|
21
21
|
};
|
|
22
22
|
|
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 * 用于替代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
|
|
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 * 用于替代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 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;;;;;;;"}
|
package/dist/main.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
function Some(value) {
|
|
2
2
|
if (value == null) {
|
|
3
|
-
throw new
|
|
3
|
+
throw new TypeError("Some value can not be null or undefined");
|
|
4
4
|
}
|
|
5
5
|
return {
|
|
6
6
|
kind: "Some",
|
|
@@ -14,7 +14,7 @@ const None = {
|
|
|
14
14
|
isSome: () => false,
|
|
15
15
|
isNone: () => true,
|
|
16
16
|
unwrap: () => {
|
|
17
|
-
throw new
|
|
17
|
+
throw new TypeError("None can not unwrap");
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
20
|
|
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 * 用于替代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
|
|
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 * 用于替代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 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;;;;"}
|
package/dist/types.d.ts
CHANGED
|
@@ -124,5 +124,10 @@ interface Err<T, E> {
|
|
|
124
124
|
*/
|
|
125
125
|
declare function Err<T, E>(error: E): Result<T, E>;
|
|
126
126
|
|
|
127
|
-
|
|
127
|
+
type AsyncOption<T> = Promise<Option<T>>;
|
|
128
|
+
type AsyncResult<T, E> = Promise<Result<T, E>>;
|
|
129
|
+
type IOResult<T> = Result<T, Error>;
|
|
130
|
+
type AsyncIOResult<T> = Promise<IOResult<T>>;
|
|
131
|
+
|
|
132
|
+
export { type AsyncIOResult, type AsyncOption, type AsyncResult, Err, type IOResult, None, Ok, type Option, type Result, Some };
|
|
128
133
|
//# sourceMappingURL=types.d.ts.map
|
package/package.json
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
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
|
+
"version": "1.0.6",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"source": "./src/mod.ts",
|
|
8
9
|
"main": "./dist/main.cjs",
|
|
9
10
|
"module": "./dist/main.mjs",
|
|
@@ -42,10 +43,11 @@
|
|
|
42
43
|
],
|
|
43
44
|
"devDependencies": {
|
|
44
45
|
"@jest/globals": "^29.7.0",
|
|
45
|
-
"@typescript-eslint/eslint-plugin": "^7.
|
|
46
|
-
"@typescript-eslint/parser": "^7.
|
|
46
|
+
"@typescript-eslint/eslint-plugin": "^7.8.0",
|
|
47
|
+
"@typescript-eslint/parser": "^7.8.0",
|
|
47
48
|
"eslint": "^8.57.0",
|
|
48
49
|
"rimraf": "^5.0.5",
|
|
50
|
+
"rollup": "^4.17.2",
|
|
49
51
|
"rollup-plugin-dts": "^6.1.0",
|
|
50
52
|
"rollup-plugin-esbuild": "^6.1.1",
|
|
51
53
|
"typescript": "^5.4.5"
|
package/src/enum/option.ts
CHANGED
|
@@ -43,7 +43,7 @@ export type Option<T> = Some<T> | None;
|
|
|
43
43
|
*/
|
|
44
44
|
export function Some<T>(value: T): Option<T> {
|
|
45
45
|
if (value == null) {
|
|
46
|
-
throw new
|
|
46
|
+
throw new TypeError('Some value can not be null or undefined');
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
return {
|
|
@@ -63,6 +63,6 @@ export const None: None = {
|
|
|
63
63
|
isSome: () => false,
|
|
64
64
|
isNone: () => true,
|
|
65
65
|
unwrap: () => {
|
|
66
|
-
throw new
|
|
66
|
+
throw new TypeError('None can not unwrap');
|
|
67
67
|
},
|
|
68
68
|
} as const;
|
package/src/mod.ts
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
+
import type { Option } from './enum/option.ts';
|
|
2
|
+
import type { Result } from './enum/result.ts';
|
|
3
|
+
|
|
1
4
|
export { None, Some, type Option } from './enum/option.ts';
|
|
2
5
|
export { Err, Ok, type Result } from './enum/result.ts';
|
|
6
|
+
|
|
7
|
+
// export some commonly used types
|
|
8
|
+
export type AsyncOption<T> = Promise<Option<T>>;
|
|
9
|
+
export type AsyncResult<T, E> = Promise<Result<T, E>>;
|
|
10
|
+
export type IOResult<T> = Result<T, Error>;
|
|
11
|
+
export type AsyncIOResult<T> = Promise<IOResult<T>>;
|