@madkarma/result 3.0.2 → 4.0.0
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 +8 -0
- package/dist/errors.d.ts +13 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +31 -0
- package/dist/errors.js.map +1 -0
- package/dist/option.d.ts +47 -2
- package/dist/option.d.ts.map +1 -1
- package/dist/option.js +51 -5
- package/dist/option.js.map +1 -1
- package/dist/result.d.ts +39 -4
- package/dist/result.d.ts.map +1 -1
- package/dist/result.js +47 -11
- package/dist/result.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -90,6 +90,14 @@ console.log(displayName);
|
|
|
90
90
|
|
|
91
91
|
Instead of Rust's `match` expressions, check `Result` values as `{ value, error }` directly and use a `switch` on `error.code` to emulate pattern matching. `Option` values are handled directly with `isSome()` / `isNone()` checks.
|
|
92
92
|
|
|
93
|
+
## Error philosophy
|
|
94
|
+
|
|
95
|
+
Some methods (for example `expect`, `unwrap`, `expectErr`, and `unwrapErr`) intentionally panic to mirror Rust behavior.
|
|
96
|
+
|
|
97
|
+
If you ever get a non-panic JavaScript error from this library, that usually means invalid runtime data was passed in (library misuse, type-system bypass, or garbage input), and the call site should be fixed.
|
|
98
|
+
|
|
99
|
+
Error classes are intentionally not exported, this encourages treating thrown JavaScript errors as unexpected behavior rather than a normal control-flow path handled with `try/catch`.
|
|
100
|
+
|
|
93
101
|
## Contributing
|
|
94
102
|
|
|
95
103
|
Contributions are welcome! Feel free to [open an issue](https://github.com/madkarmaa/result-ts/issues/new) or submit a pull request if you'd like to improve the library or fix any bugs.
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type ResultError } from './result';
|
|
2
|
+
export declare class PanicError extends Error {
|
|
3
|
+
constructor(message: string);
|
|
4
|
+
}
|
|
5
|
+
export declare class InvalidArgumentError extends Error {
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
export declare class FlattenError extends InvalidArgumentError {
|
|
9
|
+
constructor(message: string);
|
|
10
|
+
}
|
|
11
|
+
export declare function assertValueIsNotMissing<T>(value: T | null | undefined): asserts value is T;
|
|
12
|
+
export declare function assertIsResultError(error: unknown): asserts error is ResultError;
|
|
13
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,UAAU,CAAC;AAE5C,qBAAa,UAAW,SAAQ,KAAK;gBACrB,OAAO,EAAE,MAAM;CAI9B;AAED,qBAAa,oBAAqB,SAAQ,KAAK;gBAC/B,OAAO,EAAE,MAAM;CAI9B;AAED,qBAAa,YAAa,SAAQ,oBAAoB;gBACtC,OAAO,EAAE,MAAM;CAI9B;AAED,wBAAgB,uBAAuB,CAAC,CAAC,EACrC,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,GAC5B,OAAO,CAAC,KAAK,IAAI,CAAC,CAKpB;AAED,wBAAgB,mBAAmB,CAC/B,KAAK,EAAE,OAAO,GACf,OAAO,CAAC,KAAK,IAAI,WAAW,CAW9B"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {} from './result';
|
|
2
|
+
export class PanicError extends Error {
|
|
3
|
+
constructor(message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'PanicError';
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export class InvalidArgumentError extends Error {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = 'InvalidArgumentError';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export class FlattenError extends InvalidArgumentError {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = 'FlattenError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function assertValueIsNotMissing(value) {
|
|
21
|
+
if (value === null || value === undefined)
|
|
22
|
+
throw new InvalidArgumentError('Expected a non-null, non-undefined value');
|
|
23
|
+
}
|
|
24
|
+
export function assertIsResultError(error) {
|
|
25
|
+
assertValueIsNotMissing(error);
|
|
26
|
+
if (typeof error !== 'object' ||
|
|
27
|
+
error === null ||
|
|
28
|
+
typeof error.code !== 'string')
|
|
29
|
+
throw new InvalidArgumentError("Expected an object with a string 'code' property");
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,MAAM,UAAU,CAAC;AAE5C,MAAM,OAAO,UAAW,SAAQ,KAAK;IACjC,YAAY,OAAe;QACvB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;IAC7B,CAAC;CACJ;AAED,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAC3C,YAAY,OAAe;QACvB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACvC,CAAC;CACJ;AAED,MAAM,OAAO,YAAa,SAAQ,oBAAoB;IAClD,YAAY,OAAe;QACvB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC/B,CAAC;CACJ;AAED,MAAM,UAAU,uBAAuB,CACnC,KAA2B;IAE3B,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS;QACrC,MAAM,IAAI,oBAAoB,CAC1B,0CAA0C,CAC7C,CAAC;AACV,CAAC;AAED,MAAM,UAAU,mBAAmB,CAC/B,KAAc;IAEd,uBAAuB,CAAC,KAAK,CAAC,CAAC;IAE/B,IACI,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,OAAQ,KAAa,CAAC,IAAI,KAAK,QAAQ;QAEvC,MAAM,IAAI,oBAAoB,CAC1B,kDAAkD,CACrD,CAAC;AACV,CAAC"}
|
package/dist/option.d.ts
CHANGED
|
@@ -35,6 +35,8 @@ interface OptionMethods<T> {
|
|
|
35
35
|
isSome(): this is SomeOption<T>;
|
|
36
36
|
/**
|
|
37
37
|
* Returns `true` if the option is a `Some` and the value inside of it matches a predicate.
|
|
38
|
+
*
|
|
39
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
38
40
|
*/
|
|
39
41
|
isSomeAnd(f: (val: T) => boolean): boolean;
|
|
40
42
|
/**
|
|
@@ -43,50 +45,69 @@ interface OptionMethods<T> {
|
|
|
43
45
|
isNone(): this is NoneOption<T>;
|
|
44
46
|
/**
|
|
45
47
|
* Returns `true` if the option is a `None` or the value inside of it matches a predicate.
|
|
48
|
+
*
|
|
49
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
46
50
|
*/
|
|
47
51
|
isNoneOr(f: (val: T) => boolean): boolean;
|
|
48
52
|
/**
|
|
49
53
|
* Returns the contained `Some` value.
|
|
50
54
|
*
|
|
51
|
-
* @throws
|
|
55
|
+
* @throws Panics if the value is a `None` with a custom panic message provided by `msg`.
|
|
56
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
52
57
|
*/
|
|
53
58
|
expect(msg: string): T;
|
|
54
59
|
/**
|
|
55
60
|
* Returns the contained `Some` value.
|
|
56
61
|
*
|
|
57
|
-
* @throws
|
|
62
|
+
* @throws Panics if the self value equals `None`.
|
|
58
63
|
*/
|
|
59
64
|
unwrap(): T;
|
|
60
65
|
/**
|
|
61
66
|
* Returns the contained `Some` value or a provided default.
|
|
67
|
+
*
|
|
68
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
62
69
|
*/
|
|
63
70
|
unwrapOr(defaultVal: T): T;
|
|
64
71
|
/**
|
|
65
72
|
* Returns the contained `Some` value or computes it from a closure.
|
|
73
|
+
*
|
|
74
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
66
75
|
*/
|
|
67
76
|
unwrapOrElse(f: () => T): T;
|
|
68
77
|
/**
|
|
69
78
|
* Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
|
|
79
|
+
*
|
|
80
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
70
81
|
*/
|
|
71
82
|
map<U>(f: (val: T) => U): Option<U>;
|
|
72
83
|
/**
|
|
73
84
|
* Calls the provided closure with a reference to the contained value (if `Some`).
|
|
85
|
+
*
|
|
86
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
74
87
|
*/
|
|
75
88
|
inspect(f: (val: T) => void): Option<T>;
|
|
76
89
|
/**
|
|
77
90
|
* Returns the provided default result (if none), or applies a function to the contained value (if any).
|
|
91
|
+
*
|
|
92
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
78
93
|
*/
|
|
79
94
|
mapOr<U>(defaultVal: U, f: (val: T) => U): U;
|
|
80
95
|
/**
|
|
81
96
|
* Computes a default function result (if none), or applies a different function to the contained value (if any).
|
|
97
|
+
*
|
|
98
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
82
99
|
*/
|
|
83
100
|
mapOrElse<U>(defaultF: () => U, f: (val: T) => U): U;
|
|
84
101
|
/**
|
|
85
102
|
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
|
|
103
|
+
*
|
|
104
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
86
105
|
*/
|
|
87
106
|
okOr<E extends ResultError>(err: E): Result<T, E>;
|
|
88
107
|
/**
|
|
89
108
|
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
|
|
109
|
+
*
|
|
110
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
90
111
|
*/
|
|
91
112
|
okOrElse<E extends ResultError>(errF: () => E): Result<T, E>;
|
|
92
113
|
/**
|
|
@@ -95,40 +116,58 @@ interface OptionMethods<T> {
|
|
|
95
116
|
iter(): IterableIterator<T>;
|
|
96
117
|
/**
|
|
97
118
|
* Returns `None` if the option is `None`, otherwise returns `optb`.
|
|
119
|
+
*
|
|
120
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
98
121
|
*/
|
|
99
122
|
and<U>(optb: Option<U>): Option<U>;
|
|
100
123
|
/**
|
|
101
124
|
* Returns `None` if the option is `None`, otherwise calls `f` with the wrapped value and returns the result.
|
|
125
|
+
*
|
|
126
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
102
127
|
*/
|
|
103
128
|
andThen<U>(f: (val: T) => Option<U>): Option<U>;
|
|
104
129
|
/**
|
|
105
130
|
* Returns `None` if the option is `None`, otherwise calls `predicate` with the wrapped value and returns:
|
|
106
131
|
* - `Some(t)` if `predicate` returns `true` (where `t` is the wrapped value), and
|
|
107
132
|
* - `None` if `predicate` returns `false`.
|
|
133
|
+
*
|
|
134
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
108
135
|
*/
|
|
109
136
|
filter(predicate: (val: T) => boolean): Option<T>;
|
|
110
137
|
/**
|
|
111
138
|
* Returns the option if it contains a value, otherwise returns `optb`.
|
|
139
|
+
*
|
|
140
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
112
141
|
*/
|
|
113
142
|
or<T2>(optb: Option<T2>): Option<T | T2>;
|
|
114
143
|
/**
|
|
115
144
|
* Returns the option if it contains a value, otherwise calls `f` and returns the result.
|
|
145
|
+
*
|
|
146
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
116
147
|
*/
|
|
117
148
|
orElse<T2>(f: () => Option<T2>): Option<T | T2>;
|
|
118
149
|
/**
|
|
119
150
|
* Returns `Some` if exactly one of `this`, `optb` is `Some`, otherwise returns `None`.
|
|
151
|
+
*
|
|
152
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
120
153
|
*/
|
|
121
154
|
xor<T2>(optb: Option<T2>): Option<T | T2>;
|
|
122
155
|
/**
|
|
123
156
|
* Inserts `value` into the option, then returns a reference to it.
|
|
157
|
+
*
|
|
158
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
124
159
|
*/
|
|
125
160
|
insert(value: T): T;
|
|
126
161
|
/**
|
|
127
162
|
* Inserts `value` into the option if it is `None`, then returns a reference to the contained value.
|
|
163
|
+
*
|
|
164
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
128
165
|
*/
|
|
129
166
|
getOrInsert(value: T): T;
|
|
130
167
|
/**
|
|
131
168
|
* Inserts a value computed from `f` into the option if it is `None`, then returns a reference to the contained value.
|
|
169
|
+
*
|
|
170
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
132
171
|
*/
|
|
133
172
|
getOrInsertWith(f: () => T): T;
|
|
134
173
|
/**
|
|
@@ -137,15 +176,21 @@ interface OptionMethods<T> {
|
|
|
137
176
|
take(): Option<T>;
|
|
138
177
|
/**
|
|
139
178
|
* Takes the value out of the option, but only if the predicate evaluates to `true` on the value.
|
|
179
|
+
*
|
|
180
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
140
181
|
*/
|
|
141
182
|
takeIf(predicate: (val: T) => boolean): Option<T>;
|
|
142
183
|
/**
|
|
143
184
|
* Replaces the actual value in the option by the value given in parameter, returning the old value if present,
|
|
144
185
|
* leaving a `Some` in its place.
|
|
186
|
+
*
|
|
187
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
145
188
|
*/
|
|
146
189
|
replace(value: T): Option<T>;
|
|
147
190
|
/**
|
|
148
191
|
* Converts from `Option<Option<T>>` to `Option<T>`.
|
|
192
|
+
*
|
|
193
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
149
194
|
*/
|
|
150
195
|
flatten<U>(this: Option<Option<U>>): Option<U>;
|
|
151
196
|
}
|
package/dist/option.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"option.d.ts","sourceRoot":"","sources":["../src/option.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"option.d.ts","sourceRoot":"","sources":["../src/option.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,WAAW,EAAW,MAAM,UAAU,CAAC;AAElE;;GAEG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI;IACxB,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;CACrB,GAAG,aAAa,CAAC,CAAC,CA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,EAAE,MAAM,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;IAE1C;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IAEpB;;;;OAIG;IACH,WAAW,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IAEzB;;;;OAIG;IACH,eAAe,CAAC,CAAC,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;IAE/B;;OAEG;IACH,IAAI,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC;IAElB;;;;OAIG;IACH,MAAM,CAAC,SAAS,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAElD;;;;;OAKG;IACH,OAAO,CAAC,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAE7B;;;;OAIG;IACH,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;CAClD;AA0OD;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAG3C;AAED;;;GAGG;AACH,wBAAgB,IAAI,CAAC,CAAC,GAAG,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,CAE3C"}
|
package/dist/option.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { assertIsResultError, assertValueIsNotMissing, FlattenError, InvalidArgumentError, PanicError } from './errors';
|
|
1
2
|
import { Ok, Err } from './result';
|
|
2
3
|
class OptionImpl {
|
|
3
4
|
#value;
|
|
@@ -11,52 +12,75 @@ class OptionImpl {
|
|
|
11
12
|
return this.value !== null;
|
|
12
13
|
}
|
|
13
14
|
isSomeAnd(f) {
|
|
15
|
+
if (typeof f !== 'function')
|
|
16
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
14
17
|
return this.isSome() && f(this.value);
|
|
15
18
|
}
|
|
16
19
|
isNone() {
|
|
17
20
|
return this.value === null;
|
|
18
21
|
}
|
|
19
22
|
isNoneOr(f) {
|
|
23
|
+
if (typeof f !== 'function')
|
|
24
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
20
25
|
return this.value === null || f(this.value);
|
|
21
26
|
}
|
|
22
27
|
expect(msg) {
|
|
28
|
+
if (typeof msg !== 'string')
|
|
29
|
+
throw new InvalidArgumentError('Argument must be a string');
|
|
23
30
|
if (this.isNone())
|
|
24
|
-
throw new
|
|
31
|
+
throw new PanicError(msg);
|
|
25
32
|
return this.value;
|
|
26
33
|
}
|
|
27
34
|
unwrap() {
|
|
28
35
|
if (this.isNone())
|
|
29
|
-
throw new
|
|
36
|
+
throw new PanicError('called `Option.unwrap()` on a `None` value');
|
|
30
37
|
return this.value;
|
|
31
38
|
}
|
|
32
39
|
unwrapOr(defaultVal) {
|
|
40
|
+
assertValueIsNotMissing(defaultVal);
|
|
33
41
|
return this.isSome() ? this.value : defaultVal;
|
|
34
42
|
}
|
|
35
43
|
unwrapOrElse(f) {
|
|
44
|
+
if (typeof f !== 'function')
|
|
45
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
36
46
|
return this.isSome() ? this.value : f();
|
|
37
47
|
}
|
|
38
48
|
map(f) {
|
|
49
|
+
if (typeof f !== 'function')
|
|
50
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
39
51
|
if (this.isSome())
|
|
40
52
|
return Some(f(this.value));
|
|
41
53
|
return None();
|
|
42
54
|
}
|
|
43
55
|
inspect(f) {
|
|
56
|
+
if (typeof f !== 'function')
|
|
57
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
44
58
|
if (this.isSome())
|
|
45
59
|
f(this.value);
|
|
46
60
|
return this;
|
|
47
61
|
}
|
|
48
62
|
mapOr(defaultVal, f) {
|
|
63
|
+
assertValueIsNotMissing(defaultVal);
|
|
64
|
+
if (typeof f !== 'function')
|
|
65
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
49
66
|
return this.isSome() ? f(this.value) : defaultVal;
|
|
50
67
|
}
|
|
51
68
|
mapOrElse(defaultF, f) {
|
|
69
|
+
if (typeof defaultF !== 'function')
|
|
70
|
+
throw new InvalidArgumentError("Argument 'defaultF' must be a function");
|
|
71
|
+
if (typeof f !== 'function')
|
|
72
|
+
throw new InvalidArgumentError("Argument 'f' must be a function");
|
|
52
73
|
return this.isSome() ? f(this.value) : defaultF();
|
|
53
74
|
}
|
|
54
75
|
okOr(err) {
|
|
76
|
+
assertIsResultError(err);
|
|
55
77
|
if (this.isSome())
|
|
56
78
|
return Ok(this.value);
|
|
57
79
|
return Err(err);
|
|
58
80
|
}
|
|
59
81
|
okOrElse(errF) {
|
|
82
|
+
if (typeof errF !== 'function')
|
|
83
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
60
84
|
if (this.isSome())
|
|
61
85
|
return Ok(this.value);
|
|
62
86
|
return Err(errF());
|
|
@@ -66,31 +90,43 @@ class OptionImpl {
|
|
|
66
90
|
yield this.value;
|
|
67
91
|
}
|
|
68
92
|
and(optb) {
|
|
93
|
+
if (!(optb instanceof OptionImpl))
|
|
94
|
+
throw new InvalidArgumentError('Argument must be an Option');
|
|
69
95
|
if (this.isSome())
|
|
70
96
|
return optb;
|
|
71
97
|
return None();
|
|
72
98
|
}
|
|
73
99
|
andThen(f) {
|
|
100
|
+
if (typeof f !== 'function')
|
|
101
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
74
102
|
if (this.isSome())
|
|
75
103
|
return f(this.value);
|
|
76
104
|
return None();
|
|
77
105
|
}
|
|
78
106
|
filter(predicate) {
|
|
107
|
+
if (typeof predicate !== 'function')
|
|
108
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
79
109
|
if (this.isSome() && predicate(this.value))
|
|
80
110
|
return this;
|
|
81
111
|
return None();
|
|
82
112
|
}
|
|
83
113
|
or(optb) {
|
|
114
|
+
if (!(optb instanceof OptionImpl))
|
|
115
|
+
throw new InvalidArgumentError('Argument must be an Option');
|
|
84
116
|
if (this.isSome())
|
|
85
117
|
return this;
|
|
86
118
|
return optb;
|
|
87
119
|
}
|
|
88
120
|
orElse(f) {
|
|
121
|
+
if (typeof f !== 'function')
|
|
122
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
89
123
|
if (this.isSome())
|
|
90
124
|
return this;
|
|
91
125
|
return f();
|
|
92
126
|
}
|
|
93
127
|
xor(optb) {
|
|
128
|
+
if (!(optb instanceof OptionImpl))
|
|
129
|
+
throw new InvalidArgumentError('Argument must be an Option');
|
|
94
130
|
const thisIsSome = this.isSome();
|
|
95
131
|
const optbIsSome = optb.isSome();
|
|
96
132
|
if (thisIsSome && !optbIsSome)
|
|
@@ -100,15 +136,19 @@ class OptionImpl {
|
|
|
100
136
|
return None();
|
|
101
137
|
}
|
|
102
138
|
insert(value) {
|
|
139
|
+
assertValueIsNotMissing(value);
|
|
103
140
|
this.#value = value;
|
|
104
141
|
return this.#value;
|
|
105
142
|
}
|
|
106
143
|
getOrInsert(value) {
|
|
144
|
+
assertValueIsNotMissing(value);
|
|
107
145
|
if (this.isNone())
|
|
108
146
|
this.#value = value;
|
|
109
147
|
return this.#value;
|
|
110
148
|
}
|
|
111
149
|
getOrInsertWith(f) {
|
|
150
|
+
if (typeof f !== 'function')
|
|
151
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
112
152
|
if (this.isNone())
|
|
113
153
|
this.#value = f();
|
|
114
154
|
return this.#value;
|
|
@@ -122,6 +162,8 @@ class OptionImpl {
|
|
|
122
162
|
return None();
|
|
123
163
|
}
|
|
124
164
|
takeIf(predicate) {
|
|
165
|
+
if (typeof predicate !== 'function')
|
|
166
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
125
167
|
if (this.isSome() && predicate(this.value)) {
|
|
126
168
|
const val = this.value;
|
|
127
169
|
this.#value = null;
|
|
@@ -130,6 +172,7 @@ class OptionImpl {
|
|
|
130
172
|
return None();
|
|
131
173
|
}
|
|
132
174
|
replace(value) {
|
|
175
|
+
assertValueIsNotMissing(value);
|
|
133
176
|
const old = this.#value;
|
|
134
177
|
this.#value = value;
|
|
135
178
|
if (old !== null)
|
|
@@ -137,9 +180,11 @@ class OptionImpl {
|
|
|
137
180
|
return None();
|
|
138
181
|
}
|
|
139
182
|
flatten() {
|
|
140
|
-
if (this.
|
|
141
|
-
return
|
|
142
|
-
|
|
183
|
+
if (this.isNone())
|
|
184
|
+
return None();
|
|
185
|
+
if (!(this.value instanceof OptionImpl))
|
|
186
|
+
throw new FlattenError('flatten can only be called on Option<Option<T>>');
|
|
187
|
+
return this.value;
|
|
143
188
|
}
|
|
144
189
|
}
|
|
145
190
|
/**
|
|
@@ -148,6 +193,7 @@ class OptionImpl {
|
|
|
148
193
|
* @returns An `Option` representing the presence of a value.
|
|
149
194
|
*/
|
|
150
195
|
export function Some(value) {
|
|
196
|
+
assertValueIsNotMissing(value);
|
|
151
197
|
return new OptionImpl(value);
|
|
152
198
|
}
|
|
153
199
|
/**
|
package/dist/option.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"option.js","sourceRoot":"","sources":["../src/option.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiC,EAAE,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"option.js","sourceRoot":"","sources":["../src/option.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,mBAAmB,EACnB,uBAAuB,EACvB,YAAY,EACZ,oBAAoB,EACpB,UAAU,EACb,MAAM,UAAU,CAAC;AAClB,OAAO,EAAiC,EAAE,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAoOlE,MAAM,UAAU;IACZ,MAAM,CAAW;IAEjB,YAAY,KAAe;QACvB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACxB,CAAC;IAED,IAAI,KAAK;QACL,OAAO,IAA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G,CAAC,CAAC;QACnC,OAAO,IAAI,EAAE,CAAC;IAClB,CAAC;IAED,OAAO;QACH,IAAI,IAAI,CAAC,MAAM,EAAE;YAAE,OAAO,IAAI,EAAE,CAAC;QAEjC,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,YAAY,UAAU,CAAC;YACnC,MAAM,IAAI,YAAY,CAClB,iDAAiD,CACpD,CAAC;QAEN,OAAO,IAAI,CAAC,KAAkB,CAAC;IACnC,CAAC;CACJ;AAED;;;;GAIG;AACH,MAAM,UAAU,IAAI,CAAI,KAAQ;IAC5B,uBAAuB,CAAC,KAAK,CAAC,CAAC;IAC/B,OAAO,IAAI,UAAU,CAAI,KAAK,CAAc,CAAC;AACjD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,IAAI;IAChB,OAAO,IAAI,UAAU,CAAI,IAAI,CAAc,CAAC;AAChD,CAAC"}
|
package/dist/result.d.ts
CHANGED
|
@@ -53,6 +53,8 @@ interface ResultMethods<T, E extends ResultError> {
|
|
|
53
53
|
isOk(): this is OkResult<T, E>;
|
|
54
54
|
/**
|
|
55
55
|
* Returns `true` if the result is `Ok` and the value inside of it matches a predicate.
|
|
56
|
+
*
|
|
57
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
56
58
|
*/
|
|
57
59
|
isOkAnd(f: (val: T) => boolean): this is OkResult<T, E>;
|
|
58
60
|
/**
|
|
@@ -61,6 +63,8 @@ interface ResultMethods<T, E extends ResultError> {
|
|
|
61
63
|
isErr(): this is ErrResult<T, E>;
|
|
62
64
|
/**
|
|
63
65
|
* Returns `true` if the result is `Err` and the value inside of it matches a predicate.
|
|
66
|
+
*
|
|
67
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
64
68
|
*/
|
|
65
69
|
isErrAnd(f: (err: E) => boolean): this is ErrResult<T, E>;
|
|
66
70
|
/**
|
|
@@ -79,36 +83,48 @@ interface ResultMethods<T, E extends ResultError> {
|
|
|
79
83
|
* Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value, leaving an `Err` value untouched.
|
|
80
84
|
*
|
|
81
85
|
* This function can be used to compose the results of two functions.
|
|
86
|
+
*
|
|
87
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
82
88
|
*/
|
|
83
89
|
map<U>(f: (val: T) => U): Result<U, E>;
|
|
84
90
|
/**
|
|
85
91
|
* Returns the provided default (if `Err`), or applies a function to the contained value (if `Ok`).
|
|
86
92
|
*
|
|
87
93
|
* Arguments passed to `mapOr` are eagerly evaluated; if you are passing the result of a function call, it is recommended to use `mapOrElse`, which is lazily evaluated.
|
|
94
|
+
*
|
|
95
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
88
96
|
*/
|
|
89
97
|
mapOr<U>(fallback: U, f: (val: T) => U): U;
|
|
90
98
|
/**
|
|
91
99
|
* Maps a `Result<T, E>` to `U` by applying fallback function `fallbackFn` to a contained `Err` value, or function `f` to a contained `Ok` value.
|
|
92
100
|
*
|
|
93
101
|
* This function can be used to unpack a successful result while handling an error.
|
|
102
|
+
*
|
|
103
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
94
104
|
*/
|
|
95
105
|
mapOrElse<U>(fallbackFn: (err: E) => U, f: (val: T) => U): U;
|
|
96
106
|
/**
|
|
97
107
|
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value untouched.
|
|
98
108
|
*
|
|
99
109
|
* This function can be used to pass through a successful result while handling an error.
|
|
110
|
+
*
|
|
111
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
100
112
|
*/
|
|
101
113
|
mapErr<F extends ResultError>(f: (err: E) => F): Result<T, F>;
|
|
102
114
|
/**
|
|
103
115
|
* Calls a function with a reference to the contained value if `Ok`.
|
|
104
116
|
*
|
|
105
117
|
* Returns the original result.
|
|
118
|
+
*
|
|
119
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
106
120
|
*/
|
|
107
121
|
inspect(f: (val: T) => void): Result<T, E>;
|
|
108
122
|
/**
|
|
109
123
|
* Calls a function with a reference to the contained value if `Err`.
|
|
110
124
|
*
|
|
111
125
|
* Returns the original result.
|
|
126
|
+
*
|
|
127
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
112
128
|
*/
|
|
113
129
|
inspectErr(f: (err: E) => void): Result<T, E>;
|
|
114
130
|
/**
|
|
@@ -120,61 +136,80 @@ interface ResultMethods<T, E extends ResultError> {
|
|
|
120
136
|
/**
|
|
121
137
|
* Returns the contained `Ok` value, consuming the `self` value.
|
|
122
138
|
*
|
|
123
|
-
* @throws
|
|
139
|
+
* @throws Panics if the value is an `Err`, with a panic message including the passed message, and the content of the `Err`.
|
|
140
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
124
141
|
*/
|
|
125
142
|
expect(msg: string): T;
|
|
126
143
|
/**
|
|
127
144
|
* Returns the contained `Ok` value, consuming the `self` value.
|
|
128
145
|
*
|
|
129
|
-
* @throws
|
|
146
|
+
* @throws Panics if the value is an `Err`, with a panic message provided by the `Err`'s value.
|
|
130
147
|
*/
|
|
131
148
|
unwrap(): T;
|
|
132
149
|
/**
|
|
133
150
|
* Returns the contained `Err` value, consuming the `self` value.
|
|
134
151
|
*
|
|
135
|
-
* @throws
|
|
152
|
+
* @throws Panics if the value is an `Ok`, with a panic message including the passed message, and the content of the `Ok`.
|
|
153
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
136
154
|
*/
|
|
137
155
|
expectErr(msg: string): E;
|
|
138
156
|
/**
|
|
139
157
|
* Returns the contained `Err` value, consuming the `self` value.
|
|
140
158
|
*
|
|
141
|
-
* @throws
|
|
159
|
+
* @throws Panics if the value is an `Ok`, with a custom panic message provided by the `Ok`'s value.
|
|
142
160
|
*/
|
|
143
161
|
unwrapErr(): E;
|
|
144
162
|
/**
|
|
145
163
|
* Returns `res` if the result is `Ok`, otherwise returns the `Err` value of `self`.
|
|
146
164
|
*
|
|
147
165
|
* Arguments passed to `and` are eagerly evaluated; if you are passing the result of a function call, it is recommended to use `andThen`, which is lazily evaluated.
|
|
166
|
+
*
|
|
167
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
148
168
|
*/
|
|
149
169
|
and<U, E2 extends ResultError>(res: Result<U, E2>): Result<U, E | E2>;
|
|
150
170
|
/**
|
|
151
171
|
* Calls `f` if the result is `Ok`, otherwise returns the `Err` value of `self`.
|
|
152
172
|
*
|
|
153
173
|
* This function can be used for control flow based on `Result` values.
|
|
174
|
+
*
|
|
175
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
154
176
|
*/
|
|
155
177
|
andThen<U, F extends ResultError>(f: (val: T) => Result<U, F>): Result<U, E | F>;
|
|
156
178
|
/**
|
|
157
179
|
* Returns `res` if the result is `Err`, otherwise returns the `Ok` value of `self`.
|
|
158
180
|
*
|
|
159
181
|
* Arguments passed to `or` are eagerly evaluated; if you are passing the result of a function call, it is recommended to use `orElse`, which is lazily evaluated.
|
|
182
|
+
*
|
|
183
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
160
184
|
*/
|
|
161
185
|
or<T2, F extends ResultError>(res: Result<T2, F>): Result<T | T2, F>;
|
|
162
186
|
/**
|
|
163
187
|
* Calls `f` if the result is `Err`, otherwise returns the `Ok` value of `self`.
|
|
164
188
|
*
|
|
165
189
|
* This function can be used for control flow based on result values.
|
|
190
|
+
*
|
|
191
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
166
192
|
*/
|
|
167
193
|
orElse<T2, F extends ResultError>(f: (err: E) => Result<T2, F>): Result<T | T2, F>;
|
|
168
194
|
/**
|
|
169
195
|
* Returns the contained `Ok` value or a provided default.
|
|
170
196
|
*
|
|
171
197
|
* Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing the result of a function call, it is recommended to use `unwrapOrElse`, which is lazily evaluated.
|
|
198
|
+
*
|
|
199
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
172
200
|
*/
|
|
173
201
|
unwrapOr<T2>(fallback: T2): T | T2;
|
|
174
202
|
/**
|
|
175
203
|
* Returns the contained `Ok` value or computes it from a closure.
|
|
204
|
+
*
|
|
205
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
176
206
|
*/
|
|
177
207
|
unwrapOrElse<T2>(f: (err: E) => T2): T | T2;
|
|
208
|
+
/**
|
|
209
|
+
* Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
|
|
210
|
+
*
|
|
211
|
+
* @throws If this method throws an error other than a panic, it indicates misuse of the library (garbage data, bypass of the type system, or invalid runtime input). Check your code.
|
|
212
|
+
*/
|
|
178
213
|
flatten<U, F extends ResultError>(this: Result<Result<U, F>, E>): Result<U, E | F>;
|
|
179
214
|
}
|
|
180
215
|
/**
|
package/dist/result.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"result.d.ts","sourceRoot":"","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AAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,GAC9B,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;CACvB;AAwND;;;;;;;;;;GAUG;AACH,wBAAgB,EAAE,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,GAAG,KAAK,EAAE,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAG3E;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,GAAG,CACf,KAAK,CAAC,CAAC,SAAS,MAAM,EACtB,CAAC,SAAS,WAAW,GAAG;IAAE,IAAI,EAAE,CAAC,CAAA;CAAE,EACrC,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAG5B"}
|
package/dist/result.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { assertIsResultError, assertValueIsNotMissing, FlattenError, InvalidArgumentError, PanicError } from './errors';
|
|
1
2
|
import { Some, None } from './option';
|
|
2
3
|
class ResultImpl {
|
|
3
4
|
// will error at runtime if trying to access # fields
|
|
@@ -19,12 +20,16 @@ class ResultImpl {
|
|
|
19
20
|
return this.error === null;
|
|
20
21
|
}
|
|
21
22
|
isOkAnd(f) {
|
|
23
|
+
if (typeof f !== 'function')
|
|
24
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
22
25
|
return this.isOk() && f(this.value);
|
|
23
26
|
}
|
|
24
27
|
isErr() {
|
|
25
28
|
return this.value === null;
|
|
26
29
|
}
|
|
27
30
|
isErrAnd(f) {
|
|
31
|
+
if (typeof f !== 'function')
|
|
32
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
28
33
|
return this.isErr() && f(this.error);
|
|
29
34
|
}
|
|
30
35
|
ok() {
|
|
@@ -38,31 +43,46 @@ class ResultImpl {
|
|
|
38
43
|
return None();
|
|
39
44
|
}
|
|
40
45
|
map(f) {
|
|
46
|
+
if (typeof f !== 'function')
|
|
47
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
41
48
|
if (this.isErr())
|
|
42
49
|
return new ResultImpl(null, this.error);
|
|
43
50
|
return new ResultImpl(f(this.value), null);
|
|
44
51
|
}
|
|
45
52
|
mapOr(fallback, f) {
|
|
53
|
+
assertValueIsNotMissing(fallback);
|
|
54
|
+
if (typeof f !== 'function')
|
|
55
|
+
throw new InvalidArgumentError("Argument 'f' must be a function");
|
|
46
56
|
if (this.isErr())
|
|
47
57
|
return fallback;
|
|
48
58
|
return f(this.value);
|
|
49
59
|
}
|
|
50
60
|
mapOrElse(fallbackFn, f) {
|
|
61
|
+
if (typeof fallbackFn !== 'function')
|
|
62
|
+
throw new InvalidArgumentError("Argument 'fallbackFn' must be a function");
|
|
63
|
+
if (typeof f !== 'function')
|
|
64
|
+
throw new InvalidArgumentError("Argument 'f' must be a function");
|
|
51
65
|
if (this.isErr())
|
|
52
66
|
return fallbackFn(this.error);
|
|
53
67
|
return f(this.value);
|
|
54
68
|
}
|
|
55
69
|
mapErr(f) {
|
|
70
|
+
if (typeof f !== 'function')
|
|
71
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
56
72
|
if (this.isErr())
|
|
57
73
|
return new ResultImpl(null, f(this.error));
|
|
58
74
|
return new ResultImpl(this.value, null);
|
|
59
75
|
}
|
|
60
76
|
inspect(f) {
|
|
77
|
+
if (typeof f !== 'function')
|
|
78
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
61
79
|
if (this.isOk())
|
|
62
80
|
f(this.value);
|
|
63
81
|
return this;
|
|
64
82
|
}
|
|
65
83
|
inspectErr(f) {
|
|
84
|
+
if (typeof f !== 'function')
|
|
85
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
66
86
|
if (this.isErr())
|
|
67
87
|
f(this.error);
|
|
68
88
|
return this;
|
|
@@ -72,62 +92,76 @@ class ResultImpl {
|
|
|
72
92
|
yield this.value;
|
|
73
93
|
}
|
|
74
94
|
expect(msg) {
|
|
95
|
+
if (typeof msg !== 'string')
|
|
96
|
+
throw new InvalidArgumentError('Argument must be a string');
|
|
75
97
|
if (this.isErr())
|
|
76
|
-
throw new
|
|
98
|
+
throw new PanicError(`${msg}: code "${this.error.code}"`);
|
|
77
99
|
return this.value;
|
|
78
100
|
}
|
|
79
101
|
unwrap() {
|
|
80
|
-
if (this.
|
|
81
|
-
throw this.error;
|
|
102
|
+
if (this.isErr())
|
|
103
|
+
throw new PanicError(`called \`Result.unwrap()\` on an \`Err\` value: code "${this.error.code}"`);
|
|
82
104
|
return this.value;
|
|
83
105
|
}
|
|
84
106
|
expectErr(msg) {
|
|
107
|
+
if (typeof msg !== 'string')
|
|
108
|
+
throw new InvalidArgumentError('Argument must be a string');
|
|
85
109
|
if (this.isOk())
|
|
86
|
-
throw new
|
|
110
|
+
throw new PanicError(`${msg}: "${String(this.value)}"`);
|
|
87
111
|
return this.error;
|
|
88
112
|
}
|
|
89
113
|
unwrapErr() {
|
|
90
114
|
if (this.isOk())
|
|
91
|
-
throw new
|
|
115
|
+
throw new PanicError(`called \`Result.unwrapErr()\` on an \`Ok\` value: "${String(this.value)}"`);
|
|
92
116
|
return this.error;
|
|
93
117
|
}
|
|
94
118
|
and(res) {
|
|
119
|
+
if (!(res instanceof ResultImpl))
|
|
120
|
+
throw new InvalidArgumentError('Argument must be a Result');
|
|
95
121
|
if (this.isOk())
|
|
96
122
|
return res;
|
|
97
123
|
return new ResultImpl(null, this.error);
|
|
98
124
|
}
|
|
99
125
|
andThen(f) {
|
|
126
|
+
if (typeof f !== 'function')
|
|
127
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
100
128
|
if (this.isOk())
|
|
101
129
|
return f(this.value);
|
|
102
130
|
return new ResultImpl(null, this.error);
|
|
103
131
|
}
|
|
104
132
|
or(res) {
|
|
133
|
+
if (!(res instanceof ResultImpl))
|
|
134
|
+
throw new InvalidArgumentError('Argument must be a Result');
|
|
105
135
|
if (this.isErr())
|
|
106
136
|
return res;
|
|
107
137
|
return new ResultImpl(this.value, null);
|
|
108
138
|
}
|
|
109
139
|
orElse(f) {
|
|
140
|
+
if (typeof f !== 'function')
|
|
141
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
110
142
|
if (this.isErr())
|
|
111
143
|
return f(this.error);
|
|
112
144
|
return new ResultImpl(this.value, null);
|
|
113
145
|
}
|
|
114
146
|
unwrapOr(fallback) {
|
|
147
|
+
assertValueIsNotMissing(fallback);
|
|
115
148
|
if (this.isErr())
|
|
116
149
|
return fallback;
|
|
117
150
|
return this.value;
|
|
118
151
|
}
|
|
119
152
|
unwrapOrElse(f) {
|
|
153
|
+
if (typeof f !== 'function')
|
|
154
|
+
throw new InvalidArgumentError('Argument must be a function');
|
|
120
155
|
if (this.isErr())
|
|
121
156
|
return f(this.error);
|
|
122
157
|
return this.value;
|
|
123
158
|
}
|
|
124
|
-
/**
|
|
125
|
-
* Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
|
|
126
|
-
*/
|
|
127
159
|
flatten() {
|
|
128
|
-
if (this.
|
|
129
|
-
return this.
|
|
130
|
-
|
|
160
|
+
if (this.isErr())
|
|
161
|
+
return new ResultImpl(null, this.error);
|
|
162
|
+
if (!(this.value instanceof ResultImpl))
|
|
163
|
+
throw new FlattenError('flatten can only be called on Result<Result<T, E>, E>');
|
|
164
|
+
return this.value;
|
|
131
165
|
}
|
|
132
166
|
}
|
|
133
167
|
/**
|
|
@@ -142,6 +176,7 @@ class ResultImpl {
|
|
|
142
176
|
* @returns A `Result` representing a successful outcome.
|
|
143
177
|
*/
|
|
144
178
|
export function Ok(value) {
|
|
179
|
+
assertValueIsNotMissing(value);
|
|
145
180
|
return new ResultImpl(value, null);
|
|
146
181
|
}
|
|
147
182
|
/**
|
|
@@ -156,6 +191,7 @@ export function Ok(value) {
|
|
|
156
191
|
* @returns A `Result` representing a failed outcome.
|
|
157
192
|
*/
|
|
158
193
|
export function Err(error) {
|
|
194
|
+
assertIsResultError(error);
|
|
159
195
|
return new ResultImpl(null, error);
|
|
160
196
|
}
|
|
161
197
|
//# sourceMappingURL=result.js.map
|
package/dist/result.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result.js","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,IAAI,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"result.js","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,mBAAmB,EACnB,uBAAuB,EACvB,YAAY,EACZ,oBAAoB,EACpB,UAAU,EACb,MAAM,UAAU,CAAC;AAClB,OAAO,EAAe,IAAI,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAmOnD,MAAM,UAAU;IACZ,qDAAqD;IACrD,MAAM,CAAW;IACjB,MAAM,CAAW;IAEjB,YAAY,KAAe,EAAE,KAAe;QACxC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACxB,CAAC;IAED,mCAAmC;IACnC,IAAI,KAAK;QACL,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IAED,mCAAmC;IACnC,IAAI,KAAK;QACL,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IAED,IAAI;QACA,OAAO,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC;IAC/B,CAAC;IAED,OAAO,CAAC,CAAsB;QAC1B,IAAI,OAAO,CAAC,KAAK,UAAU;YACvB,MAAM,IAAI,oBAAoB,CAAC,6BAA6B,CAAC,CAAC;QAClE,OAAO,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACxC,CAAC;IAED,KAAK;QACD,OAAO,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC;IAC/B,CAAC;IAED,QAAQ,CAAC,CAAsB;QAC3B,IAAI,OAAO,CAAC,KAAK,UAAU;YACvB,MAAM,IA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