UNPKG

8.5 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Category = exports.Semigroupoid = exports.URI = exports.getAll = exports.fold = exports.foldMap = exports.findFirstNonEmpty = exports.findFirst = exports.traverse = exports.left = exports.right = exports.some = exports.atKey = exports.key = exports.indexNonEmpty = exports.index = exports.component = exports.props = exports.prop = exports.filter = exports.fromNullable = exports.set = exports.modify = exports.composeOptional = exports.composePrism = exports.composeLens = exports.composeIso = exports.composeTraversal = exports.compose = exports.fromTraversable = exports.id = exports.traversal = void 0;
4var C = require("fp-ts/lib/Const");
5var function_1 = require("fp-ts/lib/function");
6var pipeable_1 = require("fp-ts/lib/pipeable");
7var RA = require("fp-ts/lib/ReadonlyArray");
8var _ = require("./internal");
9// -------------------------------------------------------------------------------------
10// constructors
11// -------------------------------------------------------------------------------------
12/**
13 * @category constructors
14 * @since 2.3.8
15 */
16exports.traversal = _.traversal;
17/**
18 * @category constructors
19 * @since 2.3.0
20 */
21var id = function () { return (0, exports.traversal)(function (_) { return function (f) { return f; }; }); };
22exports.id = id;
23/**
24 * Create a `Traversal` from a `Traversable`.
25 *
26 * @category constructor
27 * @since 2.3.0
28 */
29exports.fromTraversable = _.fromTraversable;
30// -------------------------------------------------------------------------------------
31// compositions
32// -------------------------------------------------------------------------------------
33/**
34 * Compose a `Traversal` with a `Traversal`.
35 *
36 * @category compositions
37 * @since 2.3.0
38 */
39exports.compose = _.traversalComposeTraversal;
40/**
41 * Alias of `compose`.
42 *
43 * @category compositions
44 * @since 2.3.8
45 */
46exports.composeTraversal = exports.compose;
47/**
48 * Compose a `Traversal` with a `Iso`.
49 *
50 * @category compositions
51 * @since 2.3.8
52 */
53exports.composeIso =
54/*#__PURE__*/
55(0, function_1.flow)(_.isoAsTraversal, exports.compose);
56/**
57 * Compose a `Traversal` with a `Lens`.
58 *
59 * @category compositions
60 * @since 2.3.8
61 */
62exports.composeLens =
63/*#__PURE__*/
64(0, function_1.flow)(_.lensAsTraversal, _.traversalComposeTraversal);
65/**
66 * Compose a `Traversal` with a `Prism`.
67 *
68 * @category compositions
69 * @since 2.3.8
70 */
71exports.composePrism =
72/*#__PURE__*/
73(0, function_1.flow)(_.prismAsTraversal, _.traversalComposeTraversal);
74/**
75 * Compose a `Traversal` with a `Optional`.
76 *
77 * @category compositions
78 * @since 2.3.8
79 */
80exports.composeOptional =
81/*#__PURE__*/
82(0, function_1.flow)(_.optionalAsTraversal, _.traversalComposeTraversal);
83// -------------------------------------------------------------------------------------
84// combinators
85// -------------------------------------------------------------------------------------
86/**
87 * @category combinators
88 * @since 2.3.0
89 */
90var modify = function (f) { return function (sa) {
91 return sa.modifyF(_.ApplicativeIdentity)(f);
92}; };
93exports.modify = modify;
94/**
95 * @category combinators
96 * @since 2.3.0
97 */
98var set = function (a) { return (0, exports.modify)(function () { return a; }); };
99exports.set = set;
100/**
101 * Return a `Traversal` from a `Traversal` focused on a nullable value.
102 *
103 * @category combinators
104 * @since 2.3.0
105 */
106var fromNullable = function (sa) {
107 return (0, exports.composePrism)(_.prismFromNullable())(sa);
108};
109exports.fromNullable = fromNullable;
110function filter(predicate) {
111 return (0, exports.compose)(_.prismAsTraversal(_.prismFromPredicate(predicate)));
112}
113exports.filter = filter;
114/**
115 * Return a `Traversal` from a `Traversal` and a prop.
116 *
117 * @category combinators
118 * @since 2.3.0
119 */
120var prop = function (prop) {
121 return (0, exports.compose)((0, pipeable_1.pipe)(_.lensId(), _.lensProp(prop), _.lensAsTraversal));
122};
123exports.prop = prop;
124/**
125 * Return a `Traversal` from a `Traversal` and a list of props.
126 *
127 * @category combinators
128 * @since 2.3.0
129 */
130var props = function () {
131 var props = [];
132 for (var _i = 0; _i < arguments.length; _i++) {
133 props[_i] = arguments[_i];
134 }
135 return (0, exports.compose)((0, pipeable_1.pipe)(_.lensId(), _.lensProps.apply(_, props), _.lensAsTraversal));
136};
137exports.props = props;
138/**
139 * Return a `Traversal` from a `Traversal` focused on a component of a tuple.
140 *
141 * @category combinators
142 * @since 2.3.0
143 */
144var component = function (prop) {
145 return (0, exports.compose)((0, pipeable_1.pipe)(_.lensId(), _.lensComponent(prop), _.lensAsTraversal));
146};
147exports.component = component;
148/**
149 * Return a `Traversal` from a `Traversal` focused on an index of a `ReadonlyArray`.
150 *
151 * @category combinators
152 * @since 2.3.0
153 */
154var index = function (i) { return function (sa) {
155 return (0, pipeable_1.pipe)(sa, (0, exports.compose)(_.optionalAsTraversal(_.indexReadonlyArray().index(i))));
156}; };
157exports.index = index;
158/**
159 * @category combinators
160 * @since 2.3.8
161 */
162var indexNonEmpty = function (i) { return function (sa) {
163 return (0, pipeable_1.pipe)(sa, (0, exports.compose)(_.optionalAsTraversal(_.indexReadonlyNonEmptyArray().index(i))));
164}; };
165exports.indexNonEmpty = indexNonEmpty;
166/**
167 * Return a `Traversal` from a `Traversal` focused on a key of a `ReadonlyRecord`.
168 *
169 * @category combinators
170 * @since 2.3.0
171 */
172var key = function (key) { return function (sa) {
173 return (0, pipeable_1.pipe)(sa, (0, exports.compose)(_.optionalAsTraversal(_.indexReadonlyRecord().index(key))));
174}; };
175exports.key = key;
176/**
177 * Return a `Traversal` from a `Traversal` focused on a required key of a `ReadonlyRecord`.
178 *
179 * @category combinators
180 * @since 2.3.0
181 */
182var atKey = function (key) { return function (sa) {
183 return (0, pipeable_1.pipe)(sa, (0, exports.compose)(_.lensAsTraversal(_.atReadonlyRecord().at(key))));
184}; };
185exports.atKey = atKey;
186/**
187 * Return a `Traversal` from a `Traversal` focused on the `Some` of a `Option` type.
188 *
189 * @category combinators
190 * @since 2.3.0
191 */
192exports.some =
193/*#__PURE__*/
194(0, exports.compose)(/*#__PURE__*/ _.prismAsTraversal(/*#__PURE__*/ _.prismSome()));
195/**
196 * Return a `Traversal` from a `Traversal` focused on the `Right` of a `Either` type.
197 *
198 * @category combinators
199 * @since 2.3.0
200 */
201exports.right =
202/*#__PURE__*/
203(0, exports.compose)(/*#__PURE__*/ _.prismAsTraversal(/*#__PURE__*/ _.prismRight()));
204/**
205 * Return a `Traversal` from a `Traversal` focused on the `Left` of a `Either` type.
206 *
207 * @category combinators
208 * @since 2.3.0
209 */
210exports.left =
211/*#__PURE__*/
212(0, exports.compose)(/*#__PURE__*/ _.prismAsTraversal(/*#__PURE__*/ _.prismLeft()));
213/**
214 * Return a `Traversal` from a `Traversal` focused on a `Traversable`.
215 *
216 * @category combinators
217 * @since 2.3.0
218 */
219exports.traverse = _.traversalTraverse;
220function findFirst(predicate) {
221 return (0, exports.composeOptional)(_.optionalFindFirst(predicate));
222}
223exports.findFirst = findFirst;
224function findFirstNonEmpty(predicate) {
225 return (0, exports.composeOptional)(_.optionalFindFirstNonEmpty(predicate));
226}
227exports.findFirstNonEmpty = findFirstNonEmpty;
228/**
229 * Map each target to a `Monoid` and combine the results.
230 *
231 * @category combinators
232 * @since 2.3.0
233 */
234var foldMap = function (M) { return function (f) { return function (sa) {
235 return sa.modifyF(C.getApplicative(M))(function (a) { return C.make(f(a)); });
236}; }; };
237exports.foldMap = foldMap;
238/**
239 * Map each target to a `Monoid` and combine the results.
240 *
241 * @category combinators
242 * @since 2.3.0
243 */
244var fold = function (M) { return (0, exports.foldMap)(M)(function_1.identity); };
245exports.fold = fold;
246/**
247 * Get all the targets of a `Traversal`.
248 *
249 * @category combinators
250 * @since 2.3.0
251 */
252var getAll = function (s) { return function (sa) {
253 return (0, exports.foldMap)(RA.getMonoid())(RA.of)(sa)(s);
254}; };
255exports.getAll = getAll;
256// -------------------------------------------------------------------------------------
257// instances
258// -------------------------------------------------------------------------------------
259/**
260 * @category instances
261 * @since 2.3.0
262 */
263exports.URI = 'monocle-ts/Traversal';
264/**
265 * @category instances
266 * @since 2.3.8
267 */
268exports.Semigroupoid = {
269 URI: exports.URI,
270 compose: function (ab, ea) { return (0, exports.compose)(ab)(ea); }
271};
272/**
273 * @category instances
274 * @since 2.3.0
275 */
276exports.Category = {
277 URI: exports.URI,
278 compose: exports.Semigroupoid.compose,
279 id: exports.id
280};