UNPKG

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