-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasync-option.ts
More file actions
340 lines (278 loc) · 11.4 KB
/
Copy pathasync-option.ts
File metadata and controls
340 lines (278 loc) · 11.4 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { type Option } from './option';
import { type AsyncResult, AsyncResultImpl } from './async-result';
/**
* An async wrapper around `Option<T>` that is `PromiseLike` (so it's awaitable)
* but also carries all chainable `Option` methods.
*
* **Intentionally omitted mutation methods:** `insert`, `getOrInsert`, `getOrInsertWith`,
* `getOrInsertWithAsync`, `take`, `takeIf`, and `replace` are not available on `AsyncOption`.
* These methods mutate the `Option` in-place, which is not meaningful on a pending async value -
* the underlying `Option` doesn't exist yet. Use `await` to resolve first, then mutate.
*
* **Error behavior in async context:** Methods that throw synchronously on `Option`
* (e.g. `unwrap` on `None`, `flatten` on non-nested) will produce a rejected `Promise`.
*/
export interface AsyncOption<T> extends PromiseLike<Option<T>> {
/**
* Returns a `Promise` that resolves to `true` if the option is a `Some` value.
*/
isSome(): Promise<boolean>;
/**
* Returns a `Promise` that resolves to `true` if the option is a `Some` and the value inside matches a predicate.
*/
isSomeAnd(f: (val: T) => boolean): Promise<boolean>;
/**
* Returns a `Promise` that resolves to `true` if the option is a `None` value.
*/
isNone(): Promise<boolean>;
/**
* Returns a `Promise` that resolves to `true` if the option is a `None` or the value inside matches a predicate.
*/
isNoneOr(f: (val: T) => boolean): Promise<boolean>;
/**
* Returns the contained `Some` value.
*
* @throws Rejects with `PanicError` if the value is a `None` with a custom panic message provided by `msg`.
*/
expect(msg: string): Promise<T>;
/**
* Returns the contained `Some` value.
*
* @throws Rejects with `PanicError` if the self value equals `None`.
*/
unwrap(): Promise<T>;
/**
* Returns the contained `Some` value or a provided default.
*/
unwrapOr(defaultVal: T): Promise<T>;
/**
* Returns the contained `Some` value or computes it from a closure.
*/
unwrapOrElse(f: () => T): Promise<T>;
/**
* Async version of `unwrapOrElse`. Returns the contained `Some` value or computes it from an async closure.
*/
unwrapOrElseAsync(f: () => PromiseLike<T>): Promise<T>;
/**
* Maps an `AsyncOption<T>` to `AsyncOption<U>` by applying a function to a contained value.
*/
map<U>(f: (val: T) => U): AsyncOption<U>;
/**
* Async version of `map`. Maps an `AsyncOption<T>` to `AsyncOption<U>` by applying an async function to a contained value.
*/
mapAsync<U>(f: (val: T) => PromiseLike<U>): AsyncOption<U>;
/**
* Calls the provided closure with a reference to the contained value (if `Some`).
*
* Returns the original option.
*/
inspect(f: (val: T) => void): AsyncOption<T>;
/**
* Async version of `inspect`. Calls the provided async closure with a reference to the contained value (if `Some`), then returns the original option.
*/
inspectAsync(f: (val: T) => PromiseLike<void>): AsyncOption<T>;
/**
* Returns the provided default result (if none), or applies a function to the contained value (if any).
*/
mapOr<U>(defaultVal: U, f: (val: T) => U): Promise<U>;
/**
* Computes a default function result (if none), or applies a different function to the contained value (if any).
*/
mapOrElse<U>(defaultF: () => U, f: (val: T) => U): Promise<U>;
/**
* Async version of `mapOrElse`. Computes a default async function result (if none), or applies a different async function to the contained value (if any).
*/
mapOrElseAsync<U>(
defaultF: () => PromiseLike<U>,
f: (val: T) => PromiseLike<U>
): Promise<U>;
/**
* Transforms the `AsyncOption<T>` into an `AsyncResult<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
*/
okOr<E>(err: E): AsyncResult<T, E>;
/**
* Transforms the `AsyncOption<T>` into an `AsyncResult<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
*/
okOrElse<E>(errF: () => E): AsyncResult<T, E>;
/**
* Async version of `okOrElse`. Transforms the `AsyncOption<T>` into an `AsyncResult<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(await errF())`.
*/
okOrElseAsync<E>(errF: () => PromiseLike<E>): AsyncResult<T, E>;
/**
* Returns `None` if the option is `None`, otherwise returns `optb`.
*/
and<U>(optb: Option<U>): AsyncOption<U>;
/**
* Returns `None` if the option is `None`, otherwise calls `f` with the wrapped value and returns the result.
*/
andThen<U>(f: (val: T) => Option<U>): AsyncOption<U>;
/**
* Async version of `andThen`. Returns `None` if the option is `None`, otherwise calls async `f` with the wrapped value and returns the result.
*/
andThenAsync<U>(f: (val: T) => PromiseLike<Option<U>>): AsyncOption<U>;
/**
* Returns `None` if the option is `None`, otherwise calls `predicate` with the wrapped value and returns:
* - `Some(t)` if `predicate` returns `true` (where `t` is the wrapped value), and
* - `None` if `predicate` returns `false`.
*/
filter(predicate: (val: T) => boolean): AsyncOption<T>;
/**
* Async version of `filter`. Returns `None` if the option is `None`, otherwise calls async `predicate` with the wrapped value and returns:
* - `Some(t)` if `predicate` resolves to `true` (where `t` is the wrapped value), and
* - `None` if `predicate` resolves to `false`.
*/
filterAsync(predicate: (val: T) => PromiseLike<boolean>): AsyncOption<T>;
/**
* Returns the option if it contains a value, otherwise returns `optb`.
*/
or<T2>(optb: Option<T2>): AsyncOption<T | T2>;
/**
* Returns the option if it contains a value, otherwise calls `f` and returns the result.
*/
orElse<T2>(f: () => Option<T2>): AsyncOption<T | T2>;
/**
* Async version of `orElse`. Returns the option if it contains a value, otherwise calls async `f` and returns the result.
*/
orElseAsync<T2>(f: () => PromiseLike<Option<T2>>): AsyncOption<T | T2>;
/**
* Returns `Some` if exactly one of `this`, `optb` is `Some`, otherwise returns `None`.
*/
xor<T2>(optb: Option<T2>): AsyncOption<T | T2>;
/**
* Converts from `AsyncOption<Option<T>>` to `AsyncOption<T>`.
*
* **Async note:** If the inner value is not an `Option`, this produces a rejected `Promise`
* with `FlattenError` rather than a synchronous throw.
*/
flatten<U>(this: AsyncOption<Option<U>>): AsyncOption<U>;
/**
* Matches the `Option` with two functions, one for each variant.
*/
match<U>(handlers: { Some: (val: T) => U; None: () => U }): Promise<U>;
}
export class AsyncOptionImpl<T> implements AsyncOption<T> {
constructor(private promise: PromiseLike<Option<T>>) {}
then<TResult1 = Option<T>, TResult2 = never>(
onfulfilled?:
| ((value: Option<T>) => TResult1 | PromiseLike<TResult1>)
| undefined
| null,
onrejected?:
| ((reason: any) => TResult2 | PromiseLike<TResult2>)
| undefined
| null
): Promise<TResult1 | TResult2> {
return Promise.resolve(this.promise).then(onfulfilled, onrejected);
}
async isSome(): Promise<boolean> {
const opt = await this;
return opt.isSome();
}
async isSomeAnd(f: (val: T) => boolean): Promise<boolean> {
const opt = await this;
return opt.isSomeAnd(f);
}
async isNone(): Promise<boolean> {
const opt = await this;
return opt.isNone();
}
async isNoneOr(f: (val: T) => boolean): Promise<boolean> {
const opt = await this;
return opt.isNoneOr(f);
}
async expect(msg: string): Promise<T> {
const opt = await this;
return opt.expect(msg);
}
async unwrap(): Promise<T> {
const opt = await this;
return opt.unwrap();
}
async unwrapOr(defaultVal: T): Promise<T> {
const opt = await this;
return opt.unwrapOr(defaultVal);
}
async unwrapOrElse(f: () => T): Promise<T> {
const opt = await this;
return opt.unwrapOrElse(f);
}
async unwrapOrElseAsync(f: () => PromiseLike<T>): Promise<T> {
const opt = await this;
return await opt.unwrapOrElseAsync(f);
}
map<U>(f: (val: T) => U): AsyncOption<U> {
return new AsyncOptionImpl(this.then((opt) => opt.map(f)));
}
mapAsync<U>(f: (val: T) => PromiseLike<U>): AsyncOption<U> {
return new AsyncOptionImpl(this.then((opt) => opt.mapAsync(f)));
}
inspect(f: (val: T) => void): AsyncOption<T> {
return new AsyncOptionImpl(this.then((opt) => opt.inspect(f)));
}
inspectAsync(f: (val: T) => PromiseLike<void>): AsyncOption<T> {
return new AsyncOptionImpl(this.then((opt) => opt.inspectAsync(f)));
}
async mapOr<U>(defaultVal: U, f: (val: T) => U): Promise<U> {
const opt = await this;
return opt.mapOr(defaultVal, f);
}
async mapOrElse<U>(defaultF: () => U, f: (val: T) => U): Promise<U> {
const opt = await this;
return opt.mapOrElse(defaultF, f);
}
async mapOrElseAsync<U>(
defaultF: () => PromiseLike<U>,
f: (val: T) => PromiseLike<U>
): Promise<U> {
const opt = await this;
return await opt.mapOrElseAsync(defaultF, f);
}
okOr<E>(err: E): AsyncResult<T, E> {
return new AsyncResultImpl(this.then((opt) => opt.okOr(err)));
}
okOrElse<E>(errF: () => E): AsyncResult<T, E> {
return new AsyncResultImpl(this.then((opt) => opt.okOrElse(errF)));
}
okOrElseAsync<E>(errF: () => PromiseLike<E>): AsyncResult<T, E> {
return new AsyncResultImpl(this.then((opt) => opt.okOrElseAsync(errF)));
}
and<U>(optb: Option<U>): AsyncOption<U> {
return new AsyncOptionImpl(this.then((opt) => opt.and(optb)));
}
andThen<U>(f: (val: T) => Option<U>): AsyncOption<U> {
return new AsyncOptionImpl(this.then((opt) => opt.andThen(f)));
}
andThenAsync<U>(f: (val: T) => PromiseLike<Option<U>>): AsyncOption<U> {
return new AsyncOptionImpl(this.then((opt) => opt.andThenAsync(f)));
}
filter(predicate: (val: T) => boolean): AsyncOption<T> {
return new AsyncOptionImpl(this.then((opt) => opt.filter(predicate)));
}
filterAsync(predicate: (val: T) => PromiseLike<boolean>): AsyncOption<T> {
return new AsyncOptionImpl(
this.then((opt) => opt.filterAsync(predicate))
);
}
or<T2>(optb: Option<T2>): AsyncOption<T | T2> {
return new AsyncOptionImpl(this.then((opt) => opt.or(optb)));
}
orElse<T2>(f: () => Option<T2>): AsyncOption<T | T2> {
return new AsyncOptionImpl(this.then((opt) => opt.orElse(f)));
}
orElseAsync<T2>(f: () => PromiseLike<Option<T2>>): AsyncOption<T | T2> {
return new AsyncOptionImpl(this.then((opt) => opt.orElseAsync(f)));
}
xor<T2>(optb: Option<T2>): AsyncOption<T | T2> {
return new AsyncOptionImpl(this.then((opt) => opt.xor(optb)));
}
flatten<U>(this: AsyncOptionImpl<Option<U>>): AsyncOption<U> {
return new AsyncOptionImpl(this.then((opt) => opt.flatten()));
}
async match<U>(handlers: {
Some: (val: T) => U;
None: () => U;
}): Promise<U> {
const opt = await this;
return opt.match(handlers);
}
}