1 | "use strict";
|
2 | var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
3 | if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
4 | if (ar || !(i in from)) {
|
5 | if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
6 | ar[i] = from[i];
|
7 | }
|
8 | }
|
9 | return to.concat(ar || Array.prototype.slice.call(from));
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | exports.atReadonlyRecord = exports.at = exports.indexReadonlyRecord = exports.indexReadonlyNonEmptyArray = exports.indexReadonlyArray = exports.index = exports.traversalTraverse = exports.fromTraversable = exports.ApplicativeIdentity = exports.traversalComposeTraversal = exports.traversal = exports.optionalFindFirstNonEmpty = exports.optionalFindFirst = exports.optionalKey = exports.optionalIndexNonEmpty = exports.optionalIndex = exports.optionalComposeOptional = exports.optionalModify = exports.optionalModifyOption = exports.optionalAsTraversal = exports.optional = exports.prismLeft = exports.prismRight = exports.prismSome = exports.prismFromPredicate = exports.prismFromNullable = exports.prismComposeLens = exports.prismSet = exports.prismModify = exports.prismModifyOption = exports.prismAsTraversal = exports.prismAsOptional = exports.prism = exports.lensAtKey = exports.lensComponent = exports.lensProps = exports.lensProp = exports.lensId = exports.lensComposePrism = exports.prismComposePrism = exports.lensComposeLens = exports.lensAsTraversal = exports.lensAsOptional = exports.lens = exports.isoAsTraversal = exports.isoAsOptional = exports.isoAsPrism = exports.isoAsLens = exports.iso = void 0;
|
13 | var RA = require("fp-ts/lib/ReadonlyArray");
|
14 | var RR = require("fp-ts/lib/ReadonlyRecord");
|
15 | var function_1 = require("fp-ts/lib/function");
|
16 | var O = require("fp-ts/lib/Option");
|
17 | var E = require("fp-ts/lib/Either");
|
18 | var pipeable_1 = require("fp-ts/lib/pipeable");
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var iso = function (get, reverseGet) { return ({
|
24 | get: get,
|
25 | reverseGet: reverseGet
|
26 | }); };
|
27 | exports.iso = iso;
|
28 | /** @internal */
|
29 | var isoAsLens = function (sa) { return (0, exports.lens)(sa.get, (0, function_1.flow)(sa.reverseGet, function_1.constant)); };
|
30 | exports.isoAsLens = isoAsLens;
|
31 |
|
32 | var isoAsPrism = function (sa) { return (0, exports.prism)((0, function_1.flow)(sa.get, O.some), sa.reverseGet); };
|
33 | exports.isoAsPrism = isoAsPrism;
|
34 |
|
35 | var isoAsOptional = function (sa) {
|
36 | return (0, exports.optional)((0, function_1.flow)(sa.get, O.some), (0, function_1.flow)(sa.reverseGet, function_1.constant));
|
37 | };
|
38 | exports.isoAsOptional = isoAsOptional;
|
39 |
|
40 | var isoAsTraversal = function (sa) {
|
41 | return (0, exports.traversal)(function (F) { return function (f) { return function (s) {
|
42 | return F.map(f(sa.get(s)), function (a) { return sa.reverseGet(a); });
|
43 | }; }; });
|
44 | };
|
45 | exports.isoAsTraversal = isoAsTraversal;
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | var lens = function (get, set) { return ({ get: get, set: set }); };
|
51 | exports.lens = lens;
|
52 | /** @internal */
|
53 | var lensAsOptional = function (sa) { return (0, exports.optional)((0, function_1.flow)(sa.get, O.some), sa.set); };
|
54 | exports.lensAsOptional = lensAsOptional;
|
55 |
|
56 | var lensAsTraversal = function (sa) {
|
57 | return (0, exports.traversal)(function (F) { return function (f) { return function (s) { return F.map(f(sa.get(s)), function (a) { return sa.set(a)(s); }); }; }; });
|
58 | };
|
59 | exports.lensAsTraversal = lensAsTraversal;
|
60 |
|
61 | var lensComposeLens = function (ab) { return function (sa) {
|
62 | return (0, exports.lens)(function (s) { return ab.get(sa.get(s)); }, function (b) { return function (s) { return sa.set(ab.set(b)(sa.get(s)))(s); }; });
|
63 | }; };
|
64 | exports.lensComposeLens = lensComposeLens;
|
65 |
|
66 | var prismComposePrism = function (ab) { return function (sa) {
|
67 | return (0, exports.prism)((0, function_1.flow)(sa.getOption, O.chain(ab.getOption)), (0, function_1.flow)(ab.reverseGet, sa.reverseGet));
|
68 | }; };
|
69 | exports.prismComposePrism = prismComposePrism;
|
70 |
|
71 | var lensComposePrism = function (ab) { return function (sa) {
|
72 | return (0, exports.optionalComposeOptional)((0, exports.prismAsOptional)(ab))((0, exports.lensAsOptional)(sa));
|
73 | }; };
|
74 | exports.lensComposePrism = lensComposePrism;
|
75 |
|
76 | var lensId = function () { return (0, exports.lens)(function_1.identity, function_1.constant); };
|
77 | exports.lensId = lensId;
|
78 |
|
79 | var lensProp = function (prop) { return function (sa) {
|
80 | return (0, exports.lens)(function (s) { return sa.get(s)[prop]; }, function (ap) { return function (s) {
|
81 | var _a;
|
82 | var oa = sa.get(s);
|
83 | if (ap === oa[prop]) {
|
84 | return s;
|
85 | }
|
86 | return sa.set(Object.assign({}, oa, (_a = {}, _a[prop] = ap, _a)))(s);
|
87 | }; });
|
88 | }; };
|
89 | exports.lensProp = lensProp;
|
90 |
|
91 | var lensProps = function () {
|
92 | var props = [];
|
93 | for (var _i = 0; _i < arguments.length; _i++) {
|
94 | props[_i] = arguments[_i];
|
95 | }
|
96 | return function (sa) {
|
97 | return (0, exports.lens)(function (s) {
|
98 | var a = sa.get(s);
|
99 | var r = {};
|
100 | for (var _i = 0, props_1 = props; _i < props_1.length; _i++) {
|
101 | var k = props_1[_i];
|
102 | r[k] = a[k];
|
103 | }
|
104 | return r;
|
105 | }, function (a) { return function (s) {
|
106 | var oa = sa.get(s);
|
107 | for (var _i = 0, props_2 = props; _i < props_2.length; _i++) {
|
108 | var k = props_2[_i];
|
109 | if (a[k] !== oa[k]) {
|
110 | return sa.set(Object.assign({}, oa, a))(s);
|
111 | }
|
112 | }
|
113 | return s;
|
114 | }; });
|
115 | };
|
116 | };
|
117 | exports.lensProps = lensProps;
|
118 |
|
119 | var lensComponent = function (prop) { return function (sa) {
|
120 | return (0, exports.lens)(function (s) { return sa.get(s)[prop]; }, function (ap) { return function (s) {
|
121 | var oa = sa.get(s);
|
122 | if (ap === oa[prop]) {
|
123 | return s;
|
124 | }
|
125 | var copy = oa.slice();
|
126 | copy[prop] = ap;
|
127 | return sa.set(copy)(s);
|
128 | }; });
|
129 | }; };
|
130 | exports.lensComponent = lensComponent;
|
131 |
|
132 | var lensAtKey = function (key) { return function (sa) {
|
133 | return (0, pipeable_1.pipe)(sa, (0, exports.lensComposeLens)(atReadonlyRecord().at(key)));
|
134 | }; };
|
135 | exports.lensAtKey = lensAtKey;
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | var prism = function (getOption, reverseGet) { return ({ getOption: getOption, reverseGet: reverseGet }); };
|
141 | exports.prism = prism;
|
142 |
|
143 | var prismAsOptional = function (sa) { return (0, exports.optional)(sa.getOption, function (a) { return (0, exports.prismSet)(a)(sa); }); };
|
144 | exports.prismAsOptional = prismAsOptional;
|
145 |
|
146 | var prismAsTraversal = function (sa) {
|
147 | return (0, exports.traversal)(function (F) { return function (f) { return function (s) {
|
148 | 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 (0, exports.prismSet)(a)(sa)(s); }); }));
|
149 | }; }; });
|
150 | };
|
151 | exports.prismAsTraversal = prismAsTraversal;
|
152 |
|
153 | var prismModifyOption = function (f) { return function (sa) { return function (s) {
|
154 | return (0, pipeable_1.pipe)(sa.getOption(s), O.map(function (o) {
|
155 | var n = f(o);
|
156 | return n === o ? s : sa.reverseGet(n);
|
157 | }));
|
158 | }; }; };
|
159 | exports.prismModifyOption = prismModifyOption;
|
160 |
|
161 | var prismModify = function (f) { return function (sa) {
|
162 | var g = (0, exports.prismModifyOption)(f)(sa);
|
163 | return function (s) {
|
164 | return (0, pipeable_1.pipe)(g(s), O.getOrElse(function () { return s; }));
|
165 | };
|
166 | }; };
|
167 | exports.prismModify = prismModify;
|
168 |
|
169 | var prismSet = function (a) { return (0, exports.prismModify)(function () { return a; }); };
|
170 | exports.prismSet = prismSet;
|
171 |
|
172 | var prismComposeLens = function (ab) { return function (sa) {
|
173 | return (0, exports.optionalComposeOptional)((0, exports.lensAsOptional)(ab))((0, exports.prismAsOptional)(sa));
|
174 | }; };
|
175 | exports.prismComposeLens = prismComposeLens;
|
176 |
|
177 | var prismFromNullable = function () { return (0, exports.prism)(O.fromNullable, function_1.identity); };
|
178 | exports.prismFromNullable = prismFromNullable;
|
179 |
|
180 | var prismFromPredicate = function (predicate) {
|
181 | return (0, exports.prism)(O.fromPredicate(predicate), function_1.identity);
|
182 | };
|
183 | exports.prismFromPredicate = prismFromPredicate;
|
184 |
|
185 | var prismSome = function () { return (0, exports.prism)(function_1.identity, O.some); };
|
186 | exports.prismSome = prismSome;
|
187 |
|
188 | var prismRight = function () { return (0, exports.prism)(O.fromEither, E.right); };
|
189 | exports.prismRight = prismRight;
|
190 |
|
191 | var prismLeft = function () {
|
192 | return (0, exports.prism)(function (s) { return (E.isLeft(s) ? O.some(s.left) : O.none); },
|
193 | E.left);
|
194 | };
|
195 | exports.prismLeft = prismLeft;
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | var optional = function (getOption, set) { return ({
|
201 | getOption: getOption,
|
202 | set: set
|
203 | }); };
|
204 | exports.optional = optional;
|
205 | /** @internal */
|
206 | var optionalAsTraversal = function (sa) {
|
207 | return (0, exports.traversal)(function (F) { return function (f) { return function (s) {
|
208 | 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); }); }));
|
209 | }; }; });
|
210 | };
|
211 | exports.optionalAsTraversal = optionalAsTraversal;
|
212 |
|
213 | var optionalModifyOption = function (f) { return function (optional) { return function (s) {
|
214 | return (0, pipeable_1.pipe)(optional.getOption(s), O.map(function (a) {
|
215 | var n = f(a);
|
216 | return n === a ? s : optional.set(n)(s);
|
217 | }));
|
218 | }; }; };
|
219 | exports.optionalModifyOption = optionalModifyOption;
|
220 |
|
221 | var optionalModify = function (f) { return function (optional) {
|
222 | var g = (0, exports.optionalModifyOption)(f)(optional);
|
223 | return function (s) {
|
224 | return (0, pipeable_1.pipe)(g(s), O.getOrElse(function () { return s; }));
|
225 | };
|
226 | }; };
|
227 | exports.optionalModify = optionalModify;
|
228 |
|
229 | var optionalComposeOptional = function (ab) { return function (sa) {
|
230 | return (0, exports.optional)((0, function_1.flow)(sa.getOption, O.chain(ab.getOption)), function (b) { return (0, exports.optionalModify)(ab.set(b))(sa); });
|
231 | }; };
|
232 | exports.optionalComposeOptional = optionalComposeOptional;
|
233 |
|
234 | var optionalIndex = function (i) { return function (sa) {
|
235 | return (0, pipeable_1.pipe)(sa, (0, exports.optionalComposeOptional)((0, exports.indexReadonlyArray)().index(i)));
|
236 | }; };
|
237 | exports.optionalIndex = optionalIndex;
|
238 |
|
239 | var optionalIndexNonEmpty = function (i) { return function (sa) { return (0, pipeable_1.pipe)(sa, (0, exports.optionalComposeOptional)((0, exports.indexReadonlyNonEmptyArray)().index(i))); }; };
|
240 | exports.optionalIndexNonEmpty = optionalIndexNonEmpty;
|
241 |
|
242 | var optionalKey = function (key) { return function (sa) {
|
243 | return (0, pipeable_1.pipe)(sa, (0, exports.optionalComposeOptional)((0, exports.indexReadonlyRecord)().index(key)));
|
244 | }; };
|
245 | exports.optionalKey = optionalKey;
|
246 |
|
247 | var optionalFindFirst = function (predicate) {
|
248 | return (0, exports.optional)(RA.findFirst(predicate), function (a) { return function (s) {
|
249 | return (0, pipeable_1.pipe)(RA.findIndex(predicate)(s), O.fold(function () { return s; }, function (i) { return RA.unsafeUpdateAt(i, a, s); }));
|
250 | }; });
|
251 | };
|
252 | exports.optionalFindFirst = optionalFindFirst;
|
253 | var unsafeUpdateAt = function (i, a, as) {
|
254 | if (as[i] === a) {
|
255 | return as;
|
256 | }
|
257 | else {
|
258 | var xs = __spreadArray([as[0]], as.slice(1), true);
|
259 | xs[i] = a;
|
260 | return xs;
|
261 | }
|
262 | };
|
263 |
|
264 | var optionalFindFirstNonEmpty = function (predicate) {
|
265 | return (0, exports.optional)(RA.findFirst(predicate), function (a) { return function (as) {
|
266 | return (0, pipeable_1.pipe)(RA.findIndex(predicate)(as), O.fold(function () { return as; }, function (i) { return unsafeUpdateAt(i, a, as); }));
|
267 | }; });
|
268 | };
|
269 | exports.optionalFindFirstNonEmpty = optionalFindFirstNonEmpty;
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | var traversal = function (modifyF) { return ({
|
275 | modifyF: modifyF
|
276 | }); };
|
277 | exports.traversal = traversal;
|
278 |
|
279 | function traversalComposeTraversal(ab) {
|
280 | return function (sa) { return (0, exports.traversal)(function (F) { return function (f) { return sa.modifyF(F)(ab.modifyF(F)(f)); }; }); };
|
281 | }
|
282 | exports.traversalComposeTraversal = traversalComposeTraversal;
|
283 |
|
284 | exports.ApplicativeIdentity = {
|
285 | URI: 'Identity',
|
286 | map: function (fa, f) { return f(fa); },
|
287 | of: function_1.identity,
|
288 | ap:
|
289 |
|
290 | function (fab, fa) { return fab(fa); }
|
291 | };
|
292 | var isIdentity = function (F) { return F.URI === 'Identity'; };
|
293 | function fromTraversable(T) {
|
294 | return function () {
|
295 | return (0, exports.traversal)(function (F) {
|
296 |
|
297 | var traverseF = isIdentity(F)
|
298 | ? T.map
|
299 | : T.traverse(F);
|
300 | return function (f) { return function (s) { return traverseF(s, f); }; };
|
301 | });
|
302 | };
|
303 | }
|
304 | exports.fromTraversable = fromTraversable;
|
305 |
|
306 | function traversalTraverse(T) {
|
307 | return traversalComposeTraversal(fromTraversable(T)());
|
308 | }
|
309 | exports.traversalTraverse = traversalTraverse;
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | var index = function (index) { return ({ index: index }); };
|
315 | exports.index = index;
|
316 |
|
317 | var indexReadonlyArray = function () {
|
318 | return (0, exports.index)(function (i) {
|
319 | return (0, exports.optional)(function (as) { return RA.lookup(i, as); }, function (a) { return function (as) {
|
320 | return (0, pipeable_1.pipe)(RA.lookup(i, as), O.fold(function () { return as; }, function () { return RA.unsafeUpdateAt(i, a, as); }));
|
321 | }; });
|
322 | });
|
323 | };
|
324 | exports.indexReadonlyArray = indexReadonlyArray;
|
325 |
|
326 | var indexReadonlyNonEmptyArray = function () {
|
327 | return (0, exports.index)(function (i) {
|
328 | return (0, exports.optional)(function (as) { return RA.lookup(i, as); }, function (a) { return function (as) {
|
329 | return (0, pipeable_1.pipe)(RA.lookup(i, as), O.fold(function () { return as; }, function () { return unsafeUpdateAt(i, a, as); }));
|
330 | }; });
|
331 | });
|
332 | };
|
333 | exports.indexReadonlyNonEmptyArray = indexReadonlyNonEmptyArray;
|
334 |
|
335 | var indexReadonlyRecord = function () {
|
336 | return (0, exports.index)(function (k) {
|
337 | return (0, exports.optional)(function (r) { return RR.lookup(k, r); }, function (a) { return function (r) {
|
338 | if (r[k] === a || O.isNone(RR.lookup(k, r))) {
|
339 | return r;
|
340 | }
|
341 | return RR.insertAt(k, a)(r);
|
342 | }; });
|
343 | });
|
344 | };
|
345 | exports.indexReadonlyRecord = indexReadonlyRecord;
|
346 |
|
347 |
|
348 |
|
349 |
|
350 | var at = function (at) { return ({ at: at }); };
|
351 | exports.at = at;
|
352 |
|
353 | function atReadonlyRecord() {
|
354 | return (0, exports.at)(function (key) {
|
355 | return (0, exports.lens)(function (r) { return RR.lookup(key, r); }, O.fold(function () { return RR.deleteAt(key); }, function (a) { return RR.insertAt(key, a); }));
|
356 | });
|
357 | }
|
358 | exports.atReadonlyRecord = atReadonlyRecord;
|