-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathindex.ts
215 lines (153 loc) · 6.98 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
import { ExtractValue } from '../types'
import { Result } from '../Result'
export declare type Some<A> = A
export declare type None = undefined | null
export declare type Option<A> = Some<A> | None
export declare const Some: <A>(value: NonNullable<A>) => Option<A>
export declare const None: Option<never>
export declare type TypeOfOption<T> = T extends Option<infer U>
? ExtractValue<U>
: never
export declare type TypeOfOptionArray<T extends readonly [...any[]]> =
T extends [infer Head, ...infer Tail]
? readonly [TypeOfOption<Head>, ...TypeOfOptionArray<Tail>]
: readonly []
export declare function makeSome<A>(value: NonNullable<A>): Option<A>
export declare function makeNone<A>(): Option<NonNullable<A>>
/** Returns `Some(value)` if the provided value is non-nullable, otherwise, returns `None`. */
export declare function fromNullable<A>(value: A): Option<NonNullable<A>>
/** Returns `Some(value)` if the provided value is not falsy, otherwise, returns `None`. */
export declare function fromFalsy<A>(value: A): Option<NonNullable<A>>
/** Returns `Some(value)` if the given predicate function returns `true`, otherwise, returns `None`. */
export declare function fromPredicate<A>(
value: A,
predicateFn: (value: NonNullable<A>) => boolean,
): Option<NonNullable<A>>
export declare function fromPredicate<A>(
predicateFn: (value: NonNullable<A>) => boolean,
): (value: A) => Option<NonNullable<A>>
/** Returns `Some(value)` (`value` is the result of `fn`) if `fn` didn't throw an error, otherwise, returns `None`. */
export declare function fromExecution<A>(fn: () => A): Option<NonNullable<A>>
/** Returns `Some(value)` if `promise` is resolved successfully, otherwise, returns `None`. */
export declare function fromPromise<A>(
promise: Promise<A>,
): Promise<Option<NonNullable<A>>>
/** Returns the result of `mapFn` if `option` is `Some(value)`, otherwise, returns `None` and `mapFn` is not called. */
export declare function map<A, B>(
option: Option<A>,
mapFn: (value: A) => NonNullable<B>,
): Option<B>
export declare function map<A, B>(
mapFn: (value: A) => NonNullable<B>,
): (option: Option<A>) => Option<B>
/** Returns the result of `mapFn` (it must have a return type of `Option`) if `option` is `Some(value)`, otherwise, returns `None`. */
export declare function flatMap<A, B>(
option: Option<A>,
mapFn: (value: A) => Option<B>,
): Option<B>
export declare function flatMap<A, B>(
mapFn: (value: A) => Option<B>,
): (option: Option<A>) => Option<B>
/** Returns the result of `mapFn` if `option` is `Some(value)`, otherwise, returns a default value. */
export declare function mapWithDefault<A, B extends NonNullable<any>>(
option: Option<A>,
defaultValue: B,
mapFn: (value: A) => B,
): B
export declare function mapWithDefault<A, B extends NonNullable<any>>(
defaultValue: B,
mapFn: (value: A) => B,
): (option: Option<A>) => B
/** Returns `Some(value)` if the result of `mapFn` is non-nullable, otherwise, returns `None`. */
export declare function mapNullable<A, B>(
option: Option<A>,
mapFn: (value: A) => B | null | undefined,
): Option<NonNullable<B>>
export declare function mapNullable<A, B>(
mapFn: (value: A) => B | null | undefined,
): (option: Option<A>) => Option<NonNullable<B>>
/** Returns `Some(value)` if `option` is `Some(value)` and the result of `predicateFn` is truthy, otherwise, returns `None`. */
export declare function filter<A>(
option: Option<A>,
predicateFn: (value: A) => boolean,
): Option<A>
export declare function filter<A>(
predicateFn: (value: A) => boolean,
): (option: Option<A>) => Option<A>
/** Returns `value` if `option` is `Some(value)`, otherwise, returns a default value. */
export declare function getWithDefault<A extends NonNullable<any>>(
option: Option<A>,
defaultValue: A,
): A
export declare function getWithDefault<A extends NonNullable<any>>(
defaultValue: A,
): (option: Option<A>) => A
/** Returns `value` if `option` is `Some(value)`, otherwise, throws an exception. */
export declare function getExn<A>(option: Option<A>): A | never
/** Returns `value` if `option` is `Some(value)`, otherwise, returns `null`. */
export declare function toNullable<A>(option: Option<A>): A | null
/** Returns `value` if `option` is `Some(value)`, otherwise, returns `undefined`. */
export declare function toUndefined<A>(option: Option<A>): A | undefined
/** Returns `Ok(value)` if `option` is `Some(value)`, otherwise, returns `Error(errorValue)`, both `Ok` and `Error` come from the `Result` type. */
export declare function toResult<A, B>(
option: Option<A>,
errorValue: B,
): Result<A, B>
export declare function toResult<A, B>(
errorValue: B,
): (option: Option<A>) => Result<A, B>
/** Returns the result of `someFn` if `option` is `Some(value)`, otherwise, returns the result of `noneFn`. */
export declare function match<A, B>(
option: Option<A>,
someFn: (value: A) => B,
noneFn: () => B,
): B
export declare function match<A, B>(
someFn: (value: A) => B,
noneFn: () => B,
): (option: Option<A>) => B
/** Returns `true` if the provided `option` is `None`, otherwise, returns `false`. */
export declare function isNone<A>(option: Option<A>): option is None
/** Returns `true` if the provided `option` is `Some(value)`, otherwise, returns `false`. */
export declare function isSome<A>(option: Option<A>): option is Some<A>
/** Applies a side-effect function to the value in `Some`, and returns the original `option`. */
export declare function tap<A>(
option: Option<A>,
someFn: (value: A) => void,
): Option<A>
export declare function tap<A>(
someFn: (value: A) => void,
): (option: Option<A>) => Option<A>
/** Checks if `option` is the `Some` variant and contains the given value. */
export declare function contains<A>(option: Option<A>, value: any): boolean
export declare function contains<A>(value: any): (option: Option<A>) => boolean
/** Combines two Options into a single Option containing a tuple of their values, if both Options are `Some` variants, otherwise, returns `None`. */
export declare function zip<A, B>(
sndOption: Option<B>,
): (fstOption: Option<A>) => Option<readonly [A, B]>
export declare function zip<A, B>(
fstOption: Option<A>,
sndOption: Option<B>,
): Option<readonly [A, B]>
/** Combines two Options into a single Option. The new value is produced by applying the given function to both values, if both Options are `Some` variants, otherwise, returns `None`. */
export declare function zipWith<A, B, C>(
fstOption: Option<A>,
sndOption: Option<B>,
mapFn: (arg0: A, arg1: B) => Option<C>,
): Option<C>
export declare function zipWith<A, B, C>(
sndOption: Option<B>,
mapFn: (arg0: A, arg1: B) => Option<C>,
): (fstOption: Option<A>) => Option<C>
export declare function fold<A, B>(
option: Option<A>,
someFn: (value: A) => B,
errorFn: () => B,
): B
export declare function fold<A, B>(
someFn: (value: A) => B,
errorFn: () => B,
): (option: Option<A>) => B
export declare function all<T extends readonly [...Option<any>[]]>(
xs: readonly [...T],
): Option<TypeOfOptionArray<T>>