UNPKG

9.08 kBTypeScriptView Raw
1/**
2 * **This module is experimental**
3 *
4 * Experimental features are published in order to get early feedback from the community.
5 *
6 * A feature tagged as _Experimental_ is in a high state of flux, you're at risk of it changing without notice.
7 *
8 * An `Optional` is an optic used to zoom inside a product. Unlike the `Lens`, the element that the `Optional` focuses
9 * on may not exist.
10 *
11 * `Optional`s have two type parameters generally called `S` and `A`: `Optional<S, A>` where `S` represents the product
12 * and `A` an optional element inside of `S`.
13 *
14 * Laws:
15 *
16 * 1. `pipe(getOption(s), fold(() => s, a => set(a)(s))) = s`
17 * 2. `getOption(set(a)(s)) = pipe(getOption(s), map(_ => a))`
18 * 3. `set(a)(set(a)(s)) = set(a)(s)`
19 *
20 * @since 2.3.0
21 */
22import { Applicative, Applicative1, Applicative2, Applicative3 } from 'fp-ts/lib/Applicative'
23import { Category2 } from 'fp-ts/lib/Category'
24import { Either } from 'fp-ts/lib/Either'
25import { Predicate, Refinement } from 'fp-ts/lib/function'
26import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from 'fp-ts/lib/HKT'
27import { Invariant2 } from 'fp-ts/lib/Invariant'
28import * as O from 'fp-ts/lib/Option'
29import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray'
30import { ReadonlyRecord } from 'fp-ts/lib/ReadonlyRecord'
31import { Semigroupoid2 } from 'fp-ts/lib/Semigroupoid'
32import { Traversable1 } from 'fp-ts/lib/Traversable'
33import { Iso } from './Iso'
34import { Lens } from './Lens'
35import { Prism } from './Prism'
36import { Traversal } from './Traversal'
37import Option = O.Option
38/**
39 * @category model
40 * @since 2.3.0
41 */
42export interface Optional<S, A> {
43 readonly getOption: (s: S) => Option<A>
44 readonly set: (a: A) => (s: S) => S
45}
46/**
47 * @category constructors
48 * @since 2.3.8
49 */
50export declare const optional: <S, A>(
51 getOption: Optional<S, A>['getOption'],
52 set: Optional<S, A>['set']
53) => Optional<S, A>
54/**
55 * @category constructors
56 * @since 2.3.0
57 */
58export declare const id: <S>() => Optional<S, S>
59/**
60 * View a `Optional` as a `Traversal`.
61 *
62 * @category converters
63 * @since 2.3.0
64 */
65export declare const asTraversal: <S, A>(sa: Optional<S, A>) => Traversal<S, A>
66/**
67 * Compose a `Optional` with a `Optional`.
68 *
69 * @category compositions
70 * @since 2.3.0
71 */
72export declare const compose: <A, B>(ab: Optional<A, B>) => <S>(sa: Optional<S, A>) => Optional<S, B>
73/**
74 * Alias of `compose`.
75 *
76 * @category compositions
77 * @since 2.3.8
78 */
79export declare const composeOptional: <A, B>(ab: Optional<A, B>) => <S>(sa: Optional<S, A>) => Optional<S, B>
80/**
81 * Compose a `Optional` with a `Iso`.
82 *
83 * @category compositions
84 * @since 2.3.8
85 */
86export declare const composeIso: <A, B>(ab: Iso<A, B>) => <S>(sa: Optional<S, A>) => Optional<S, B>
87/**
88 * Compose a `Optional` with a `Lens`.
89 *
90 * @category compositions
91 * @since 2.3.7
92 */
93export declare const composeLens: <A, B>(ab: Lens<A, B>) => <S>(sa: Optional<S, A>) => Optional<S, B>
94/**
95 * Compose a `Optional` with a `Prism`.
96 *
97 * @category compositions
98 * @since 2.3.7
99 */
100export declare const composePrism: <A, B>(ab: Prism<A, B>) => <S>(sa: Optional<S, A>) => Optional<S, B>
101/**
102 * Compose a `Optional` with an `Traversal`.
103 *
104 * @category compositions
105 * @since 2.3.8
106 */
107export declare const composeTraversal: <A, B>(ab: Traversal<A, B>) => <S>(sa: Optional<S, A>) => Traversal<S, B>
108/**
109 * @category combinators
110 * @since 2.3.0
111 */
112export declare const modifyOption: <A, B extends A = A>(
113 f: (a: A) => B
114) => <S>(optional: Optional<S, A>) => (s: S) => Option<S>
115/**
116 * @category combinators
117 * @since 2.3.7
118 */
119export declare const setOption: <A>(a: A) => <S>(optional: Optional<S, A>) => (s: S) => O.Option<S>
120/**
121 * @category combinators
122 * @since 2.3.0
123 */
124export declare const modify: <A, B extends A = A>(f: (a: A) => B) => <S>(optional: Optional<S, A>) => (s: S) => S
125/**
126 * @category combinators
127 * @since 2.3.5
128 */
129export declare function modifyF<F extends URIS3>(
130 F: Applicative3<F>
131): <A, R, E>(f: (a: A) => Kind3<F, R, E, A>) => <S>(sa: Optional<S, A>) => (s: S) => Kind3<F, R, E, S>
132export declare function modifyF<F extends URIS2>(
133 F: Applicative2<F>
134): <A, E>(f: (a: A) => Kind2<F, E, A>) => <S>(sa: Optional<S, A>) => (s: S) => Kind2<F, E, S>
135export declare function modifyF<F extends URIS>(
136 F: Applicative1<F>
137): <A>(f: (a: A) => Kind<F, A>) => <S>(sa: Optional<S, A>) => (s: S) => Kind<F, S>
138export declare function modifyF<F>(
139 F: Applicative<F>
140): <A>(f: (a: A) => HKT<F, A>) => <S>(sa: Optional<S, A>) => (s: S) => HKT<F, S>
141/**
142 * Return an `Optional` from a `Optional` focused on a nullable value.
143 *
144 * @category combinators
145 * @since 2.3.3
146 */
147export declare const fromNullable: <S, A>(sa: Optional<S, A>) => Optional<S, NonNullable<A>>
148/**
149 * @category combinators
150 * @since 2.3.0
151 */
152export declare function filter<A, B extends A>(refinement: Refinement<A, B>): <S>(sa: Optional<S, A>) => Optional<S, B>
153export declare function filter<A>(predicate: Predicate<A>): <S>(sa: Optional<S, A>) => Optional<S, A>
154/**
155 * Return a `Optional` from a `Optional` and a prop.
156 *
157 * @category combinators
158 * @since 2.3.0
159 */
160export declare const prop: <A, P extends keyof A>(prop: P) => <S>(sa: Optional<S, A>) => Optional<S, A[P]>
161/**
162 * Return a `Optional` from a `Optional` and a list of props.
163 *
164 * @category combinators
165 * @since 2.3.0
166 */
167export declare const props: <A, P extends keyof A>(
168 props_0: P,
169 props_1: P,
170 ...props_2: P[]
171) => <S>(sa: Optional<S, A>) => Optional<S, { [K in P]: A[K] }>
172/**
173 * Return a `Optional` from a `Optional` focused on a component of a tuple.
174 *
175 * @category combinators
176 * @since 2.3.0
177 */
178export declare const component: <A extends readonly unknown[], P extends keyof A>(
179 prop: P
180) => <S>(sa: Optional<S, A>) => Optional<S, A[P]>
181/**
182 * Return a `Optional` from a `Optional` focused on an index of a `ReadonlyArray`.
183 *
184 * @category combinators
185 * @since 2.3.0
186 */
187export declare const index: (i: number) => <S, A>(sa: Optional<S, ReadonlyArray<A>>) => Optional<S, A>
188/**
189 * Return a `Optional` from a `Optional` focused on an index of a `ReadonlyNonEmptyArray`.
190 *
191 * @category combinators
192 * @since 2.3.8
193 */
194export declare const indexNonEmpty: (i: number) => <S, A>(sa: Optional<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A>
195/**
196 * Return a `Optional` from a `Optional` focused on a key of a `ReadonlyRecord`.
197 *
198 * @category combinators
199 * @since 2.3.0
200 */
201export declare const key: (key: string) => <S, A>(sa: Optional<S, ReadonlyRecord<string, A>>) => Optional<S, A>
202/**
203 * Return a `Optional` from a `Optional` focused on a required key of a `ReadonlyRecord`.
204 *
205 * @category combinators
206 * @since 2.3.0
207 */
208export declare const atKey: (
209 key: string
210) => <S, A>(sa: Optional<S, Readonly<Record<string, A>>>) => Optional<S, O.Option<A>>
211/**
212 * Return a `Optional` from a `Optional` focused on the `Some` of a `Option` type.
213 *
214 * @category combinators
215 * @since 2.3.0
216 */
217export declare const some: <S, A>(soa: Optional<S, Option<A>>) => Optional<S, A>
218/**
219 * Return a `Optional` from a `Optional` focused on the `Right` of a `Either` type.
220 *
221 * @category combinators
222 * @since 2.3.0
223 */
224export declare const right: <S, E, A>(sea: Optional<S, Either<E, A>>) => Optional<S, A>
225/**
226 * Return a `Optional` from a `Optional` focused on the `Left` of a `Either` type.
227 *
228 * @category combinators
229 * @since 2.3.0
230 */
231export declare const left: <S, E, A>(sea: Optional<S, Either<E, A>>) => Optional<S, E>
232/**
233 * Return a `Traversal` from a `Optional` focused on a `Traversable`.
234 *
235 * @category combinators
236 * @since 2.3.0
237 */
238export declare function traverse<T extends URIS>(
239 T: Traversable1<T>
240): <S, A>(sta: Optional<S, Kind<T, A>>) => Traversal<S, A>
241/**
242 * @category combinators
243 * @since 2.3.2
244 */
245export declare function findFirst<A, B extends A>(
246 refinement: Refinement<A, B>
247): <S>(sa: Optional<S, ReadonlyArray<A>>) => Optional<S, B>
248export declare function findFirst<A>(predicate: Predicate<A>): <S>(sa: Optional<S, ReadonlyArray<A>>) => Optional<S, A>
249/**
250 * @category combinators
251 * @since 2.3.8
252 */
253export declare function findFirstNonEmpty<A, B extends A>(
254 refinement: Refinement<A, B>
255): <S>(sa: Optional<S, ReadonlyNonEmptyArray<A>>) => Optional<S, B>
256export declare function findFirstNonEmpty<A>(
257 predicate: Predicate<A>
258): <S>(sa: Optional<S, ReadonlyNonEmptyArray<A>>) => Optional<S, A>
259/**
260 * @category Invariant
261 * @since 2.3.0
262 */
263export declare const imap: <A, B>(f: (a: A) => B, g: (b: B) => A) => <E>(fa: Optional<E, A>) => Optional<E, B>
264/**
265 * @category instances
266 * @since 2.3.0
267 */
268export declare const URI = 'monocle-ts/Optional'
269/**
270 * @category instances
271 * @since 2.3.0
272 */
273export declare type URI = typeof URI
274declare module 'fp-ts/lib/HKT' {
275 interface URItoKind2<E, A> {
276 readonly [URI]: Optional<E, A>
277 }
278}
279/**
280 * @category instances
281 * @since 2.3.0
282 */
283export declare const Invariant: Invariant2<URI>
284/**
285 * @category instances
286 * @since 2.3.8
287 */
288export declare const Semigroupoid: Semigroupoid2<URI>
289/**
290 * @category instances
291 * @since 2.3.0
292 */
293export declare const Category: Category2<URI>
294
\No newline at end of file