-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult.ts
More file actions
234 lines (219 loc) · 6.03 KB
/
result.ts
File metadata and controls
234 lines (219 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { None, Optional, toOptional } from "./optional.js";
import {
First,
OptionalPair,
Second,
toOptionalPair,
} from "./optional-pair.js";
import { nonNullable, Func } from "./util.types.js";
import { Variant, variant, VariantTypeClass } from "./variant.js";
type ResultVariants<T extends nonNullable, E extends nonNullable> =
| Variant<"Ok", [T]>
| Variant<"Err", [E]>;
type ResultOk<T> = T extends Result<infer K, nonNullable> ? K : never;
type ResultErr<T> = T extends Result<nonNullable, infer K> ? K : never;
const combineErrsAsArray = <A extends nonNullable, B extends nonNullable>(
errs: OptionalPair<A, B>
) =>
errs.match({
First(a) {
return [a];
},
Second(a) {
return [a];
},
Both(a, b) {
return [a, b];
},
Neither() {
return [];
},
});
class Result<
T extends nonNullable = nonNullable,
E extends nonNullable = nonNullable
> extends VariantTypeClass<ResultVariants<T, E>> {
/**
* Maps a `Result<T, E>` to a `Result<M, ME>`
*
* @param mapOk - A function that maps `T` to `M`
* @param mapErr - A function that maps `E` to `ME`
* @returns An `Result<M, E>`
*/
map<M extends nonNullable>(mapOk: Func<[value: T], M>): Result<M, E>;
map<M extends nonNullable, ME extends nonNullable>(
mapOk: Func<[value: T], M>,
mapErr: Func<[value: E], ME>
): Result<M, ME>;
map(mapOk: Func<[value: T], any>, mapErr?: Func<[value: E], any>) {
return this.match({
Ok(value) {
const result = mapOk(value);
if (result instanceof Result) {
return result;
}
return Ok(result);
},
Err(error) {
return Err(mapErr ? mapErr(error) : error);
},
});
}
/**
* If `value` is the Ok variant it returns the value stored in it,
* otherwise it returns the result of executing the `fallback` function.
*
* @param fallback A function to call if `this` is the Err variant.
* @returns The value stored in the Ok variant or the result of calling `fallback`.
*/
fallback(fallback: Func<[error: E], T>): T {
return this.match({
Ok(value) {
return value;
},
Err: fallback,
});
}
/**
* Combines this `Result<T, E>` with `Result<B, E>` to make `Result<C, CE>`.
*
* @param b - A `Result<B, BE>`
* @param combineOk - A function that combines `T` and `B` into `C`
* @param combineErr - A function that combines `E` and `BE` into `CE`
* @returns `Result<C, CE>`
*/
combine<B extends nonNullable, BE extends nonNullable, C extends nonNullable>(
b: Result<B, BE>,
combineOk: Func<[a: T, b: B], C>
): Result<C, (E | BE)[]>;
combine<
B extends nonNullable,
BE extends nonNullable,
C extends nonNullable,
CE extends nonNullable
>(
b: Result<B, BE>,
combineOk: Func<[a: T, b: B], C>,
combineErr: Func<[errors: OptionalPair<E, BE>], CE>
): Result<C, CE>;
combine(
b: Result,
combineOk: Func<[a: T, b: any], any>,
combineErr: Func<[errors: OptionalPair<any, any>], any> = combineErrsAsArray
) {
return this.match({
Ok(a) {
return b.match({
Ok(b) {
return Ok(combineOk(a, b));
},
Err(be) {
return Err(combineErr(Second(toOptional(be))));
},
});
},
Err(ae) {
return b.match({
Ok() {
return Err(combineErr(First(toOptional(ae))));
},
Err(be) {
return Err(combineErr(toOptionalPair(ae, be)));
},
});
},
});
}
/**
* Converts Ok variants to Err variants if the `filter` predicate results in false.
*
* If this variant is Err or the `filter` predicate results in false a new Err variant
* will be constructed with the result of calling `error`. Otherwise the Ok variant is
* returned.
*
* @param filter - A predicate to determine whether or not to return the Ok variant or
* Err variant.
* @param error - Used to create the Err variant.
* @returns A new Result
*/
filter<E extends nonNullable>(
filter: Func<[value: T], boolean>,
error: Func<[], E>
): Result<T, E> {
return this.match({
Ok(value) {
return filter(value) ? Ok<T, E>(value) : Err<T, E>(error());
},
Err() {
return Err<T, E>(error());
},
});
}
/**
* Converts this `Result<T, E>` into `Optional<T>`
*
* @returns `Optional<T>`
*/
toOptional(): Optional<T> {
return this.match({
Ok(value) {
return toOptional(value);
},
Err() {
return None;
},
});
}
}
/**
* Ok variant representing the result was ok.
*
* @param value - A value to store in the Ok variant.
* @returns An Ok variant
*/
function Ok<T extends nonNullable, E extends nonNullable>(value: T) {
if (value == null) {
throw new TypeError(
"Ok variant of Result cannot be constructed with null or undefined"
);
}
return new Result<T, E>(variant("Ok", value));
}
/**
* Err variant representing the result was an error.
*
* @param error - A error to store in the Err variant.
* @returns An Err variant
*/
function Err<T extends nonNullable, E extends nonNullable>(error: E) {
if (error == null) {
throw new TypeError(
"Err variant of Result cannot be constructed with null or undefined"
);
}
return new Result<T, E>(variant("Err", error));
}
/**
* Converts a `err`, `value` pair into a Result variant. If `err` is not nullish it
* returns the `err` wrapped in the Err variant. Otherwise it returns `value`
* wrapped in the Ok variant.
*
* @param err - A value representing the err state.
* @param value - A value representing the ok state.
* @returns A Result.
*/
function toResult<T, E>(
err: E,
value: T
): Result<NonNullable<T>, NonNullable<E>>;
function toResult<T, E>(
err: E,
value: T
): Result<NonNullable<T>, NonNullable<E>>;
function toResult(err: any, value: any) {
if (err != null) {
return Err(err);
}
return Ok(value);
}
export { type Result, type ResultOk, type ResultErr, toResult, Ok, Err };