-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoption.ts
More file actions
412 lines (340 loc) · 10.4 KB
/
option.ts
File metadata and controls
412 lines (340 loc) · 10.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import { ToString } from './utils';
import { Err, Ok, Result } from './result';
interface BaseOption<S> {
/**
* Returns true if the option is a `Some` value.
*
* @returns boolean
*/
isSome(): this is Some<S>;
/**
* Returns true if the option is a `Some` and the value inside of it matches
* a predicate.
*
* @param fn Predicate to match
* @returns boolean
*/
isSomeAnd(fn: (value: Readonly<S>) => boolean): this is Some<S>;
/**
* Returns true if the option is a `None` value.
*
* @returns boolean
*/
isNone(): this is None;
match<T>(matcher: { some: (value: Readonly<S>) => T; none: () => T }): T;
/**
* Maps an `Option<S>` to `Option<T>` by applying a function to a contained
* value (if `Some`) or returns `None` (if `None`).
*
* @param fn Mapping function.
* @returns The mapped option.
*/
map<T>(fn: (value: Readonly<S>) => T): Option<T>;
/**
* Returns the provided default result (if none), or applies a function to
* the contained value (if any).
*
* @param defaultValue The default value if `None`.
* @param fn The mapping function.
*/
mapOr<T>(defaultValue: T, fn: (value: Readonly<S>) => T): T;
/**
* Computes a default function result (if none), or applies a different
* function to the contained value (if any).
*
* @param defaultFn Closure computing the default value if `None`.
* @param fn The mapping function.
*/
mapOrElse<T>(defaultFn: () => T, fn: (value: Readonly<S>) => T): T;
/**
* Transforms the `Option<S>` into a `Result<S, E>`, mapping `Some(v)` to
* `Ok(v)` and `None` to `Err(err)`.
*
* @param error The error value if `None`.
* @returns The transformed result.
*/
okOr<E extends ToString>(error: E): Result<S, E>;
/**
* Transforms the `Option<S>` into a `Result<S, E>`, mapping `Some(v)` to
* `Ok(v)` and `None` to `Err(err())`.
*
* @param err Closure to compute the error value if `None`.
* @returns The transformed result.
*/
okOrElse<E extends ToString>(err: () => E): Result<S, E>;
/**
* Returns `None` if the option is `None`, otherwise returns `other`.
*
* @param other The other option if `Some`.
* @returns Either `None` or the `other` option.
*/
and<T>(other: Option<T>): Option<T>;
/**
* Returns `None` if the option is `None`, otherwise calls `fn` with the
* wrapped value and returns the result.
*
* @param fn The function which produces the other option if `Some`.
* @returns Either `None` or the computed other option.
*/
andThen<T>(fn: (value: Readonly<S>) => Option<T>): Option<T>;
/**
* 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.
*
* @param predicate The filter function.
* @returns `Some` if the filter function returned true, otherwise `None`.
*/
filter(predicate: (value: Readonly<S>) => boolean): Option<S>;
/**
* Returns the option if it contains a value, otherwise returns `other`.
*
* @param other The `other` option if `None`.
* @returns Either this or `other`.
*/
or<T>(other: Option<T>): Option<S | T>;
/**
* Returns the option if it contains a value, otherwise calls `fn` and returns
* the result.
*
* @param fn Function to compute the other option.
* @returns Either this or the other computed option.
*/
orElse<T>(fn: () => Option<T>): Option<S | T>;
/**
* Returns `Some` if exactly one of self, `other` is `Some`, otherwise returns
* `None`.
*
* @param other The other option.
*/
xor<T>(other: Option<T>): Option<S | T>;
/**
* Returns true if the option is a `Some` value containing the given `value`.
*
* @param value The value to match `Some(value)`.
* @returns boolean
*/
contains(value: S): boolean;
/**
* Zips `self` with another `Option`.
*
* If `self` is `Some(s)` and other is `Some(o)`, this method returns
* `Some([s, o])`. Otherwise, `None` is returned.
*
* @param other The other option to zip with.
* @returns The zipped option or `None`.
*/
zip<T>(other: Option<T>): Option<[S, T]>;
/**
* Zips `self` and another `Option` with function `fn`.
*
* If `self` is `Some(s)` and other is `Some(o)`, this method returns
* `Some(fn(s, o))`. Otherwise, `None` is returned.
*
* @param other The other option to zip with.
* @param fn Function which get's applied to the zipped values.
* @returns The zipped and transformed option or `None`.
*/
zipWith<T, K>(other: Option<T>, fn: (self: S, other: T) => K): Option<K>;
/**
* Returns the contained `Some` value.
*
* Because this function may throw, its use is generally discouraged. Instead,
* prefer to use pattern matching and handle the `None` case explicitly, or
* call `unwrap_or` or `unwrap_or_else`.
*
* @returns The contained `Some` value.
*
* @throws Throws if the self value equals `None`.
*/
unwrap(): S;
/**
* Returns the contained `Some` value or a provided default.
*
* @param defaultValue Default value returned when `None`.
* @returns Either contained `Some` value of `defaultValue`.
*/
unwrapOr<T>(defaultValue: T): S | T;
/**
* Returns the contained `Some` value or computes it from a closure.
*
* @param fn Closure to compute default value.
*/
unwrapOrElse<T>(fn: () => T): S | T;
/**
* Returns the contained Some value
*
* ### Recommended Message Style
*
* We recommend that `expect` messages are used to describe the reason you
* expect the `Option` should be `Some`.
*
* **Hint:** If you’re having trouble remembering how to phrase expect error
* messages remember to focus on the word “should” as in “env variable should
* be set by blah” or “the given binary should be available and executable by
* the current user”.
*
* @param message Error message.
* @returns The contained `Some` value.
*
* @throws Throws if the value is a `None` with a custom panic message
* provided by `msg`.
*/
expect(message: string): S;
}
class NoneImpl implements BaseOption<never> {
isSome(): this is Some<never> {
return false;
}
isSomeAnd(_fn: (value: never) => boolean): this is Some<never> {
return false;
}
isNone(): this is None {
return true;
}
match<T>(matcher: { none: () => T }): T {
return matcher.none();
}
map<T>(_fn: (value: never) => T): None {
return None;
}
mapOr<T>(defaultValue: T, _fn: (value: never) => T): T {
return defaultValue;
}
mapOrElse<T>(defaultFn: () => T, _fn: (value: never) => T): T {
return defaultFn();
}
okOr<E extends ToString>(error: E): Result<never, E> {
return Err(error);
}
okOrElse<E extends ToString>(err: () => E): Result<never, E> {
return Err(err());
}
and<T>(_other: Option<T>): None {
return None;
}
andThen<T>(_fn: (value: never) => Option<T>): None {
return None;
}
filter(_predicate: (value: never) => boolean): None {
return None;
}
or<T>(other: Option<T>): Option<T> {
return other;
}
orElse<T>(fn: () => Option<T>): Option<T> {
return fn();
}
xor<T>(other: Option<T>): Option<T> {
return other.isSome() ? other : None;
}
contains(_value: never): false {
return false;
}
zip<T>(_other: Option<T>): None {
return None;
}
zipWith<T, K>(_other: Option<T>, _fn: (self: never, other: T) => K): None {
return None;
}
unwrap(): never {
return this.expect('called `Option::unwrap()` on a `None` value');
}
unwrapOr<T>(defaultValue: T): T {
return defaultValue;
}
unwrapOrElse<T>(fn: () => T): T {
return fn();
}
expect(message: string): never {
throw new Error(message);
}
}
export const None = new NoneImpl();
export type None = NoneImpl;
Object.freeze(None);
class SomeImpl<S> implements BaseOption<S> {
constructor(public value: S) {}
isSome(): this is Some<S> {
return true;
}
isSomeAnd(op: (value: Readonly<S>) => boolean): this is Some<S> {
return op(this.value);
}
isNone(): this is None {
return false;
}
match<T>(matcher: { some: (value: Readonly<S>) => T }): T {
// See
// - https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1947
// - https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1946
// - https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1940
// eslint-disable-next-line unicorn/no-array-callback-reference
return matcher.some(this.value);
}
map<T>(fn: (value: Readonly<S>) => T): Some<T> {
return Some(fn(this.value));
}
mapOr<T>(_defaultValue: T, fn: (value: Readonly<S>) => T): T {
return fn(this.value);
}
mapOrElse<T>(_defaultFn: () => T, fn: (value: Readonly<S>) => T): T {
return fn(this.value);
}
okOr<E extends ToString>(_error: E): Result<S, never> {
return Ok(this.value);
}
okOrElse<E extends ToString>(_err: () => E): Result<S, never> {
return Ok(this.value);
}
and<T>(other: Option<T>): typeof other {
return other;
}
andThen<T>(fn: (value: Readonly<S>) => Option<T>): Option<T> {
return fn(this.value);
}
filter(predicate: (value: Readonly<S>) => boolean): Option<S> {
return predicate(this.value) ? this : None;
}
or<T>(_other: Option<T>): Some<S> {
return this;
}
orElse<T>(_fn: () => Option<T>): Some<S> {
return this;
}
xor<T>(other: Option<T>): Option<S> {
return other.isNone() ? this : None;
}
contains(value: S): boolean {
return this.value === value;
}
zip<T>(other: Option<T>): Option<[S, T]> {
return other.map((otherValue) => [this.value, otherValue]);
}
zipWith<T, K>(other: Option<T>, fn: (self: S, other: T) => K): Option<K> {
return other.map((otherValue) => fn(this.value, otherValue));
}
unwrap(): S {
return this.value;
}
unwrapOr<T>(_defaultValue: T): S {
return this.value;
}
unwrapOrElse<T>(_fn: () => T): S {
return this.value;
}
expect(_message: string): S {
return this.value;
}
}
export const Some = <T>(value: T) => new SomeImpl(value);
export type Some<S> = SomeImpl<S>;
export type Option<S> = Some<S> | None;
export function someIfDefined<T>(value: T | undefined): Option<T> {
return value === undefined ? None : Some(value);
}
export function someIfNotNull<T>(value: T | null): Option<T> {
return value === null ? None : Some(value);
}