UNPKG

9.23 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 `Traversal` is the generalisation of an `Optional` to several targets. In other word, a `Traversal` allows to focus
9 * from a type `S` into `0` to `n` values of type `A`.
10 *
11 * The most common example of a `Traversal` would be to focus into all elements inside of a container (e.g.
12 * `ReadonlyArray`, `Option`). To do this we will use the relation between the typeclass `Traversable` and `Traversal`.
13 *
14 * @since 2.3.0
15 */
16import { Applicative, Applicative1, Applicative2, Applicative2C, Applicative3 } from 'fp-ts/lib/Applicative'
17import { Category2 } from 'fp-ts/lib/Category'
18import { Either } from 'fp-ts/lib/Either'
19import { Predicate, Refinement } from 'fp-ts/lib/function'
20import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from 'fp-ts/lib/HKT'
21import { Monoid } from 'fp-ts/lib/Monoid'
22import { Option } from 'fp-ts/lib/Option'
23import { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray'
24import { ReadonlyRecord } from 'fp-ts/lib/ReadonlyRecord'
25import { Semigroupoid2 } from 'fp-ts/lib/Semigroupoid'
26import { Traversable, Traversable1, Traversable2, Traversable3 } from 'fp-ts/lib/Traversable'
27import { Iso } from './Iso'
28import { Lens } from './Lens'
29import { Optional } from './Optional'
30import { Prism } from './Prism'
31/**
32 * @category model
33 * @since 2.3.0
34 */
35export interface ModifyF<S, A> {
36 <F extends URIS3>(F: Applicative3<F>): <R, E>(f: (a: A) => Kind3<F, R, E, A>) => (s: S) => Kind3<F, R, E, S>
37 <F extends URIS2>(F: Applicative2<F>): <E>(f: (a: A) => Kind2<F, E, A>) => (s: S) => Kind2<F, E, S>
38 <F extends URIS2, E>(F: Applicative2C<F, E>): (f: (a: A) => Kind2<F, E, A>) => (s: S) => Kind2<F, E, S>
39 <F extends URIS>(F: Applicative1<F>): (f: (a: A) => Kind<F, A>) => (s: S) => Kind<F, S>
40 <F>(F: Applicative<F>): (f: (a: A) => HKT<F, A>) => (s: S) => HKT<F, S>
41}
42/**
43 * @category model
44 * @since 2.3.0
45 */
46export interface Traversal<S, A> {
47 readonly modifyF: ModifyF<S, A>
48}
49/**
50 * @category constructors
51 * @since 2.3.8
52 */
53export declare const traversal: <S, A>(modifyF: Traversal<S, A>['modifyF']) => Traversal<S, A>
54/**
55 * @category constructors
56 * @since 2.3.0
57 */
58export declare const id: <S>() => Traversal<S, S>
59/**
60 * Create a `Traversal` from a `Traversable`.
61 *
62 * @category constructor
63 * @since 2.3.0
64 */
65export declare const fromTraversable: {
66 <T extends URIS3>(T: Traversable3<T>): <R, E, A>() => Traversal<Kind3<T, R, E, A>, A>
67 <T extends URIS2>(T: Traversable2<T>): <E, A>() => Traversal<Kind2<T, E, A>, A>
68 <T extends URIS>(T: Traversable1<T>): <A>() => Traversal<Kind<T, A>, A>
69 <T>(T: Traversable<T>): <A>() => Traversal<HKT<T, A>, A>
70}
71/**
72 * Compose a `Traversal` with a `Traversal`.
73 *
74 * @category compositions
75 * @since 2.3.0
76 */
77export declare const compose: <A, B>(ab: Traversal<A, B>) => <S>(sa: Traversal<S, A>) => Traversal<S, B>
78/**
79 * Alias of `compose`.
80 *
81 * @category compositions
82 * @since 2.3.8
83 */
84export declare const composeTraversal: <A, B>(ab: Traversal<A, B>) => <S>(sa: Traversal<S, A>) => Traversal<S, B>
85/**
86 * Compose a `Traversal` with a `Iso`.
87 *
88 * @category compositions
89 * @since 2.3.8
90 */
91export declare const composeIso: <A, B>(ab: Iso<A, B>) => <S>(sa: Traversal<S, A>) => Traversal<S, B>
92/**
93 * Compose a `Traversal` 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: Traversal<S, A>) => Traversal<S, B>
99/**
100 * Compose a `Traversal` 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: Traversal<S, A>) => Traversal<S, B>
106/**
107 * Compose a `Traversal` 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: Traversal<S, A>) => Traversal<S, B>
113/**
114 * @category combinators
115 * @since 2.3.0
116 */
117export declare const modify: <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Traversal<S, A>) => (s: S) => S
118/**
119 * @category combinators
120 * @since 2.3.0
121 */
122export declare const set: <A>(a: A) => <S>(sa: Traversal<S, A>) => (s: S) => S
123/**
124 * Return a `Traversal` from a `Traversal` focused on a nullable value.
125 *
126 * @category combinators
127 * @since 2.3.0
128 */
129export declare const fromNullable: <S, A>(sa: Traversal<S, A>) => Traversal<S, NonNullable<A>>
130/**
131 * @category combinators
132 * @since 2.3.0
133 */
134export declare function filter<A, B extends A>(
135 refinement: Refinement<A, B>
136): <S>(sa: Traversal<S, A>) => Traversal<S, B>
137export declare function filter<A>(predicate: Predicate<A>): <S>(sa: Traversal<S, A>) => Traversal<S, A>
138/**
139 * Return a `Traversal` from a `Traversal` and a prop.
140 *
141 * @category combinators
142 * @since 2.3.0
143 */
144export declare const prop: <A, P extends keyof A>(prop: P) => <S>(sa: Traversal<S, A>) => Traversal<S, A[P]>
145/**
146 * Return a `Traversal` from a `Traversal` and a list of props.
147 *
148 * @category combinators
149 * @since 2.3.0
150 */
151export declare const props: <A, P extends keyof A>(
152 props_0: P,
153 props_1: P,
154 ...props_2: P[]
155) => <S>(sa: Traversal<S, A>) => Traversal<S, { [K in P]: A[K] }>
156/**
157 * Return a `Traversal` from a `Traversal` focused on a component of a tuple.
158 *
159 * @category combinators
160 * @since 2.3.0
161 */
162export declare const component: <A extends readonly unknown[], P extends keyof A>(
163 prop: P
164) => <S>(sa: Traversal<S, A>) => Traversal<S, A[P]>
165/**
166 * Return a `Traversal` from a `Traversal` focused on an index of a `ReadonlyArray`.
167 *
168 * @category combinators
169 * @since 2.3.0
170 */
171export declare const index: (i: number) => <S, A>(sa: Traversal<S, readonly A[]>) => Traversal<S, A>
172/**
173 * @category combinators
174 * @since 2.3.8
175 */
176export declare const indexNonEmpty: (i: number) => <S, A>(sa: Traversal<S, ReadonlyNonEmptyArray<A>>) => Traversal<S, A>
177/**
178 * Return a `Traversal` from a `Traversal` focused on a key of a `ReadonlyRecord`.
179 *
180 * @category combinators
181 * @since 2.3.0
182 */
183export declare const key: (key: string) => <S, A>(sa: Traversal<S, Readonly<Record<string, A>>>) => Traversal<S, A>
184/**
185 * Return a `Traversal` from a `Traversal` focused on a required key of a `ReadonlyRecord`.
186 *
187 * @category combinators
188 * @since 2.3.0
189 */
190export declare const atKey: (
191 key: string
192) => <S, A>(sa: Traversal<S, Readonly<Record<string, A>>>) => Traversal<S, Option<A>>
193/**
194 * Return a `Traversal` from a `Traversal` focused on the `Some` of a `Option` type.
195 *
196 * @category combinators
197 * @since 2.3.0
198 */
199export declare const some: <S, A>(soa: Traversal<S, Option<A>>) => Traversal<S, A>
200/**
201 * Return a `Traversal` from a `Traversal` focused on the `Right` of a `Either` type.
202 *
203 * @category combinators
204 * @since 2.3.0
205 */
206export declare const right: <S, E, A>(sea: Traversal<S, Either<E, A>>) => Traversal<S, A>
207/**
208 * Return a `Traversal` from a `Traversal` focused on the `Left` of a `Either` type.
209 *
210 * @category combinators
211 * @since 2.3.0
212 */
213export declare const left: <S, E, A>(sea: Traversal<S, Either<E, A>>) => Traversal<S, E>
214/**
215 * Return a `Traversal` from a `Traversal` focused on a `Traversable`.
216 *
217 * @category combinators
218 * @since 2.3.0
219 */
220export declare const traverse: <T extends URIS>(
221 T: Traversable1<T>
222) => <S, A>(sta: Traversal<S, Kind<T, A>>) => Traversal<S, A>
223/**
224 * @category combinators
225 * @since 2.3.8
226 */
227export declare function findFirst<A, B extends A>(
228 refinement: Refinement<A, B>
229): <S>(sa: Traversal<S, ReadonlyArray<A>>) => Traversal<S, B>
230export declare function findFirst<A>(
231 predicate: Predicate<A>
232): <S>(sa: Traversal<S, ReadonlyArray<A>>) => Traversal<S, A>
233/**
234 * @category combinators
235 * @since 2.3.8
236 */
237export declare function findFirstNonEmpty<A, B extends A>(
238 refinement: Refinement<A, B>
239): <S>(sa: Traversal<S, ReadonlyNonEmptyArray<A>>) => Traversal<S, B>
240export declare function findFirstNonEmpty<A>(
241 predicate: Predicate<A>
242): <S>(sa: Traversal<S, ReadonlyNonEmptyArray<A>>) => Traversal<S, A>
243/**
244 * Map each target to a `Monoid` and combine the results.
245 *
246 * @category combinators
247 * @since 2.3.0
248 */
249export declare const foldMap: <M>(M: Monoid<M>) => <A>(f: (a: A) => M) => <S>(sa: Traversal<S, A>) => (s: S) => M
250/**
251 * Map each target to a `Monoid` and combine the results.
252 *
253 * @category combinators
254 * @since 2.3.0
255 */
256export declare const fold: <A>(M: Monoid<A>) => <S>(sa: Traversal<S, A>) => (s: S) => A
257/**
258 * Get all the targets of a `Traversal`.
259 *
260 * @category combinators
261 * @since 2.3.0
262 */
263export declare const getAll: <S>(s: S) => <A>(sa: Traversal<S, A>) => readonly A[]
264/**
265 * @category instances
266 * @since 2.3.0
267 */
268export declare const URI = 'monocle-ts/Traversal'
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]: Traversal<E, A>
277 }
278}
279/**
280 * @category instances
281 * @since 2.3.8
282 */
283export declare const Semigroupoid: Semigroupoid2<URI>
284/**
285 * @category instances
286 * @since 2.3.0
287 */
288export declare const Category: Category2<URI>
289
\No newline at end of file