UNPKG

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