UNPKG

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