UNPKG

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