UNPKG

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