-
Notifications
You must be signed in to change notification settings - Fork 52
/
Optional.ts
413 lines (368 loc) · 12.2 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
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
413
/**
* **This module is experimental**
*
* Experimental features are published in order to get early feedback from the community.
*
* A feature tagged as _Experimental_ is in a high state of flux, you're at risk of it changing without notice.
*
* An `Optional` is an optic used to zoom inside a product. Unlike the `Lens`, the element that the `Optional` focuses
* on may not exist.
*
* `Optional`s have two type parameters generally called `S` and `A`: `Optional<S, A>` where `S` represents the product
* and `A` an optional element inside of `S`.
*
* Laws:
*
* 1. `pipe(getOption(s), fold(() => s, a => set(a)(s))) = s`
* 2. `getOption(set(a)(s)) = pipe(getOption(s), map(_ => a))`
* 3. `set(a)(set(a)(s)) = set(a)(s)`
*
* @since 2.3.0
*/
import { Applicative, Applicative1, Applicative2, Applicative3 } from 'fp-ts/lib/Applicative'
import { Category2 } from 'fp-ts/lib/Category'
import { Either } from 'fp-ts/lib/Either'
import { constant, flow, Predicate, Refinement } from 'fp-ts/lib/function'
import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from 'fp-ts/lib/HKT'
import { Invariant2 } from 'fp-ts/lib/Invariant'
import * as O from 'fp-ts/lib/Option'
import { pipe } from 'fp-ts/lib/pipeable'
import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray'
import { ReadonlyRecord } from 'fp-ts/lib/ReadonlyRecord'
import { Semigroupoid2 } from 'fp-ts/lib/Semigroupoid'
import { Traversable1 } from 'fp-ts/lib/Traversable'
import * as _ from './internal'
import { Iso } from './Iso'
import { Lens } from './Lens'
import { Prism } from './Prism'
import { Traversal } from './Traversal'
// -------------------------------------------------------------------------------------
// model
// -------------------------------------------------------------------------------------
import Option = O.Option
/**
* @category model
* @since 2.3.0
*/
export interface Optional<S, A> {
readonly getOption: (s: S) => Option<A>
readonly set: (a: A) => (s: S) => S
}
// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------
/**
* @category constructors
* @since 2.3.8
*/
export const optional: <S, A>(getOption: Optional<S, A>['getOption'], set: Optional<S, A>['set']) => Optional<S, A> =
_.optional
/**
* @category constructors
* @since 2.3.0
*/
export const id = <S>(): Optional<S, S> => optional(O.some, constant)
// -------------------------------------------------------------------------------------
// converters
// -------------------------------------------------------------------------------------
/**
* View a `Optional` as a `Traversal`.
*
* @category converters
* @since 2.3.0
*/
export const asTraversal: <S, A>(sa: Optional<S, A>) => Traversal<S, A> = _.optionalAsTraversal
// -------------------------------------------------------------------------------------
// compositions
// -------------------------------------------------------------------------------------
/**
* Compose a `Optional` with a `Optional`.
*
* @category compositions
* @since 2.3.0
*/
export const compose: <A, B>(ab: Optional<A, B>) => <S>(sa: Optional<S, A>) => Optional<S, B> =
_.optionalComposeOptional
/**
* Alias of `compose`.
*
* @category compositions
* @since 2.3.8
*/
export const composeOptional = compose
/**
* Compose a `Optional` with a `Iso`.
*
* @category compositions
* @since 2.3.8
*/
export const composeIso: <A, B>(ab: Iso<A, B>) => <S>(sa: Optional<S, A>) => Optional<S, B> =
/*#__PURE__*/
flow(_.isoAsOptional, compose)
/**
* Compose a `Optional` with a `Lens`.
*
* @category compositions
* @since 2.3.7
*/
export const composeLens: <A, B>(ab: Lens<A, B>) => <S>(sa: Optional<S, A>) => Optional<S, B> =
/*#__PURE__*/
flow(_.lensAsOptional, _.optionalComposeOptional)
/**
* Compose a `Optional` with a `Prism`.
*
* @category compositions
* @since 2.3.7
*/
export const composePrism: <A, B>(ab: Prism<A, B>) => <S>(sa: Optional<S, A>) => Optional<S, B> =
/*#__PURE__*/
flow(_.prismAsOptional, _.optionalComposeOptional)
/**
* Compose a `Optional` with an `Traversal`.
*
* @category compositions
* @since 2.3.8
*/
export const composeTraversal = <A, B>(ab: Traversal<A, B>): (<S>(sa: Optional<S, A>) => Traversal<S, B>) =>
flow(asTraversal, _.traversalComposeTraversal(ab))
// -------------------------------------------------------------------------------------
// combinators
// -------------------------------------------------------------------------------------
/**
* @category combinators
* @since 2.3.0
*/
export const modifyOption: <A, B extends A = A>(
f: (a: A) => B
) => <S>(optional: Optional<S, A>) => (s: S) => Option<S> = _.optionalModifyOption
/**
* @category combinators
* @since 2.3.7
*/
export const setOption = <A>(a: A): (<S>(optional: Optional<S, A>) => (s: S) => Option<S>) => modifyOption(() => a)
/**
* @category combinators
* @since 2.3.0
*/
export const modify: <A, B extends A = A>(f: (a: A) => B) => <S>(optional: Optional<S, A>) => (s: S) => S =
_.optionalModify
/**
* @category combinators
* @since 2.3.5
*/
export function modifyF<F extends URIS3>(
F: Applicative3<F>
): <A, R, E>(f: (a: A) => Kind3<F, R, E, A>) => <S>(sa: Optional<S, A>) => (s: S) => Kind3<F, R, E, S>
export function modifyF<F extends URIS2>(
F: Applicative2<F>
): <A, E>(f: (a: A) => Kind2<F, E, A>) => <S>(sa: Optional<S, A>) => (s: S) => Kind2<F, E, S>
export function modifyF<F extends URIS>(
F: Applicative1<F>
): <A>(f: (a: A) => Kind<F, A>) => <S>(sa: Optional<S, A>) => (s: S) => Kind<F, S>
export function modifyF<F>(
F: Applicative<F>
): <A>(f: (a: A) => HKT<F, A>) => <S>(sa: Optional<S, A>) => (s: S) => HKT<F, S>
export function modifyF<F>(
F: Applicative<F>
): <A>(f: (a: A) => HKT<F, A>) => <S>(sa: Optional<S, A>) => (s: S) => HKT<F, S> {
return (f) => (sa) => (s) =>
pipe(
sa.getOption(s),
O.fold(
() => F.of(s),
(a) => F.map(f(a), (a) => sa.set(a)(s))
)
)
}
/**
* Return an `Optional` from a `Optional` focused on a nullable value.
*
* @category combinators
* @since 2.3.3
*/
export const fromNullable: <S, A>(sa: Optional<S, A>) => Optional<S, NonNullable<A>> =
/*#__PURE__*/
compose(/*#__PURE__*/ _.prismAsOptional(/*#__PURE__*/ _.prismFromNullable()))
/**
* @category combinators
* @since 2.3.0
*/
export function filter<A, B extends A>(refinement: Refinement<A, B>): <S>(sa: Optional<S, A>) => Optional<S, B>
export function filter<A>(predicate: Predicate<A>): <S>(sa: Optional<S, A>) => Optional<S, A>
export function filter<A>(predicate: Predicate<A>): <S>(sa: Optional<S, A>) => Optional<S, A> {
return compose(_.prismAsOptional(_.prismFromPredicate(predicate)))
}
/**
* Return a `Optional` from a `Optional` and a prop.
*
* @category combinators
* @since 2.3.0
*/
export const prop = <A, P extends keyof A>(prop: P): (<S>(sa: Optional<S, A>) => Optional<S, A[P]>) =>
compose(pipe(_.lensId<A>(), _.lensProp(prop), _.lensAsOptional))
/**
* Return a `Optional` from a `Optional` and a list of props.
*
* @category combinators
* @since 2.3.0
*/
export const props = <A, P extends keyof A>(
...props: readonly [P, P, ...ReadonlyArray<P>]
): (<S>(sa: Optional<S, A>) => Optional<S, { [K in P]: A[K] }>) =>
compose(pipe(_.lensId<A>(), _.lensProps(...props), _.lensAsOptional))
/**
* Return a `Optional` from a `Optional` focused on a component of a tuple.
*
* @category combinators
* @since 2.3.0
*/
export const component = <A extends ReadonlyArray<unknown>, P extends keyof A>(
prop: P
): (<S>(sa: Optional<S, A>) => Optional<S, A[P]>) =>
compose(pipe(_.lensId<A>(), _.lensComponent(prop), _.lensAsOptional))
/**
* Return a `Optional` from a `Optional` focused on an index of a `ReadonlyArray`.
*
* @category combinators
* @since 2.3.0
*/
export const index: (i: number) => <S, A>(sa: Optional<S, ReadonlyArray<A>>) => Optional<S, A> = _.optionalIndex
/**
* Return a `Optional` from a `Optional` focused on an index of a `ReadonlyNonEmptyArray`.
*
* @category combinators
* @since 2.3.8
*/
export const indexNonEmpty: (i: number) => <S, A>(sa: Optional<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> =
_.optionalIndexNonEmpty
/**
* Return a `Optional` from a `Optional` focused on a key of a `ReadonlyRecord`.
*
* @category combinators
* @since 2.3.0
*/
export const key: (key: string) => <S, A>(sa: Optional<S, ReadonlyRecord<string, A>>) => Optional<S, A> = _.optionalKey
/**
* Return a `Optional` from a `Optional` focused on a required key of a `ReadonlyRecord`.
*
* @category combinators
* @since 2.3.0
*/
export const atKey = (key: string) => <S, A>(sa: Optional<S, ReadonlyRecord<string, A>>): Optional<S, Option<A>> =>
pipe(sa, compose(_.lensAsOptional(_.atReadonlyRecord<A>().at(key))))
/**
* Return a `Optional` from a `Optional` focused on the `Some` of a `Option` type.
*
* @category combinators
* @since 2.3.0
*/
export const some: <S, A>(soa: Optional<S, Option<A>>) => Optional<S, A> =
/*#__PURE__*/
compose(/*#__PURE__*/ _.prismAsOptional(/*#__PURE__*/ _.prismSome()))
/**
* Return a `Optional` from a `Optional` focused on the `Right` of a `Either` type.
*
* @category combinators
* @since 2.3.0
*/
export const right: <S, E, A>(sea: Optional<S, Either<E, A>>) => Optional<S, A> =
/*#__PURE__*/
compose(/*#__PURE__*/ _.prismAsOptional(/*#__PURE__*/ _.prismRight()))
/**
* Return a `Optional` from a `Optional` focused on the `Left` of a `Either` type.
*
* @category combinators
* @since 2.3.0
*/
export const left: <S, E, A>(sea: Optional<S, Either<E, A>>) => Optional<S, E> =
/*#__PURE__*/
compose(/*#__PURE__*/ _.prismAsOptional(/*#__PURE__*/ _.prismLeft()))
/**
* Return a `Traversal` from a `Optional` focused on a `Traversable`.
*
* @category combinators
* @since 2.3.0
*/
export function traverse<T extends URIS>(T: Traversable1<T>): <S, A>(sta: Optional<S, Kind<T, A>>) => Traversal<S, A> {
return flow(asTraversal, _.traversalTraverse(T))
}
/**
* @category combinators
* @since 2.3.2
*/
export function findFirst<A, B extends A>(
refinement: Refinement<A, B>
): <S>(sa: Optional<S, ReadonlyArray<A>>) => Optional<S, B>
export function findFirst<A>(predicate: Predicate<A>): <S>(sa: Optional<S, ReadonlyArray<A>>) => Optional<S, A>
export function findFirst<A>(predicate: Predicate<A>): <S>(sa: Optional<S, ReadonlyArray<A>>) => Optional<S, A> {
return compose(_.optionalFindFirst(predicate))
}
/**
* @category combinators
* @since 2.3.8
*/
export function findFirstNonEmpty<A, B extends A>(
refinement: Refinement<A, B>
): <S>(sa: Optional<S, ReadonlyNonEmptyArray<A>>) => Optional<S, B>
export function findFirstNonEmpty<A>(
predicate: Predicate<A>
): <S>(sa: Optional<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A>
export function findFirstNonEmpty<A>(
predicate: Predicate<A>
): <S>(sa: Optional<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A> {
return compose(_.optionalFindFirstNonEmpty(predicate))
}
// -------------------------------------------------------------------------------------
// pipeables
// -------------------------------------------------------------------------------------
/**
* @category Invariant
* @since 2.3.0
*/
export const imap: <A, B>(f: (a: A) => B, g: (b: B) => A) => <E>(fa: Optional<E, A>) => Optional<E, B> = (f, g) => (
ea
) => imap_(ea, f, g)
// -------------------------------------------------------------------------------------
// instances
// -------------------------------------------------------------------------------------
const imap_: Invariant2<URI>['imap'] = (ea, ab, ba) => optional(flow(ea.getOption, O.map(ab)), flow(ba, ea.set))
/**
* @category instances
* @since 2.3.0
*/
export const URI = 'monocle-ts/Optional'
/**
* @category instances
* @since 2.3.0
*/
export type URI = typeof URI
declare module 'fp-ts/lib/HKT' {
interface URItoKind2<E, A> {
readonly [URI]: Optional<E, A>
}
}
/**
* @category instances
* @since 2.3.0
*/
export const Invariant: Invariant2<URI> = {
URI,
imap: imap_
}
/**
* @category instances
* @since 2.3.8
*/
export const Semigroupoid: Semigroupoid2<URI> = {
URI,
compose: (ab, ea) => compose(ab)(ea)
}
/**
* @category instances
* @since 2.3.0
*/
export const Category: Category2<URI> = {
URI,
compose: Semigroupoid.compose,
id
}