-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
365 lines (275 loc) · 7.75 KB
/
index.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
/**
* Copyright (c) 2019 shogogg <[email protected]>
*
* This software is released under the MIA License.
* http://opensource.org/licenses/mit-license.php
*/
export interface Matcher<A, B> {
some: (_: A) => B
none: () => B
}
export interface OptionLike<A> {
/**
* Returns true if the option is non-empty and the predicate p returns true when applied to the option's value.
*/
exists (p: (_: A) => boolean): boolean
/**
* Returns the option if it is non-empty and applying the predicate p to the option's value returns true.
*/
filter (p: (_: A) => boolean): Option<A>
/**
* Returns the option if it is non-empty and applying the predicate p to the option's value returns false.
*/
filterNot (p: (_: A) => boolean): Option<A>
/**
* Returns the result of applying f to the option's value if the option is non-empty.
*/
flatMap<B> (f: (_: A) => Option<B>): Option<B>
/**
* Returns the result of applying f to the option's value if the option is non-empty.
* Otherwise, evaluates expression ifEmpty.
*/
fold<B> (ifEmpty: () => B): (f: (_: A) => B) => B
/**
* Tests whether a predicate holds for all elements of the option.
*/
forAll (p: (_: A) => boolean): boolean
/**
* Performs a for-comprehension like flatMap and map operation using the given functions
*/
forComprehension (...fns: ((x: any) => Option<any>)[]): Option<any>
/**
* Apply the given procedure f to the option's value, if it is non-empty.
*/
forEach (f: (_: A) => void): void
/**
* Returns the option's value if the option is non-empty, otherwise throws an error.
*/
readonly get: A
/**
* Returns the option's value if the option is non-empty, otherwise return the result of evaluating defaultValue.
*/
getOrElse (defaultValue: () => A): A
/**
* Returns the option's value if the option is non-empty, otherwise return defaultValue.
*/
getOrElseValue (defaultValue: A): A
/**
* Returns true if the option's value is non-empty, false otherwise.
*/
readonly isDefined: boolean
/**
* Returns true if the option's value is empty, false otherwise.
*/
readonly isEmpty: boolean
/**
* Builds a new option by applying a function to all elements of this option.
*/
map<B> (f: (_: A) => B): Option<B>
/**
* Pattern match signature.
*/
match<B> (matcher: Matcher<A, B>): B
/**
* Returns true if the option's value is non-empty, false otherwise.
*/
readonly nonEmpty: boolean
/**
* Returns the option itself if it is non-empty, otherwise return the result of evaluating alternative.
*/
orElse (alternative: () => Option<A>): Option<A>
/**
* Returns the option itself if it is non-empty, otherwise return the alternative.
*/
orElseValue (alternative: Option<A>): Option<A>
/**
* Returns the option's value if it is non-empty, or null if it is empty.
*/
readonly orNull: A | null
/**
* Returns the option's value if it is non-empty, or undefined if it is empty.
*/
readonly orUndefined: A | undefined
/**
* Converts the option to an array.
*/
readonly toArray: A[]
/**
* Returns a string representation
*/
toString (): string
}
export abstract class Option<A> implements OptionLike<A> {
abstract exists (p: (_: A) => boolean): boolean
abstract filter (p: (_: A) => boolean): Option<A>
abstract filterNot (p: (_: A) => boolean): Option<A>
abstract flatMap<B> (f: (_: A) => Option<B>): Option<B>
abstract fold<B> (ifEmpty: () => B): (f: (_: A) => B) => B
abstract forAll (p: (_: A) => boolean): boolean
abstract forComprehension (...fns: ((x: any) => Option<any>)[]): Option<any>
abstract forEach (f: (_: A) => void): void
readonly get: A
abstract getOrElse (defaultValue: () => A): A
abstract getOrElseValue (defaultValue: A): A
readonly isDefined: boolean
readonly isEmpty: boolean
abstract map<B> (f: (_: A) => B): Option<B>
abstract match<B> (matcher: Matcher<A, B>): B
readonly nonEmpty: boolean
abstract orElse (alternative: () => Option<A>): Option<A>
abstract orElseValue (alternative: Option<A>): Option<A>
readonly orNull: A | null
readonly orUndefined: A | undefined
readonly toArray: A[]
abstract toString (): string
}
export class Some<A> extends Option<A> implements OptionLike<A> {
constructor (private readonly value: A) {
super()
}
exists (p: (_: A) => boolean): boolean {
return p(this.value)
}
filter (p: (_: A) => boolean): Option<A> {
return p(this.value) ? this : none
}
filterNot (p: (_: A) => boolean): Option<A> {
return p(this.value) ? none : this
}
flatMap<B> (f: (_: A) => Option<B>): Option<B> {
return f(this.value)
}
fold<B> (/* ifEmpty: () => B) */): (f: (_: A) => B) => B {
return f => f(this.value)
}
forAll (p: (_: A) => boolean): boolean {
return p(this.value)
}
forComprehension (...fns: ((x: any) => Option<any>)[]): Option<any> {
let result: Option<any> = this
for (let i = 0; i < fns.length - 1; ++i) {
result = result.flatMap<any>(fns[i])
}
return result.map(fns[fns.length - 1])
}
forEach (f: (_: A) => void): void {
return f(this.value)
}
get get (): A {
return this.value
}
getOrElse (/* defaultValue: () => A) */): A {
return this.value
}
getOrElseValue (/* defaultValue: A */): A {
return this.value
}
get isDefined (): boolean {
return true
}
get isEmpty (): boolean {
return false
}
map<B> (f: (_: A) => B): Option<B> {
return some(f(this.value))
}
match<B> (matcher: Matcher<A, B>): B {
return matcher.some(this.value)
}
get nonEmpty (): boolean {
return true
}
orElse (/* alternative: () => Option<A> */): Option<A> {
return this
}
orElseValue (/* alternative: Option<A> */): Option<A> {
return this
}
get orNull (): A | null {
return this.value
}
get orUndefined (): A | undefined {
return this.value
}
get toArray (): A[] {
return [this.value]
}
toString (): string {
return 'Some(' + this.value + ')'
}
}
export class None<A> extends Option<A> implements OptionLike<A> {
exists (/* p: (_: A) => boolean */): boolean {
return false
}
filter (/* p: (_: A) => boolean */): Option<A> {
return this
}
filterNot (/* p: (_: A) => boolean */): Option<A> {
return this
}
flatMap<B> (/* f: (_: A) => Option<B> */): Option<B> {
return none
}
fold<B> (ifEmpty: () => B): (f: (_: A) => B) => B {
return () => ifEmpty()
}
forAll (/* p: (_: A) => boolean */): boolean {
return true
}
forComprehension (/* ...fns: ((x: any) => Option<any>)[] */): Option<any> {
return none
}
forEach (): void {
// do nothing.
}
get get (): A {
throw new Error('No such element.')
}
getOrElse (defaultValue: () => A): A {
return defaultValue()
}
getOrElseValue (defaultValue: A): A {
return defaultValue
}
get isDefined (): boolean {
return false
}
get isEmpty (): boolean {
return true
}
map<B> (/* f: (_: A) => B */): Option<B> {
return none
}
match<B> (matcher: Matcher<A, B>): B {
return matcher.none()
}
get nonEmpty (): boolean {
return false
}
orElse (alternative: () => Option<A>): Option<A> {
return alternative()
}
orElseValue (alternative: Option<A>): Option<A> {
return alternative
}
get orNull (): A | null {
return null
}
get orUndefined (): A | undefined {
return undefined
}
get toArray (): Array<A> {
return []
}
toString (): string {
return 'None'
}
}
export function some<A> (value: A): Option<A> {
return new Some(value)
}
export const none: Option<never> = new None()
export function option<A> (value?: A | null): Option<A> {
return value === null || typeof value === 'undefined' ? none : some(value)
}