UNPKG

16.5 kBJavaScriptView Raw
1"use strict";
2var __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};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.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;
13var RA = require("fp-ts/lib/ReadonlyArray");
14var RR = require("fp-ts/lib/ReadonlyRecord");
15var function_1 = require("fp-ts/lib/function");
16var O = require("fp-ts/lib/Option");
17var E = require("fp-ts/lib/Either");
18var pipeable_1 = require("fp-ts/lib/pipeable");
19// -------------------------------------------------------------------------------------
20// Iso
21// -------------------------------------------------------------------------------------
22/** @internal */
23var iso = function (get, reverseGet) { return ({
24 get: get,
25 reverseGet: reverseGet
26}); };
27exports.iso = iso;
28/** @internal */
29var isoAsLens = function (sa) { return (0, exports.lens)(sa.get, (0, function_1.flow)(sa.reverseGet, function_1.constant)); };
30exports.isoAsLens = isoAsLens;
31/** @internal */
32var isoAsPrism = function (sa) { return (0, exports.prism)((0, function_1.flow)(sa.get, O.some), sa.reverseGet); };
33exports.isoAsPrism = isoAsPrism;
34/** @internal */
35var 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};
38exports.isoAsOptional = isoAsOptional;
39/** @internal */
40var 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};
45exports.isoAsTraversal = isoAsTraversal;
46// -------------------------------------------------------------------------------------
47// Lens
48// -------------------------------------------------------------------------------------
49/** @internal */
50var lens = function (get, set) { return ({ get: get, set: set }); };
51exports.lens = lens;
52/** @internal */
53var lensAsOptional = function (sa) { return (0, exports.optional)((0, function_1.flow)(sa.get, O.some), sa.set); };
54exports.lensAsOptional = lensAsOptional;
55/** @internal */
56var 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};
59exports.lensAsTraversal = lensAsTraversal;
60/** @internal */
61var 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}; };
64exports.lensComposeLens = lensComposeLens;
65/** @internal */
66var 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}; };
69exports.prismComposePrism = prismComposePrism;
70/** @internal */
71var lensComposePrism = function (ab) { return function (sa) {
72 return (0, exports.optionalComposeOptional)((0, exports.prismAsOptional)(ab))((0, exports.lensAsOptional)(sa));
73}; };
74exports.lensComposePrism = lensComposePrism;
75/** @internal */
76var lensId = function () { return (0, exports.lens)(function_1.identity, function_1.constant); };
77exports.lensId = lensId;
78/** @internal */
79var 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}; };
89exports.lensProp = lensProp;
90/** @internal */
91var 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};
117exports.lensProps = lensProps;
118/** @internal */
119var 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}; };
130exports.lensComponent = lensComponent;
131/** @internal */
132var lensAtKey = function (key) { return function (sa) {
133 return (0, pipeable_1.pipe)(sa, (0, exports.lensComposeLens)(atReadonlyRecord().at(key)));
134}; };
135exports.lensAtKey = lensAtKey;
136// -------------------------------------------------------------------------------------
137// Prism
138// -------------------------------------------------------------------------------------
139/** @internal */
140var prism = function (getOption, reverseGet) { return ({ getOption: getOption, reverseGet: reverseGet }); };
141exports.prism = prism;
142/** @internal */
143var prismAsOptional = function (sa) { return (0, exports.optional)(sa.getOption, function (a) { return (0, exports.prismSet)(a)(sa); }); };
144exports.prismAsOptional = prismAsOptional;
145/** @internal */
146var 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};
151exports.prismAsTraversal = prismAsTraversal;
152/** @internal */
153var 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}; }; };
159exports.prismModifyOption = prismModifyOption;
160/** @internal */
161var 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}; };
167exports.prismModify = prismModify;
168/** @internal */
169var prismSet = function (a) { return (0, exports.prismModify)(function () { return a; }); };
170exports.prismSet = prismSet;
171/** @internal */
172var prismComposeLens = function (ab) { return function (sa) {
173 return (0, exports.optionalComposeOptional)((0, exports.lensAsOptional)(ab))((0, exports.prismAsOptional)(sa));
174}; };
175exports.prismComposeLens = prismComposeLens;
176/** @internal */
177var prismFromNullable = function () { return (0, exports.prism)(O.fromNullable, function_1.identity); };
178exports.prismFromNullable = prismFromNullable;
179/** @internal */
180var prismFromPredicate = function (predicate) {
181 return (0, exports.prism)(O.fromPredicate(predicate), function_1.identity);
182};
183exports.prismFromPredicate = prismFromPredicate;
184/** @internal */
185var prismSome = function () { return (0, exports.prism)(function_1.identity, O.some); };
186exports.prismSome = prismSome;
187/** @internal */
188var prismRight = function () { return (0, exports.prism)(O.fromEither, E.right); };
189exports.prismRight = prismRight;
190/** @internal */
191var prismLeft = function () {
192 return (0, exports.prism)(function (s) { return (E.isLeft(s) ? O.some(s.left) : O.none); }, // TODO: replace with E.getLeft in v3
193 E.left);
194};
195exports.prismLeft = prismLeft;
196// -------------------------------------------------------------------------------------
197// Optional
198// -------------------------------------------------------------------------------------
199/** @internal */
200var optional = function (getOption, set) { return ({
201 getOption: getOption,
202 set: set
203}); };
204exports.optional = optional;
205/** @internal */
206var 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};
211exports.optionalAsTraversal = optionalAsTraversal;
212/** @internal */
213var 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}; }; };
219exports.optionalModifyOption = optionalModifyOption;
220/** @internal */
221var 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}; };
227exports.optionalModify = optionalModify;
228/** @internal */
229var 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}; };
232exports.optionalComposeOptional = optionalComposeOptional;
233/** @internal */
234var optionalIndex = function (i) { return function (sa) {
235 return (0, pipeable_1.pipe)(sa, (0, exports.optionalComposeOptional)((0, exports.indexReadonlyArray)().index(i)));
236}; };
237exports.optionalIndex = optionalIndex;
238/** @internal */
239var optionalIndexNonEmpty = function (i) { return function (sa) { return (0, pipeable_1.pipe)(sa, (0, exports.optionalComposeOptional)((0, exports.indexReadonlyNonEmptyArray)().index(i))); }; };
240exports.optionalIndexNonEmpty = optionalIndexNonEmpty;
241/** @internal */
242var optionalKey = function (key) { return function (sa) {
243 return (0, pipeable_1.pipe)(sa, (0, exports.optionalComposeOptional)((0, exports.indexReadonlyRecord)().index(key)));
244}; };
245exports.optionalKey = optionalKey;
246/** @internal */
247var 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};
252exports.optionalFindFirst = optionalFindFirst;
253var 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/** @internal */
264var 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};
269exports.optionalFindFirstNonEmpty = optionalFindFirstNonEmpty;
270// -------------------------------------------------------------------------------------
271// Traversal
272// -------------------------------------------------------------------------------------
273/** @internal */
274var traversal = function (modifyF) { return ({
275 modifyF: modifyF
276}); };
277exports.traversal = traversal;
278/** @internal */
279function traversalComposeTraversal(ab) {
280 return function (sa) { return (0, exports.traversal)(function (F) { return function (f) { return sa.modifyF(F)(ab.modifyF(F)(f)); }; }); };
281}
282exports.traversalComposeTraversal = traversalComposeTraversal;
283/** @internal */
284exports.ApplicativeIdentity = {
285 URI: 'Identity',
286 map: function (fa, f) { return f(fa); },
287 of: function_1.identity,
288 ap:
289 /* istanbul ignore next */
290 function (fab, fa) { return fab(fa); }
291};
292var isIdentity = function (F) { return F.URI === 'Identity'; };
293function fromTraversable(T) {
294 return function () {
295 return (0, exports.traversal)(function (F) {
296 // if `F` is `Identity` then `traverseF = map`
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}
304exports.fromTraversable = fromTraversable;
305/** @internal */
306function traversalTraverse(T) {
307 return traversalComposeTraversal(fromTraversable(T)());
308}
309exports.traversalTraverse = traversalTraverse;
310// -------------------------------------------------------------------------------------
311// Ix
312// -------------------------------------------------------------------------------------
313/** @internal */
314var index = function (index) { return ({ index: index }); };
315exports.index = index;
316/** @internal */
317var 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};
324exports.indexReadonlyArray = indexReadonlyArray;
325/** @internal */
326var 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};
333exports.indexReadonlyNonEmptyArray = indexReadonlyNonEmptyArray;
334/** @internal */
335var 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};
345exports.indexReadonlyRecord = indexReadonlyRecord;
346// -------------------------------------------------------------------------------------
347// At
348// -------------------------------------------------------------------------------------
349/** @internal */
350var at = function (at) { return ({ at: at }); };
351exports.at = at;
352/** @internal */
353function 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}
358exports.atReadonlyRecord = atReadonlyRecord;