UNPKG

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