-
Notifications
You must be signed in to change notification settings - Fork 4
/
optional.ts
386 lines (324 loc) · 8.99 KB
/
optional.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
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
import { Result, UnpackResult, Ok, Err, is_err, is_ok } from './result'
/** `Optional.None` unwrap panic error */
export const OPTIONAL_ERROR: Error = new Error(`Unwrapped Optional.None`)
/** `Optional` transpose error */
export const OPTIONAL_TRANSPOSE: Error = new TypeError(`Only Result<T, E> can be transpose`)
/** `Optional` type */
export const enum OptionalType {
/** `Optional.Some` type */
Some,
/** `Optional.None` type */
None
}
/** unpack a `Optional<T>` transform to `T` */
export type UnpackOptional<O> = O extends Optional<infer T> ? T : never
/** a container that have (Some) or not (None) value */
export interface Optional<T> {
/** the optional container value, readonly */
readonly value: T | never
/** the optional container type, `Some` or `None`, readonly */
readonly type: OptionalType
/**
* predicate function, test instance was or not a Optional.Some
*
* @example```ts
* assert.strictEqual(Some(42).is_some(), true)
* assert.strictEqual(None.is_some(), false)
* ```
*/
is_some(): boolean
/**
* predicate function, test instance was or not a Optional.None
*
* @example```ts
* assert.strictEqual(Some(42).is_none(), false)
* assert.strictEqual(None.is_none(), true)
* ```
*/
is_none(): boolean
/// ITERS ///
/**
* maps a `Optional<T>` to `Optional<U>`
*
* @param fn mapper function
* @example
* const inc = (a: number): number => a + 1
* assert.deepStrictEqual(Some(42).map(inc), Some(43))
* assert.deepStrictEqual(None.map(inc), None)
*/
map<U>(fn: (v: T) => U): Optional<U>
map_async<U>(fn: (v: T) => Promise<U>): Promise<Optional<U>>
/**
* @todo mapOrElse
*/
// mapOr()
// mapOrElse()
// forEach()
/**
* filter a `Optional<T>` by a predicate function, return `None` if result is `false`
*
* @param fn predicate function
*/
filter(fn: (v: T) => boolean): Optional<T>
filter_async(fn: (v: T) => Promise<boolean>): Promise<Optional<T>>
// flatMap()
/**
* `and` operation for Optional
*
* | Left | Right | Result |
* |-------|-------|--------|
* | SomeL | SomeR | SomeR |
* | Some | None | None |
* | None | None | None |
* | None | None | None |
*
* @param opt target optional
* @example```ts
* assert.deepStrictEqual(Some(42).add(Some(41)), Some(41))
* assert.deepStrictEqual(Some(42).add(None), None)
* assert.deepStrictEqual(None.add(Some(42)), None)
* assert.deepStrictEqual(None.add(None), None)
* ```
*/
and<U>(opt: Optional<U>): Optional<T | U>
/**
* `and` operation for lazily evaluated
*
* @param opt target optional
*/
and_then<U>(op: (v: T) => Optional<U>): Optional<T | U>
and_then_async<U>(op: (v: T) => Promise<Optional<U>>): Promise<Optional<U>>
/**
* `or` operation for Optional
*
* | Left | Right | Result |
* |-------|-------|--------|
* | SomeL | SomeR | SomeL |
* | Some | None | Some |
* | None | Some | Some |
* | None | None | None |
*
* @param opt new result
*/
or<U>(opt: Optional<U>): Optional<T | U>
/**
* `or` operation for lazily evaluated
*
* @param op apply function
*/
or_else<U>(op: (v: T) => Optional<U>): Optional<T | U>
or_else_async<U>(op: (v: T) => Promise<Optional<U>>): Promise<Optional<T | U>>
// xor<U>(opt: Optional<U>): Optional<U>
/**
* unwrap the container, throw when `None`
*/
unwrap(): T
/**
* unwrap the container, or get default when `None`
*
* @param u option value
*/
unwrap_or<U>(u: U): T | U
/**
* `unwrapOr` operation for lazily evaluated
*
* @param op caller function
*/
unwrap_or_else<U>(op: () => U): T | U
unwrap_or_else_async<U>(op: () => Promise<U>): Promise<T | U>
/// RESULT ///
/**
* transform `Optional<T>` to `Result<T, E>`, mapping `Some(v)` to `Ok(v)`, and
* `None` to `Err(e)`
*/
ok_or<E>(e: E): Result<T, E>
/**
* okOr for lazily evaluated
*
* @param e err mapper
*/
ok_or_else<E>(e: () => E): Result<T, E>
ok_or_else_async<E>(e: () => Promise<E>): Promise<Result<T, E>>
/**
* transposes an `Optional` of a `Result` into a `Result` of an `Optional`,
* 1. `None` will be mapped to `Ok(None)`
* 2. `Some(Ok(v))` will be mapped to `Ok(Some(v))`
* 3. `Some(Err(v))` will be mapped to `Err(v)`
*/
transpose<U extends UnpackResult<T>>(): Result<Optional<U[0]> | never, U[1]>
}
class OptionalSome<T> implements Optional<T> {
public readonly type = OptionalType.Some
constructor(public readonly value: T) {}
map<U>(fn: (v: T) => U): Optional<U> {
// use momo's solution
return new OptionalSome(fn(this.value))
/* try {
return new _Some(fn(this.value))
} catch(_) {
return None
} */
}
async map_async<U>(fn: (v: T) => Promise<U>): Promise<Optional<U>> {
return new OptionalSome(await fn(this.value))
}
filter(fn: (v: T) => boolean): Optional<T> {
return fn(this.value) ? new OptionalSome(this.value) : None
}
async filter_async(fn: (v: T) => Promise<boolean>): Promise<Optional<T>> {
return await fn(this.value) ? new OptionalSome(this.value) : None
}
is_some(): boolean {
return true
}
is_none(): boolean {
return false
}
and<U>(opt: Optional<U>): Optional<U> {
return opt
}
and_then<U>(op: (v: T) => Optional<U>): Optional<U> {
return op(this.value)
}
async and_then_async<U>(op: (v: T) => Promise<Optional<U>>): Promise<Optional<U>> {
return op(this.value)
}
or<U>(_opt: Optional<U>): Optional<T> {
return new OptionalSome(this.value)
}
or_else<U>(_op: (v: T) => Optional<U>): Optional<T> {
return new OptionalSome(this.value)
}
async or_else_async<U>(_op: (v: T) => Promise<Optional<U>>): Promise<Optional<T | U>> {
return new OptionalSome(this.value)
}
unwrap(): T {
return this.value
}
unwrap_or<U>(_v: U): T {
return this.value
}
unwrap_or_else<U>(_op: () => U): T {
return this.value
}
async unwrap_or_else_async<U>(_op: () => Promise<U>): Promise<T> {
return this.value
}
ok_or(_e: never): Result<T, never> {
return Ok(this.value)
}
ok_or_else(_e: () => never): Result<T, never>{
return Ok(this.value)
}
async ok_or_else_async(_e: () => Promise<never>): Promise<Result<T, never>> {
return Ok(this.value)
}
transpose<U extends UnpackResult<T>>(): Result<Optional<U[0]> | never, U[1]> {
if(is_err<U[1]>(this.value)) {
return Err(this.value.unwrap_err())
} else if(is_ok<U[0]>(this.value)) {
return Ok(Some(this.value.unwrap()))
} else {
throw OPTIONAL_TRANSPOSE
}
}
}
class OptionalNone implements Optional<never> {
public readonly value!: never
public readonly type = OptionalType.None
map<U>(_fn: (_v: never) => U): Optional<never> {
return None
}
async map_async<U>(_fn: (_v: never) => Promise<U>): Promise<Optional<never>> {
return None
}
filter(_fn: (v: never) => boolean): Optional<never> {
return None
}
async filter_async(_fn: (v: never) => Promise<boolean>): Promise<Optional<never>> {
return None
}
is_some(): boolean {
return false
}
is_none(): boolean {
return true
}
and<U>(_opt: Optional<U>): Optional<never> {
return None
}
and_then<U>(_op: (v: never) => Optional<U>): Optional<never> {
return None
}
async and_then_async<U>(_op: (v: never) => Promise<Optional<U>>): Promise<Optional<never>> {
return None
}
or<U>(opt: Optional<U>): Optional<U> {
return opt
}
or_else<U>(op: (v: never) => Optional<U>): Optional<U> {
return op(this.value)
}
async or_else_async<U>(op: (v: never) => Promise<Optional<U>>): Promise<Optional<U>> {
return op(this.value)
}
unwrap(): never {
throw OPTIONAL_ERROR
}
unwrap_or<U>(v: U): U {
return v
}
unwrap_or_else<U>(op: (e: never) => U): U {
return op(this.value)
}
async unwrap_or_else_async<U>(op: (e: never) => Promise<U>): Promise<U> {
return op(this.value)
}
ok_or<E>(e: E): Result<never, E> {
return Err(e)
}
ok_or_else<E>(e: () => E): Result<never, E> {
return Err(e())
}
async ok_or_else_async<E>(e: () => Promise<E>): Promise<Result<never, E>> {
return Err(await e())
}
transpose(): Result<Optional<never>, never> {
return Ok(None)
}
}
/**
* Optional.Some
*
* @param v value
*/
export function Some<T>(v: T): Optional<T> {
return new OptionalSome(v)
}
/** Optional.None */
export const None: Optional<never> = new OptionalNone()
/// PREDICATE ///
/**
* test given is or not a Optional
*
* @param result target result
*/
export function is_optional<T>(value: unknown): value is Optional<T> {
return is_some(value) || is_none(value)
}
/**
* test value is or not Optional.Some
*
* @param value value
*/
export function is_some<T>(value: unknown): value is Optional<T> {
return value instanceof OptionalSome
}
/**
* test value is or not Optional.Some
*
* @param value value
*/
export function is_none(value: unknown): value is Optional<never> {
return value === None
}