1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.fromFoldable = exports.fromTraversable = exports.Setter = exports.Fold = exports.Getter = exports.Index = exports.At = exports.Traversal = exports.Optional = exports.Prism = exports.Lens = exports.Iso = exports.traversal = exports.optional = exports.prism = exports.lens = exports.index = exports.iso = exports.at = void 0;
|
4 |
|
5 |
|
6 |
|
7 | var A = require("fp-ts/lib/Array");
|
8 | var Const_1 = require("fp-ts/lib/Const");
|
9 | var function_1 = require("fp-ts/lib/function");
|
10 | var Monoid_1 = require("fp-ts/lib/Monoid");
|
11 | var Option_1 = require("fp-ts/lib/Option");
|
12 | var pipeable_1 = require("fp-ts/lib/pipeable");
|
13 | var at = require("./At");
|
14 | exports.at = at;
|
15 | var iso = require("./Iso");
|
16 | exports.iso = iso;
|
17 | var index = require("./Ix");
|
18 | exports.index = index;
|
19 | var lens = require("./Lens");
|
20 | exports.lens = lens;
|
21 | var optional = require("./Optional");
|
22 | exports.optional = optional;
|
23 | var prism = require("./Prism");
|
24 | exports.prism = prism;
|
25 | var traversal = require("./Traversal");
|
26 | exports.traversal = traversal;
|
27 |
|
28 |
|
29 |
|
30 | var fromIso = function (iso) { return new Iso(iso.get, iso.reverseGet); };
|
31 | var fromLens = function (lens) { return new Lens(lens.get, lens.set); };
|
32 | var fromPrism = function (prism) { return new Prism(prism.getOption, prism.reverseGet); };
|
33 | var fromOptional = function (optional) {
|
34 | return new Optional(optional.getOption, optional.set);
|
35 | };
|
36 | var fromTraversal = function (traversal) { return new Traversal(traversal.modifyF); };
|
37 | var fromAt = function (at) { return new At(function (i) { return fromLens(at.at(i)); }); };
|
38 | var fromIndex = function (ix) { return new Index(function (i) { return fromOptional(ix.index(i)); }); };
|
39 |
|
40 |
|
41 |
|
42 | var update = function (o, k, a) {
|
43 | var _a;
|
44 | return a === o[k] ? o : Object.assign({}, o, (_a = {}, _a[k] = a, _a));
|
45 | };
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | var Iso = (function () {
|
55 | function Iso(get, reverseGet) {
|
56 | this.get = get;
|
57 | this.reverseGet = reverseGet;
|
58 | /**
|
59 | * @since 1.0.0
|
60 | */
|
61 | this._tag = 'Iso';
|
62 | /**
|
63 | * @since 1.0.0
|
64 | */
|
65 | this.unwrap = this.get;
|
66 | /**
|
67 | * @since 1.0.0
|
68 | */
|
69 | this.to = this.get;
|
70 | /**
|
71 | * @since 1.0.0
|
72 | */
|
73 | this.wrap = this.reverseGet;
|
74 | /**
|
75 | * @since 1.0.0
|
76 | */
|
77 | this.from = this.reverseGet;
|
78 | }
|
79 | /**
|
80 | * reverse the `Iso`: the source becomes the target and the target becomes the source
|
81 | * @since 1.0.0
|
82 | */
|
83 | Iso.prototype.reverse = function () {
|
84 | return fromIso(iso.reverse(this));
|
85 | };
|
86 | |
87 |
|
88 |
|
89 | Iso.prototype.modify = function (f) {
|
90 | return iso.modify(f)(this);
|
91 | };
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 | Iso.prototype.asLens = function () {
|
98 | return fromLens(iso.asLens(this));
|
99 | };
|
100 | |
101 |
|
102 |
|
103 |
|
104 |
|
105 | Iso.prototype.asPrism = function () {
|
106 | return fromPrism(iso.asPrism(this));
|
107 | };
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 | Iso.prototype.asOptional = function () {
|
114 | return fromOptional(iso.asOptional(this));
|
115 | };
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 | Iso.prototype.asTraversal = function () {
|
122 | return fromTraversal(iso.asTraversal(this));
|
123 | };
|
124 | |
125 |
|
126 |
|
127 |
|
128 |
|
129 | Iso.prototype.asFold = function () {
|
130 | var _this = this;
|
131 | return new Fold(function () { return function (f) { return function (s) { return f(_this.get(s)); }; }; });
|
132 | };
|
133 | |
134 |
|
135 |
|
136 |
|
137 |
|
138 | Iso.prototype.asGetter = function () {
|
139 | var _this = this;
|
140 | return new Getter(function (s) { return _this.get(s); });
|
141 | };
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 | Iso.prototype.asSetter = function () {
|
148 | var _this = this;
|
149 | return new Setter(function (f) { return _this.modify(f); });
|
150 | };
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 | Iso.prototype.compose = function (ab) {
|
157 | return fromIso(iso.compose(ab)(this));
|
158 | };
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 | Iso.prototype.composeIso = function (ab) {
|
165 | return this.compose(ab);
|
166 | };
|
167 | |
168 |
|
169 |
|
170 |
|
171 |
|
172 | Iso.prototype.composeLens = function (ab) {
|
173 | return fromLens((0, pipeable_1.pipe)(this, iso.asLens, lens.compose(ab)));
|
174 | };
|
175 | |
176 |
|
177 |
|
178 |
|
179 |
|
180 | Iso.prototype.composePrism = function (ab) {
|
181 | return fromPrism((0, pipeable_1.pipe)(this, iso.asPrism, prism.compose(ab)));
|
182 | };
|
183 | |
184 |
|
185 |
|
186 |
|
187 |
|
188 | Iso.prototype.composeOptional = function (ab) {
|
189 | return fromOptional((0, pipeable_1.pipe)(this, iso.asOptional, optional.compose(ab)));
|
190 | };
|
191 | |
192 |
|
193 |
|
194 |
|
195 |
|
196 | Iso.prototype.composeTraversal = function (ab) {
|
197 | return fromTraversal((0, pipeable_1.pipe)(this, iso.asTraversal, traversal.compose(ab)));
|
198 | };
|
199 | |
200 |
|
201 |
|
202 |
|
203 |
|
204 | Iso.prototype.composeFold = function (ab) {
|
205 | return this.asFold().compose(ab);
|
206 | };
|
207 | |
208 |
|
209 |
|
210 |
|
211 |
|
212 | Iso.prototype.composeGetter = function (ab) {
|
213 | return this.asGetter().compose(ab);
|
214 | };
|
215 | |
216 |
|
217 |
|
218 |
|
219 |
|
220 | Iso.prototype.composeSetter = function (ab) {
|
221 | return this.asSetter().compose(ab);
|
222 | };
|
223 | return Iso;
|
224 | }());
|
225 | exports.Iso = Iso;
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | var Lens = (function () {
|
236 | function Lens(get, set) {
|
237 | this.get = get;
|
238 | this.set = set;
|
239 | /**
|
240 | * @since 1.0.0
|
241 | */
|
242 | this._tag = 'Lens';
|
243 | }
|
244 | /**
|
245 | * @example
|
246 | * import { Lens } from 'monocle-ts'
|
247 | *
|
248 | * type Person = {
|
249 | * name: string
|
250 | * age: number
|
251 | * address: {
|
252 | * city: string
|
253 | * }
|
254 | * }
|
255 | *
|
256 | * const city = Lens.fromPath<Person>()(['address', 'city'])
|
257 | *
|
258 | * const person: Person = { name: 'Giulio', age: 43, address: { city: 'Milan' } }
|
259 | *
|
260 | * assert.strictEqual(city.get(person), 'Milan')
|
261 | * assert.deepStrictEqual(city.set('London')(person), { name: 'Giulio', age: 43, address: { city: 'London' } })
|
262 | *
|
263 | * @since 1.0.0
|
264 | */
|
265 | Lens.fromPath = function () {
|
266 | var fromProp = Lens.fromProp();
|
267 | return function (path) {
|
268 | var lens = fromProp(path[0]);
|
269 | return path.slice(1).reduce(function (acc, prop) { return acc.compose(fromProp(prop)); }, lens);
|
270 | };
|
271 | };
|
272 | |
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 | Lens.fromProp = function () {
|
293 | return function (prop) { return fromLens((0, pipeable_1.pipe)(lens.id(), lens.prop(prop))); };
|
294 | };
|
295 | Lens.fromProps = function () {
|
296 | return function (props) { return fromLens((0, pipeable_1.pipe)(lens.id(), lens.props.apply(lens, props))); };
|
297 | };
|
298 | |
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 | Lens.fromNullableProp = function () {
|
325 | return function (k, defaultValue) {
|
326 | return new Lens(function (s) {
|
327 | var osk = (0, Option_1.fromNullable)(s[k]);
|
328 | if ((0, Option_1.isNone)(osk)) {
|
329 | return defaultValue;
|
330 | }
|
331 | else {
|
332 | return osk.value;
|
333 | }
|
334 | }, function (a) { return function (s) { return update(s, k, a); }; });
|
335 | };
|
336 | };
|
337 | |
338 |
|
339 |
|
340 | Lens.prototype.modify = function (f) {
|
341 | return lens.modify(f)(this);
|
342 | };
|
343 | |
344 |
|
345 |
|
346 |
|
347 |
|
348 | Lens.prototype.asOptional = function () {
|
349 | return fromOptional(lens.asOptional(this));
|
350 | };
|
351 | |
352 |
|
353 |
|
354 |
|
355 |
|
356 | Lens.prototype.asTraversal = function () {
|
357 | return fromTraversal(lens.asTraversal(this));
|
358 | };
|
359 | |
360 |
|
361 |
|
362 |
|
363 |
|
364 | Lens.prototype.asSetter = function () {
|
365 | var _this = this;
|
366 | return new Setter(function (f) { return _this.modify(f); });
|
367 | };
|
368 | |
369 |
|
370 |
|
371 |
|
372 |
|
373 | Lens.prototype.asGetter = function () {
|
374 | var _this = this;
|
375 | return new Getter(function (s) { return _this.get(s); });
|
376 | };
|
377 | |
378 |
|
379 |
|
380 |
|
381 |
|
382 | Lens.prototype.asFold = function () {
|
383 | var _this = this;
|
384 | return new Fold(function () { return function (f) { return function (s) { return f(_this.get(s)); }; }; });
|
385 | };
|
386 | |
387 |
|
388 |
|
389 |
|
390 |
|
391 | Lens.prototype.compose = function (ab) {
|
392 | return fromLens(lens.compose(ab)(this));
|
393 | };
|
394 | |
395 |
|
396 |
|
397 |
|
398 |
|
399 | Lens.prototype.composeLens = function (ab) {
|
400 | return this.compose(ab);
|
401 | };
|
402 | |
403 |
|
404 |
|
405 |
|
406 |
|
407 | Lens.prototype.composeGetter = function (ab) {
|
408 | return this.asGetter().compose(ab);
|
409 | };
|
410 | |
411 |
|
412 |
|
413 |
|
414 |
|
415 | Lens.prototype.composeFold = function (ab) {
|
416 | return this.asFold().compose(ab);
|
417 | };
|
418 | |
419 |
|
420 |
|
421 |
|
422 |
|
423 | Lens.prototype.composeOptional = function (ab) {
|
424 | return fromOptional((0, pipeable_1.pipe)(this, lens.asOptional, optional.compose(ab)));
|
425 | };
|
426 | |
427 |
|
428 |
|
429 |
|
430 |
|
431 | Lens.prototype.composeTraversal = function (ab) {
|
432 | return fromTraversal((0, pipeable_1.pipe)(this, lens.asTraversal, traversal.compose(ab)));
|
433 | };
|
434 | |
435 |
|
436 |
|
437 |
|
438 |
|
439 | Lens.prototype.composeSetter = function (ab) {
|
440 | return this.asSetter().compose(ab);
|
441 | };
|
442 | |
443 |
|
444 |
|
445 |
|
446 |
|
447 | Lens.prototype.composeIso = function (ab) {
|
448 | return fromLens((0, pipeable_1.pipe)(this, lens.compose((0, pipeable_1.pipe)(ab, iso.asLens))));
|
449 | };
|
450 | |
451 |
|
452 |
|
453 |
|
454 |
|
455 | Lens.prototype.composePrism = function (ab) {
|
456 | return fromOptional(lens.composePrism(ab)(this));
|
457 | };
|
458 | return Lens;
|
459 | }());
|
460 | exports.Lens = Lens;
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 | var Prism = (function () {
|
470 | function Prism(getOption, reverseGet) {
|
471 | this.getOption = getOption;
|
472 | this.reverseGet = reverseGet;
|
473 | |
474 |
|
475 |
|
476 | this._tag = 'Prism';
|
477 | }
|
478 | Prism.fromPredicate = function (predicate) {
|
479 | return fromPrism(prism.fromPredicate(predicate));
|
480 | };
|
481 | |
482 |
|
483 |
|
484 | Prism.some = function () {
|
485 | return somePrism;
|
486 | };
|
487 | |
488 |
|
489 |
|
490 | Prism.prototype.modify = function (f) {
|
491 | var _this = this;
|
492 | return function (s) {
|
493 | var os = _this.modifyOption(f)(s);
|
494 | if ((0, Option_1.isNone)(os)) {
|
495 | return s;
|
496 | }
|
497 | else {
|
498 | return os.value;
|
499 | }
|
500 | };
|
501 | };
|
502 | |
503 |
|
504 |
|
505 | Prism.prototype.modifyOption = function (f) {
|
506 | var _this = this;
|
507 | return function (s) {
|
508 | return Option_1.option.map(_this.getOption(s), function (v) {
|
509 | var n = f(v);
|
510 | return n === v ? s : _this.reverseGet(n);
|
511 | });
|
512 | };
|
513 | };
|
514 | |
515 |
|
516 |
|
517 |
|
518 |
|
519 | Prism.prototype.set = function (a) {
|
520 | return this.modify(function () { return a; });
|
521 | };
|
522 | |
523 |
|
524 |
|
525 |
|
526 |
|
527 | Prism.prototype.asOptional = function () {
|
528 | return fromOptional(prism.asOptional(this));
|
529 | };
|
530 | |
531 |
|
532 |
|
533 |
|
534 |
|
535 | Prism.prototype.asTraversal = function () {
|
536 | return fromTraversal(prism.asTraversal(this));
|
537 | };
|
538 | |
539 |
|
540 |
|
541 |
|
542 |
|
543 | Prism.prototype.asSetter = function () {
|
544 | var _this = this;
|
545 | return new Setter(function (f) { return _this.modify(f); });
|
546 | };
|
547 | |
548 |
|
549 |
|
550 |
|
551 |
|
552 | Prism.prototype.asFold = function () {
|
553 | var _this = this;
|
554 | return new Fold(function (M) { return function (f) { return function (s) {
|
555 | var oa = _this.getOption(s);
|
556 | return (0, Option_1.isNone)(oa) ? M.empty : f(oa.value);
|
557 | }; }; });
|
558 | };
|
559 | |
560 |
|
561 |
|
562 |
|
563 |
|
564 | Prism.prototype.compose = function (ab) {
|
565 | return fromPrism(prism.compose(ab)(this));
|
566 | };
|
567 | |
568 |
|
569 |
|
570 |
|
571 |
|
572 | Prism.prototype.composePrism = function (ab) {
|
573 | return this.compose(ab);
|
574 | };
|
575 | |
576 |
|
577 |
|
578 |
|
579 |
|
580 | Prism.prototype.composeOptional = function (ab) {
|
581 | return fromOptional((0, pipeable_1.pipe)(this, prism.asOptional, optional.compose(ab)));
|
582 | };
|
583 | |
584 |
|
585 |
|
586 |
|
587 |
|
588 | Prism.prototype.composeTraversal = function (ab) {
|
589 | return fromTraversal((0, pipeable_1.pipe)(this, prism.asTraversal, traversal.compose(ab)));
|
590 | };
|
591 | |
592 |
|
593 |
|
594 |
|
595 |
|
596 | Prism.prototype.composeFold = function (ab) {
|
597 | return this.asFold().compose(ab);
|
598 | };
|
599 | |
600 |
|
601 |
|
602 |
|
603 |
|
604 | Prism.prototype.composeSetter = function (ab) {
|
605 | return this.asSetter().compose(ab);
|
606 | };
|
607 | |
608 |
|
609 |
|
610 |
|
611 |
|
612 | Prism.prototype.composeIso = function (ab) {
|
613 | return fromPrism((0, pipeable_1.pipe)(this, prism.compose((0, pipeable_1.pipe)(ab, iso.asPrism))));
|
614 | };
|
615 | |
616 |
|
617 |
|
618 |
|
619 |
|
620 | Prism.prototype.composeLens = function (ab) {
|
621 | return fromOptional(prism.composeLens(ab)(this));
|
622 | };
|
623 | |
624 |
|
625 |
|
626 |
|
627 |
|
628 | Prism.prototype.composeGetter = function (ab) {
|
629 | return this.asFold().compose(ab.asFold());
|
630 | };
|
631 | return Prism;
|
632 | }());
|
633 | exports.Prism = Prism;
|
634 | var somePrism =
|
635 |
|
636 | new Prism(function_1.identity, Option_1.some);
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 |
|
644 |
|
645 |
|
646 | var Optional = (function () {
|
647 | function Optional(getOption, set) {
|
648 | this.getOption = getOption;
|
649 | this.set = set;
|
650 | /**
|
651 | * @since 1.0.0
|
652 | */
|
653 | this._tag = 'Optional';
|
654 | }
|
655 | /**
|
656 | * Returns an `Optional` from a nullable (`A | null | undefined`) prop
|
657 | *
|
658 | * @example
|
659 | * import { Optional } from 'monocle-ts'
|
660 | *
|
661 | * interface Phone {
|
662 | * number: string
|
663 | * }
|
664 | * interface Employment {
|
665 | * phone?: Phone
|
666 | * }
|
667 | * interface Info {
|
668 | * employment?: Employment
|
669 | * }
|
670 | * interface Response {
|
671 | * info?: Info
|
672 | * }
|
673 | *
|
674 | * const numberFromResponse = Optional.fromPath<Response>()(['info', 'employment', 'phone', 'number'])
|
675 | *
|
676 | * const response1: Response = {
|
677 | * info: {
|
678 | * employment: {
|
679 | * phone: {
|
680 | * number: '555-1234'
|
681 | * }
|
682 | * }
|
683 | * }
|
684 | * }
|
685 | * const response2: Response = {
|
686 | * info: {
|
687 | * employment: {}
|
688 | * }
|
689 | * }
|
690 | *
|
691 | * numberFromResponse.getOption(response1)
|
692 | * numberFromResponse.getOption(response2)
|
693 | *
|
694 | * @since 2.1.0
|
695 | */
|
696 | Optional.fromPath = function () {
|
697 | var fromNullableProp = Optional.fromNullableProp();
|
698 | return function (path) {
|
699 | var optional = fromNullableProp(path[0]);
|
700 | return path.slice(1).reduce(function (acc, prop) { return acc.compose(fromNullableProp(prop)); }, optional);
|
701 | };
|
702 | };
|
703 | |
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 | Optional.fromNullableProp = function () {
|
724 | return function (k) {
|
725 | return new Optional(function (s) { return (0, Option_1.fromNullable)(s[k]); }, function (a) { return function (s) { return (s[k] == null ? s : update(s, k, a)); }; });
|
726 | };
|
727 | };
|
728 | |
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 | Optional.fromOptionProp = function () {
|
748 | var formProp = Lens.fromProp();
|
749 | return function (prop) { return formProp(prop).composePrism(somePrism); };
|
750 | };
|
751 | |
752 |
|
753 |
|
754 | Optional.prototype.modify = function (f) {
|
755 | return optional.modify(f)(this);
|
756 | };
|
757 | |
758 |
|
759 |
|
760 | Optional.prototype.modifyOption = function (f) {
|
761 | return optional.modifyOption(f)(this);
|
762 | };
|
763 | |
764 |
|
765 |
|
766 |
|
767 |
|
768 | Optional.prototype.asTraversal = function () {
|
769 | return fromTraversal(optional.asTraversal(this));
|
770 | };
|
771 | |
772 |
|
773 |
|
774 |
|
775 |
|
776 | Optional.prototype.asFold = function () {
|
777 | var _this = this;
|
778 | return new Fold(function (M) { return function (f) { return function (s) {
|
779 | var oa = _this.getOption(s);
|
780 | return (0, Option_1.isNone)(oa) ? M.empty : f(oa.value);
|
781 | }; }; });
|
782 | };
|
783 | |
784 |
|
785 |
|
786 |
|
787 |
|
788 | Optional.prototype.asSetter = function () {
|
789 | var _this = this;
|
790 | return new Setter(function (f) { return _this.modify(f); });
|
791 | };
|
792 | |
793 |
|
794 |
|
795 |
|
796 |
|
797 | Optional.prototype.compose = function (ab) {
|
798 | return fromOptional(optional.compose(ab)(this));
|
799 | };
|
800 | |
801 |
|
802 |
|
803 |
|
804 |
|
805 | Optional.prototype.composeOptional = function (ab) {
|
806 | return this.compose(ab);
|
807 | };
|
808 | |
809 |
|
810 |
|
811 |
|
812 |
|
813 | Optional.prototype.composeTraversal = function (ab) {
|
814 | return fromTraversal((0, pipeable_1.pipe)(this, optional.asTraversal, traversal.compose(ab)));
|
815 | };
|
816 | |
817 |
|
818 |
|
819 |
|
820 |
|
821 | Optional.prototype.composeFold = function (ab) {
|
822 | return this.asFold().compose(ab);
|
823 | };
|
824 | |
825 |
|
826 |
|
827 |
|
828 |
|
829 | Optional.prototype.composeSetter = function (ab) {
|
830 | return this.asSetter().compose(ab);
|
831 | };
|
832 | |
833 |
|
834 |
|
835 |
|
836 |
|
837 | Optional.prototype.composeLens = function (ab) {
|
838 | return fromOptional((0, pipeable_1.pipe)(this, optional.compose((0, pipeable_1.pipe)(ab, lens.asOptional))));
|
839 | };
|
840 | |
841 |
|
842 |
|
843 |
|
844 |
|
845 | Optional.prototype.composePrism = function (ab) {
|
846 | return fromOptional((0, pipeable_1.pipe)(this, optional.compose((0, pipeable_1.pipe)(ab, prism.asOptional))));
|
847 | };
|
848 | |
849 |
|
850 |
|
851 |
|
852 |
|
853 | Optional.prototype.composeIso = function (ab) {
|
854 | return fromOptional((0, pipeable_1.pipe)(this, optional.compose((0, pipeable_1.pipe)(ab, iso.asOptional))));
|
855 | };
|
856 | |
857 |
|
858 |
|
859 |
|
860 |
|
861 | Optional.prototype.composeGetter = function (ab) {
|
862 | return this.asFold().compose(ab.asFold());
|
863 | };
|
864 | return Optional;
|
865 | }());
|
866 | exports.Optional = Optional;
|
867 |
|
868 |
|
869 |
|
870 |
|
871 | var Traversal = (function () {
|
872 | function Traversal(
|
873 | // Van Laarhoven representation
|
874 | modifyF) {
|
875 | this.modifyF = modifyF;
|
876 | |
877 |
|
878 |
|
879 | this._tag = 'Traversal';
|
880 | }
|
881 | |
882 |
|
883 |
|
884 | Traversal.prototype.modify = function (f) {
|
885 | return traversal.modify(f)(this);
|
886 | };
|
887 | |
888 |
|
889 |
|
890 | Traversal.prototype.set = function (a) {
|
891 | return traversal.set(a)(this);
|
892 | };
|
893 | Traversal.prototype.filter = function (predicate) {
|
894 | return fromTraversal(traversal.filter(predicate)(this));
|
895 | };
|
896 | |
897 |
|
898 |
|
899 |
|
900 |
|
901 | Traversal.prototype.asFold = function () {
|
902 | var _this = this;
|
903 | return new Fold(function (M) { return function (f) {
|
904 | return _this.modifyF((0, Const_1.getApplicative)(M))(function (a) { return (0, Const_1.make)(f(a)); });
|
905 | }; });
|
906 | };
|
907 | |
908 |
|
909 |
|
910 |
|
911 |
|
912 | Traversal.prototype.asSetter = function () {
|
913 | var _this = this;
|
914 | return new Setter(function (f) { return _this.modify(f); });
|
915 | };
|
916 | |
917 |
|
918 |
|
919 |
|
920 |
|
921 | Traversal.prototype.compose = function (ab) {
|
922 | return fromTraversal(traversal.compose(ab)(this));
|
923 | };
|
924 | |
925 |
|
926 |
|
927 |
|
928 |
|
929 | Traversal.prototype.composeTraversal = function (ab) {
|
930 | return this.compose(ab);
|
931 | };
|
932 | |
933 |
|
934 |
|
935 |
|
936 |
|
937 | Traversal.prototype.composeFold = function (ab) {
|
938 | return this.asFold().compose(ab);
|
939 | };
|
940 | |
941 |
|
942 |
|
943 |
|
944 |
|
945 | Traversal.prototype.composeSetter = function (ab) {
|
946 | return this.asSetter().compose(ab);
|
947 | };
|
948 | |
949 |
|
950 |
|
951 |
|
952 |
|
953 | Traversal.prototype.composeOptional = function (ab) {
|
954 | return this.compose(ab.asTraversal());
|
955 | };
|
956 | |
957 |
|
958 |
|
959 |
|
960 |
|
961 | Traversal.prototype.composeLens = function (ab) {
|
962 | return fromTraversal((0, pipeable_1.pipe)(this, traversal.compose((0, pipeable_1.pipe)(ab, lens.asTraversal))));
|
963 | };
|
964 | |
965 |
|
966 |
|
967 |
|
968 |
|
969 | Traversal.prototype.composePrism = function (ab) {
|
970 | return fromTraversal((0, pipeable_1.pipe)(this, traversal.compose((0, pipeable_1.pipe)(ab, prism.asTraversal))));
|
971 | };
|
972 | |
973 |
|
974 |
|
975 |
|
976 |
|
977 | Traversal.prototype.composeIso = function (ab) {
|
978 | return fromTraversal((0, pipeable_1.pipe)(this, traversal.compose((0, pipeable_1.pipe)(ab, iso.asTraversal))));
|
979 | };
|
980 | |
981 |
|
982 |
|
983 |
|
984 |
|
985 | Traversal.prototype.composeGetter = function (ab) {
|
986 | return this.asFold().compose(ab.asFold());
|
987 | };
|
988 | return Traversal;
|
989 | }());
|
990 | exports.Traversal = Traversal;
|
991 |
|
992 |
|
993 |
|
994 |
|
995 | var At = (function () {
|
996 | function At(at) {
|
997 | this.at = at;
|
998 | |
999 |
|
1000 |
|
1001 | this._tag = 'At';
|
1002 | }
|
1003 | |
1004 |
|
1005 |
|
1006 |
|
1007 |
|
1008 | At.prototype.fromIso = function (iso) {
|
1009 | return fromAt(at.fromIso(iso)(this));
|
1010 | };
|
1011 | return At;
|
1012 | }());
|
1013 | exports.At = At;
|
1014 |
|
1015 |
|
1016 |
|
1017 |
|
1018 | var Index = (function () {
|
1019 | function Index(index) {
|
1020 | this.index = index;
|
1021 | |
1022 |
|
1023 |
|
1024 | this._tag = 'Index';
|
1025 | }
|
1026 | |
1027 |
|
1028 |
|
1029 | Index.fromAt = function (at) {
|
1030 | return fromIndex(index.fromAt(at));
|
1031 | };
|
1032 | |
1033 |
|
1034 |
|
1035 |
|
1036 |
|
1037 | Index.prototype.fromIso = function (iso) {
|
1038 | return fromIndex(index.fromIso(iso)(this));
|
1039 | };
|
1040 | return Index;
|
1041 | }());
|
1042 | exports.Index = Index;
|
1043 |
|
1044 |
|
1045 |
|
1046 |
|
1047 | var Getter = (function () {
|
1048 | function Getter(get) {
|
1049 | this.get = get;
|
1050 | /**
|
1051 | * @since 1.0.0
|
1052 | */
|
1053 | this._tag = 'Getter';
|
1054 | }
|
1055 | /**
|
1056 | * view a `Getter` as a `Fold`
|
1057 | *
|
1058 | * @since 1.0.0
|
1059 | */
|
1060 | Getter.prototype.asFold = function () {
|
1061 | var _this = this;
|
1062 | return new Fold(function () { return function (f) { return function (s) { return f(_this.get(s)); }; }; });
|
1063 | };
|
1064 | |
1065 |
|
1066 |
|
1067 |
|
1068 |
|
1069 | Getter.prototype.compose = function (ab) {
|
1070 | var _this = this;
|
1071 | return new Getter(function (s) { return ab.get(_this.get(s)); });
|
1072 | };
|
1073 | |
1074 |
|
1075 |
|
1076 |
|
1077 |
|
1078 | Getter.prototype.composeGetter = function (ab) {
|
1079 | return this.compose(ab);
|
1080 | };
|
1081 | |
1082 |
|
1083 |
|
1084 |
|
1085 |
|
1086 | Getter.prototype.composeFold = function (ab) {
|
1087 | return this.asFold().compose(ab);
|
1088 | };
|
1089 | |
1090 |
|
1091 |
|
1092 |
|
1093 |
|
1094 | Getter.prototype.composeLens = function (ab) {
|
1095 | return this.compose(ab.asGetter());
|
1096 | };
|
1097 | |
1098 |
|
1099 |
|
1100 |
|
1101 |
|
1102 | Getter.prototype.composeIso = function (ab) {
|
1103 | return this.compose(ab.asGetter());
|
1104 | };
|
1105 | |
1106 |
|
1107 |
|
1108 |
|
1109 |
|
1110 | Getter.prototype.composeTraversal = function (ab) {
|
1111 | return this.asFold().compose(ab.asFold());
|
1112 | };
|
1113 | |
1114 |
|
1115 |
|
1116 |
|
1117 |
|
1118 | Getter.prototype.composeOptional = function (ab) {
|
1119 | return this.asFold().compose(ab.asFold());
|
1120 | };
|
1121 | |
1122 |
|
1123 |
|
1124 |
|
1125 |
|
1126 | Getter.prototype.composePrism = function (ab) {
|
1127 | return this.asFold().compose(ab.asFold());
|
1128 | };
|
1129 | return Getter;
|
1130 | }());
|
1131 | exports.Getter = Getter;
|
1132 |
|
1133 |
|
1134 |
|
1135 |
|
1136 | var Fold = (function () {
|
1137 | function Fold(foldMap) {
|
1138 | this.foldMap = foldMap;
|
1139 | |
1140 |
|
1141 |
|
1142 | this._tag = 'Fold';
|
1143 | this.getAll = foldMap(A.getMonoid())(A.of);
|
1144 | this.exist = foldMap(Monoid_1.monoidAny);
|
1145 | this.all = foldMap(Monoid_1.monoidAll);
|
1146 | this.foldMapFirst = foldMap((0, Option_1.getFirstMonoid)());
|
1147 | }
|
1148 | |
1149 |
|
1150 |
|
1151 |
|
1152 |
|
1153 | Fold.prototype.compose = function (ab) {
|
1154 | var _this = this;
|
1155 | return new Fold(function (M) { return function (f) { return _this.foldMap(M)(ab.foldMap(M)(f)); }; });
|
1156 | };
|
1157 | |
1158 |
|
1159 |
|
1160 |
|
1161 |
|
1162 | Fold.prototype.composeFold = function (ab) {
|
1163 | return this.compose(ab);
|
1164 | };
|
1165 | |
1166 |
|
1167 |
|
1168 |
|
1169 |
|
1170 | Fold.prototype.composeGetter = function (ab) {
|
1171 | return this.compose(ab.asFold());
|
1172 | };
|
1173 | |
1174 |
|
1175 |
|
1176 |
|
1177 |
|
1178 | Fold.prototype.composeTraversal = function (ab) {
|
1179 | return this.compose(ab.asFold());
|
1180 | };
|
1181 | |
1182 |
|
1183 |
|
1184 |
|
1185 |
|
1186 | Fold.prototype.composeOptional = function (ab) {
|
1187 | return this.compose(ab.asFold());
|
1188 | };
|
1189 | |
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 | Fold.prototype.composeLens = function (ab) {
|
1195 | return this.compose(ab.asFold());
|
1196 | };
|
1197 | |
1198 |
|
1199 |
|
1200 |
|
1201 |
|
1202 | Fold.prototype.composePrism = function (ab) {
|
1203 | return this.compose(ab.asFold());
|
1204 | };
|
1205 | |
1206 |
|
1207 |
|
1208 |
|
1209 |
|
1210 | Fold.prototype.composeIso = function (ab) {
|
1211 | return this.compose(ab.asFold());
|
1212 | };
|
1213 | Fold.prototype.find = function (p) {
|
1214 | return this.foldMapFirst((0, Option_1.fromPredicate)(p));
|
1215 | };
|
1216 | |
1217 |
|
1218 |
|
1219 |
|
1220 |
|
1221 | Fold.prototype.headOption = function (s) {
|
1222 | return this.find(function () { return true; })(s);
|
1223 | };
|
1224 | return Fold;
|
1225 | }());
|
1226 | exports.Fold = Fold;
|
1227 |
|
1228 |
|
1229 |
|
1230 |
|
1231 | var Setter = (function () {
|
1232 | function Setter(modify) {
|
1233 | this.modify = modify;
|
1234 | |
1235 |
|
1236 |
|
1237 | this._tag = 'Setter';
|
1238 | }
|
1239 | |
1240 |
|
1241 |
|
1242 | Setter.prototype.set = function (a) {
|
1243 | return this.modify((0, function_1.constant)(a));
|
1244 | };
|
1245 | |
1246 |
|
1247 |
|
1248 |
|
1249 |
|
1250 | Setter.prototype.compose = function (ab) {
|
1251 | var _this = this;
|
1252 | return new Setter(function (f) { return _this.modify(ab.modify(f)); });
|
1253 | };
|
1254 | |
1255 |
|
1256 |
|
1257 |
|
1258 |
|
1259 | Setter.prototype.composeSetter = function (ab) {
|
1260 | return this.compose(ab);
|
1261 | };
|
1262 | |
1263 |
|
1264 |
|
1265 |
|
1266 |
|
1267 | Setter.prototype.composeTraversal = function (ab) {
|
1268 | return this.compose(ab.asSetter());
|
1269 | };
|
1270 | |
1271 |
|
1272 |
|
1273 |
|
1274 |
|
1275 | Setter.prototype.composeOptional = function (ab) {
|
1276 | return this.compose(ab.asSetter());
|
1277 | };
|
1278 | |
1279 |
|
1280 |
|
1281 |
|
1282 |
|
1283 | Setter.prototype.composeLens = function (ab) {
|
1284 | return this.compose(ab.asSetter());
|
1285 | };
|
1286 | |
1287 |
|
1288 |
|
1289 |
|
1290 |
|
1291 | Setter.prototype.composePrism = function (ab) {
|
1292 | return this.compose(ab.asSetter());
|
1293 | };
|
1294 | |
1295 |
|
1296 |
|
1297 |
|
1298 |
|
1299 | Setter.prototype.composeIso = function (ab) {
|
1300 | return this.compose(ab.asSetter());
|
1301 | };
|
1302 | return Setter;
|
1303 | }());
|
1304 | exports.Setter = Setter;
|
1305 | function fromTraversable(T) {
|
1306 | var f = traversal.fromTraversable(T);
|
1307 | return function () { return fromTraversal(f()); };
|
1308 | }
|
1309 | exports.fromTraversable = fromTraversable;
|
1310 | function fromFoldable(F) {
|
1311 | return function () {
|
1312 | return new Fold(function (M) {
|
1313 | var foldMapFM = F.foldMap(M);
|
1314 | return function (f) { return function (s) { return foldMapFM(s, f); }; };
|
1315 | });
|
1316 | };
|
1317 | }
|
1318 | exports.fromFoldable = fromFoldable;
|