UNPKG

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