-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespace.ts
310 lines (272 loc) · 8.37 KB
/
namespace.ts
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
import { RemoteData } from "../RemoteData/namespace";
import { Result } from "../Result/namespace";
import { Equals, IsAnyOrUnknown } from "../util/type";
import { Some, Some as SomeImpl } from "./Some/namespace";
import { None, None as NoneImpl } from "./None/namespace";
/**
*
* Type: An `Option<A>` is a value that may or may not be present. Values of
* this type are either `Some<A>`, a desired value of type `A`, or `None`, the
* lack of that value encoded as `undefined`.
*
*/
export type Option<A> = A | undefined;
export declare namespace Option {
export { Some, None };
}
/**
*
* Namespace: The `Option` namespace contains functions for `Option` values
* including constructors, type guards, conversions to other data types, and
* operations over the type.
*
*/
export namespace Option {
//
// Namespaces
//
Option.Some = SomeImpl;
Option.None = NoneImpl;
//
// Types
//
type Some<A> = Option.Some<A>;
type None = Option.None;
/* Create a type where each member of the array `T` is a `Some`. */
type SomeMapped<T extends ReadonlyArray<any>> = {
[k in keyof T]: Some<T[k]>;
};
/* Primitive/literal types which are considered false in a boolean context. */
type Falsy = false | undefined | null | "" | 0;
/* Predicate to test if every array element is a Some value. */
type IsAllSome<T extends ReadonlyArray<any>> = IsAnyOrUnknown<
T[number]
> extends 1
? 0
: Equals<Extract<T[number], undefined>, never>;
/* Predicate to test if at least one array element is strictly None. */
type HasStrictlyNoneElement<T extends ReadonlyArray<any>> = {
[k in keyof T]: IsAnyOrUnknown<T[k]> extends 0
? Exclude<T[k], undefined> extends never
? T[k]
: never
: never;
}[number] extends never
? 0
: 1;
/* Transform type with shape `Option<A>[]` into `Option<A[]>`, while preserving
* readonly arrays and tuples.
*/
type ConsolidateTuple<T extends ReadonlyArray<any>> =
HasStrictlyNoneElement<T> extends 1
? None
: IsAllSome<T> extends 1
? T
: Option<SomeMapped<T>>;
/* The `match` function expects either exhaustive pattern matching, or
* non-exhaustive with a `default` case.
*/
type MatchPattern<A, B> =
| {
Some: (x: Some<A>) => B;
None: () => B;
}
| {
Some?: (x: Some<A>) => B;
None?: () => B;
default: () => B;
};
//
// Constructors
//
/**
* A constructor for the `Some` variant of `Option`. It accepts any value `a`
* except for `undefined`.
*/
export const some = Some.of;
/**
* A constructor for the `Some` variant of `Option`. It accepts any value `a`
* except for `undefined`.
*/
export const of = Some.of;
/**
* A constructor for the `None` variant of `Option`, which is an alias
* for undefined.
*/
export const none = None.value;
//
// Typeguards
//
/** Typeguard for the `Some` variant of a `Option`. */
export const isSome = Some.isType;
/** Typeguard for the `None` variant of a `Option`. */
export const isNone = None.isType;
//
// Conversions
//
/**
* Create a `Option` from a `Result` by replacing an `Err` with `None`.
*
* Ok<V> -> Some<V>
* Err<E> -> None
*/
export function fromResult<A>(x: A): Option<Result.Ok<A>> {
return Result.isOk(x) ? x : undefined;
}
/**
* Create a `Option` from a `RemoteData` by mapping `Success` to `Some` and
* everything else to `None`.
*
* NotAsked -> None
* Loading -> None
* Success<V> -> Some<V>
* Err<E> -> None
*/
export function fromRemoteData<A>(
x: RemoteData.Success<A>,
): RemoteData.Success<A>;
export function fromRemoteData<
A extends RemoteData.NotAsked | RemoteData.Loading | Error,
>(x: A): None;
export function fromRemoteData<A>(x: A): Option<RemoteData.Success<A>>;
export function fromRemoteData(x: unknown) {
return RemoteData.isSuccess(x) ? x : undefined;
}
/**
* Given a value which might be null, return a `Option`. In other words,
* substitute null with undefined.
*
* null -> None
* undefined -> None
* A -> Some<A>
*/
export function fromNullable<A>(x: A): Option<Exclude<A, null>> {
return x == null ? none : (x as Exclude<A, null>);
}
/**
* Keeps the value `a` if `test` returns true, otherwise returns `None`.
* Supports narrowing the return type via typeguards.
*/
export function fromPredicate<A, B extends A>(
a: A,
test: (a: A) => a is B,
): Option<B>;
export function fromPredicate<A>(a: A, test: (a: A) => boolean): Option<A>;
export function fromPredicate<A>(a: A, test: (a: A) => boolean) {
return test(a) ? a : none;
}
/** Keep truthy values, return `None` for falsy values such as `null`, `0` and `""`. */
export function fromFalsy<A>(x: A | Falsy): Option<A> {
return !!x ? x : none;
}
/**
* Given a `Option`, return a value which might be null. In other words, replace
* undefined with null.
*
* Some<A> -> A
* None -> null
*/
export function toNullable<A>(x: Option<A>): Some<A> | null {
return isSome(x) ? x : null;
}
/**
* Create a `Result` from a `Option` by providing the `Err` to use in place of a `None`.
*
* Some<A> -> Ok<A>
* None -> Err<E>
*/
export function toResult<V, E extends Error>(
x: Option<V>,
e: E,
): Result<Some<V>, E> {
return isSome(x) ? x : e;
}
/**
* Create a `RemoteData` from a `Option` by returning either a `NotAsked` or `Success`
*
* Some<A> -> Success<A>
* None -> NotAsked
*/
export function toRemoteData<V>(
x: Option<V>,
): RemoteData.Success<Some<V>> | RemoteData.NotAsked {
return isNone(x) ? RemoteData.notAsked : (x as RemoteData.Success<Some<V>>);
}
/**
* Assert that `x` is a `Some`. If the assertion holds then return `x` with an
* updated type. If the assertion fails throw an error.
*/
export const coerceSome = Some.coerce;
/**
* Assert that `x` is `None`. If the assertion holds then return `x` with an
* updated type. If the assertion fails throw an error.
*/
export const coerceNone = None.coerce;
//
// Operations
//
/** Apply `fn` if `x` is a `Some`. Otherwise return `None`. */
export const ifSome = Some.ifType;
/** Call `fn` if `x` is `None`. Otherwise return `x`. */
export const ifNone = None.ifType;
/** Return `defaultVal` if `x` is `None`. Otherwise return `x`. */
export function orDefault<A>(x: None, defaultVal: A): A;
export function orDefault<A, B>(x: Some<A>, defaultVal: B): Some<A>;
export function orDefault<A, B>(x: Option<A>, defaultVal: B): Some<A> | B;
export function orDefault<A, B>(x: Option<A>, defaultVal: B) {
return isNone(x) ? defaultVal : x;
}
/**
* Similar to a `case` expression in languages with pattern matching. The
* `pattern` object either needs to be exhastive or needs to have a `default`
* branch.
*/
export function match<A, B>(x: Option<A>, pattern: MatchPattern<A, B>): B {
if (isSome(x) && pattern["Some"]) {
return pattern["Some"](x);
} else if (isNone(x) && pattern["None"]) {
return pattern["None"]();
} else {
return (pattern as any)["default"]();
}
}
/**
* If the values in the `xs` array are all `Some`s then return a shallow copy
* of the array. Otherwise return `None`. At a type level this function takes
* values of type `Option<A>[]` and returns values of type `Option<A[]>`.
*/
export function consolidate<T extends ReadonlyArray<any>>(
xs: T,
): ConsolidateTuple<T>;
export function consolidate<A>(xs: Array<Option<A>>): Option<Array<A>>;
export function consolidate(xs: ReadonlyArray<unknown>) {
const someVals = xs.filter(isSome);
return someVals.length === xs.length ? someVals : none;
}
/**
* Create a version of a function which returns a `Option` instead of throwing
* an error.
*/
export function encase<Args extends Array<any>, R>(fn: (...args: Args) => R) {
return (...args: Args): Option<R> => {
try {
return fn(...args);
} catch {
return none;
}
};
}
/**
* Given a promise, return a promise which will always fulfill, catching
* rejected values as `None`.
*/
export async function encasePromise<A>(
p: PromiseLike<A>,
): Promise<Option<A>> {
try {
return await p;
} catch {
return none;
}
}
}