happy-rusty 1.0.9 → 1.1.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/README.cn.md +15 -35
- package/README.md +15 -35
- package/dist/main.cjs +198 -19
- package/dist/main.cjs.map +1 -1
- package/dist/main.mjs +198 -20
- package/dist/main.mjs.map +1 -1
- package/dist/types.d.ts +520 -105
- package/docs/README.md +37 -0
- package/docs/functions/Err.md +46 -0
- package/docs/functions/Ok.md +46 -0
- package/docs/functions/Some.md +45 -0
- package/docs/functions/promiseToResult.md +50 -0
- package/docs/interfaces/None.md +827 -0
- package/docs/interfaces/Option.md +737 -0
- package/docs/interfaces/Result.md +696 -0
- package/docs/type-aliases/AsyncIOResult.md +24 -0
- package/docs/type-aliases/AsyncOption.md +24 -0
- package/docs/type-aliases/AsyncResult.md +25 -0
- package/docs/type-aliases/IOResult.md +24 -0
- package/docs/variables/None.md +18 -0
- package/package.json +17 -11
- package/src/enum/prelude.ts +963 -0
- package/src/mod.ts +13 -27
- package/src/enum/option.ts +0 -68
- package/src/enum/result.ts +0 -104
package/src/mod.ts
CHANGED
|
@@ -1,27 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* The shorthand of Promise<Result<T, E>>.
|
|
16
|
-
*/
|
|
17
|
-
export type AsyncResult<T, E> = Promise<Result<T, E>>;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* The shorthand of Result<T, Error>.
|
|
21
|
-
*/
|
|
22
|
-
export type IOResult<T> = Result<T, Error>;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* The shorthand of Promise<Result<T, Error>>.
|
|
26
|
-
*/
|
|
27
|
-
export type AsyncIOResult<T> = Promise<IOResult<T>>;
|
|
1
|
+
export {
|
|
2
|
+
Err,
|
|
3
|
+
None,
|
|
4
|
+
Ok,
|
|
5
|
+
Some,
|
|
6
|
+
promiseToResult,
|
|
7
|
+
type AsyncIOResult,
|
|
8
|
+
type AsyncOption,
|
|
9
|
+
type AsyncResult,
|
|
10
|
+
type IOResult,
|
|
11
|
+
type Option,
|
|
12
|
+
type Result
|
|
13
|
+
} from './enum/prelude.ts';
|
package/src/enum/option.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/**
|
|
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.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* option::Some type
|
|
7
|
-
*/
|
|
8
|
-
interface Some<T> {
|
|
9
|
-
readonly kind: 'Some';
|
|
10
|
-
readonly isSome: (this: Option<T>) => this is Some<T>;
|
|
11
|
-
readonly isNone: (this: Option<T>) => this is None;
|
|
12
|
-
readonly unwrap: () => T;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* option::None type
|
|
17
|
-
*/
|
|
18
|
-
interface None {
|
|
19
|
-
readonly kind: 'None';
|
|
20
|
-
readonly isSome: <T>(this: Option<T>) => this is Some<T>;
|
|
21
|
-
readonly isNone: <T>(this: Option<T>) => this is None;
|
|
22
|
-
readonly unwrap: () => never;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* option::Option type
|
|
27
|
-
*/
|
|
28
|
-
export type Option<T> = Some<T> | None;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Create a `Some` object.
|
|
32
|
-
*
|
|
33
|
-
* # Examples
|
|
34
|
-
*
|
|
35
|
-
* ```
|
|
36
|
-
* const v = Some(10);
|
|
37
|
-
* console.assert(v.unwrap() === 10);
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* @param value The wrapped value which can not be null or undefined.
|
|
41
|
-
* @returns {Some}
|
|
42
|
-
*/
|
|
43
|
-
export function Some<T>(value: NonNullable<T>): Option<T> {
|
|
44
|
-
if (value == null) {
|
|
45
|
-
throw new TypeError('Some value can not be null or undefined');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
kind: 'Some',
|
|
50
|
-
isSome: () => true,
|
|
51
|
-
isNone: () => false,
|
|
52
|
-
unwrap: () => value,
|
|
53
|
-
} as const;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* `None` value is freeze.
|
|
58
|
-
*
|
|
59
|
-
* @constant {None}
|
|
60
|
-
*/
|
|
61
|
-
export const None: None = {
|
|
62
|
-
kind: 'None',
|
|
63
|
-
isSome: () => false,
|
|
64
|
-
isNone: () => true,
|
|
65
|
-
unwrap: () => {
|
|
66
|
-
throw new TypeError('None can not unwrap');
|
|
67
|
-
},
|
|
68
|
-
} as const;
|
package/src/enum/result.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview A Rust-inspired [Result](https://doc.rust-lang.org/core/result/index.html) enum, used for better error handling.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* result::Ok type
|
|
7
|
-
*/
|
|
8
|
-
interface Ok<T, E> {
|
|
9
|
-
readonly kind: 'Ok';
|
|
10
|
-
readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;
|
|
11
|
-
readonly isErr: (this: Result<T, E>) => this is Err<T, E>;
|
|
12
|
-
readonly unwrap: () => T;
|
|
13
|
-
readonly err: () => never;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* result::Err type
|
|
18
|
-
*/
|
|
19
|
-
interface Err<T, E> {
|
|
20
|
-
readonly kind: 'Err';
|
|
21
|
-
readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;
|
|
22
|
-
readonly isErr: (this: Result<T, E>) => this is Err<T, E>;
|
|
23
|
-
readonly unwrap: () => never;
|
|
24
|
-
readonly err: () => E;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* result::Result type
|
|
29
|
-
*/
|
|
30
|
-
export type Result<T, E> = Ok<T, E> | Err<T, E>;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Create an `Ok` object.
|
|
34
|
-
*
|
|
35
|
-
* # Examples
|
|
36
|
-
*
|
|
37
|
-
* ```
|
|
38
|
-
* const v = Ok(10);
|
|
39
|
-
* console.assert(v.unwrap() === 10);
|
|
40
|
-
*
|
|
41
|
-
function judge(n: number): Option<Promise<Result<number, Error>>> {
|
|
42
|
-
if (n < 0 || n >= 1) {
|
|
43
|
-
return None;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return Some(new Promise(resolve => {
|
|
47
|
-
const r = Math.random();
|
|
48
|
-
resolve(r > n ? Ok(r) : Err(new Error('lose')));
|
|
49
|
-
}));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const res = judge(0.8);
|
|
53
|
-
if (res.isNone()) {
|
|
54
|
-
console.error('invalid number');
|
|
55
|
-
} else {
|
|
56
|
-
const result = await res.unwrap();
|
|
57
|
-
if (result.isErr()) {
|
|
58
|
-
console.assert(result.err().message === 'lose');
|
|
59
|
-
} else {
|
|
60
|
-
console.log(result.unwrap()); // must greater than 0.8
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
*
|
|
64
|
-
* ```
|
|
65
|
-
*
|
|
66
|
-
* @param value The wrapped value.
|
|
67
|
-
* @returns {Ok}
|
|
68
|
-
*/
|
|
69
|
-
export function Ok<T, E>(value: T): Result<T, E> {
|
|
70
|
-
return {
|
|
71
|
-
kind: 'Ok',
|
|
72
|
-
isOk: () => true,
|
|
73
|
-
isErr: () => false,
|
|
74
|
-
unwrap: () => value,
|
|
75
|
-
err: () => {
|
|
76
|
-
throw new TypeError('Ok is not Err');
|
|
77
|
-
},
|
|
78
|
-
} as const;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Create an `Err` object.
|
|
83
|
-
*
|
|
84
|
-
* # Examples
|
|
85
|
-
*
|
|
86
|
-
* ```
|
|
87
|
-
* const e = Err(new Error('unknown error'));
|
|
88
|
-
* console.assert(e.err().message === 'unknown error');
|
|
89
|
-
* ```
|
|
90
|
-
*
|
|
91
|
-
* @param error The wrapped error value.
|
|
92
|
-
* @returns {Err}
|
|
93
|
-
*/
|
|
94
|
-
export function Err<T, E>(error: E): Result<T, E> {
|
|
95
|
-
return {
|
|
96
|
-
kind: 'Err',
|
|
97
|
-
isOk: () => false,
|
|
98
|
-
isErr: () => true,
|
|
99
|
-
unwrap: () => {
|
|
100
|
-
throw error;
|
|
101
|
-
},
|
|
102
|
-
err: () => error,
|
|
103
|
-
} as const;
|
|
104
|
-
}
|