UNPKG

34.3 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.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 * @since 1.0.0
6 */
7var A = require("fp-ts/lib/Array");
8var Const_1 = require("fp-ts/lib/Const");
9var function_1 = require("fp-ts/lib/function");
10var Monoid_1 = require("fp-ts/lib/Monoid");
11var Option_1 = require("fp-ts/lib/Option");
12var pipeable_1 = require("fp-ts/lib/pipeable");
13var at = require("./At");
14exports.at = at;
15var iso = require("./Iso");
16exports.iso = iso;
17var index = require("./Ix");
18exports.index = index;
19var lens = require("./Lens");
20exports.lens = lens;
21var optional = require("./Optional");
22exports.optional = optional;
23var prism = require("./Prism");
24exports.prism = prism;
25var traversal = require("./Traversal");
26exports.traversal = traversal;
27//
28// compat
29//
30var fromIso = function (iso) { return new Iso(iso.get, iso.reverseGet); };
31var fromLens = function (lens) { return new Lens(lens.get, lens.set); };
32var fromPrism = function (prism) { return new Prism(prism.getOption, prism.reverseGet); };
33var fromOptional = function (optional) {
34 return new Optional(optional.getOption, optional.set);
35};
36var fromTraversal = function (traversal) { return new Traversal(traversal.modifyF); };
37var fromAt = function (at) { return new At(function (i) { return fromLens(at.at(i)); }); };
38var fromIndex = function (ix) { return new Index(function (i) { return fromOptional(ix.index(i)); }); };
39//
40// old APIs
41//
42var 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 * Laws:
48 * 1. `reverseGet(get(s)) = s`
49 * 2. `get(reversetGet(a)) = a`
50 *
51 * @category constructor
52 * @since 1.0.0
53 */
54var Iso = /** @class */ (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 * @since 1.0.0
88 */
89 Iso.prototype.modify = function (f) {
90 return iso.modify(f)(this);
91 };
92 /**
93 * view an `Iso` as a `Lens`
94 *
95 * @since 1.0.0
96 */
97 Iso.prototype.asLens = function () {
98 return fromLens(iso.asLens(this));
99 };
100 /**
101 * view an `Iso` as a `Prism`
102 *
103 * @since 1.0.0
104 */
105 Iso.prototype.asPrism = function () {
106 return fromPrism(iso.asPrism(this));
107 };
108 /**
109 * view an `Iso` as a `Optional`
110 *
111 * @since 1.0.0
112 */
113 Iso.prototype.asOptional = function () {
114 return fromOptional(iso.asOptional(this));
115 };
116 /**
117 * view an `Iso` as a `Traversal`
118 *
119 * @since 1.0.0
120 */
121 Iso.prototype.asTraversal = function () {
122 return fromTraversal(iso.asTraversal(this));
123 };
124 /**
125 * view an `Iso` as a `Fold`
126 *
127 * @since 1.0.0
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 * view an `Iso` as a `Getter`
135 *
136 * @since 1.0.0
137 */
138 Iso.prototype.asGetter = function () {
139 var _this = this;
140 return new Getter(function (s) { return _this.get(s); });
141 };
142 /**
143 * view an `Iso` as a `Setter`
144 *
145 * @since 1.0.0
146 */
147 Iso.prototype.asSetter = function () {
148 var _this = this;
149 return new Setter(function (f) { return _this.modify(f); });
150 };
151 /**
152 * compose an `Iso` with an `Iso`
153 *
154 * @since 1.0.0
155 */
156 Iso.prototype.compose = function (ab) {
157 return fromIso(iso.compose(ab)(this));
158 };
159 /**
160 * Alias of `compose`
161 *
162 * @since 1.0.0
163 */
164 Iso.prototype.composeIso = function (ab) {
165 return this.compose(ab);
166 };
167 /**
168 * compose an `Iso` with a `Lens `
169 *
170 * @since 1.0.0
171 */
172 Iso.prototype.composeLens = function (ab) {
173 return fromLens((0, pipeable_1.pipe)(this, iso.asLens, lens.compose(ab)));
174 };
175 /**
176 * compose an `Iso` with a `Prism`
177 *
178 * @since 1.0.0
179 */
180 Iso.prototype.composePrism = function (ab) {
181 return fromPrism((0, pipeable_1.pipe)(this, iso.asPrism, prism.compose(ab)));
182 };
183 /**
184 * compose an `Iso` with an `Optional`
185 *
186 * @since 1.0.0
187 */
188 Iso.prototype.composeOptional = function (ab) {
189 return fromOptional((0, pipeable_1.pipe)(this, iso.asOptional, optional.compose(ab)));
190 };
191 /**
192 * compose an `Iso` with a `Traversal`
193 *
194 * @since 1.0.0
195 */
196 Iso.prototype.composeTraversal = function (ab) {
197 return fromTraversal((0, pipeable_1.pipe)(this, iso.asTraversal, traversal.compose(ab)));
198 };
199 /**
200 * compose an `Iso` with a `Fold`
201 *
202 * @since 1.0.0
203 */
204 Iso.prototype.composeFold = function (ab) {
205 return this.asFold().compose(ab);
206 };
207 /**
208 * compose an `Iso` with a `Getter`
209 *
210 * @since 1.0.0
211 */
212 Iso.prototype.composeGetter = function (ab) {
213 return this.asGetter().compose(ab);
214 };
215 /**
216 * compose an `Iso` with a `Setter`
217 *
218 * @since 1.0.0
219 */
220 Iso.prototype.composeSetter = function (ab) {
221 return this.asSetter().compose(ab);
222 };
223 return Iso;
224}());
225exports.Iso = Iso;
226/**
227 * Laws:
228 * 1. `get(set(a)(s)) = a`
229 * 2. `set(get(s))(s) = s`
230 * 3. `set(a)(set(a)(s)) = set(a)(s)`
231 *
232 * @category constructor
233 * @since 1.0.0
234 */
235var Lens = /** @class */ (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 * Returns a `Lens` from a type and a prop
274 *
275 * @example
276 * import { Lens } from 'monocle-ts'
277 *
278 * type Person = {
279 * name: string
280 * age: number
281 * }
282 *
283 * const age = Lens.fromProp<Person>()('age')
284 *
285 * const person: Person = { name: 'Giulio', age: 43 }
286 *
287 * assert.strictEqual(age.get(person), 43)
288 * assert.deepStrictEqual(age.set(44)(person), { name: 'Giulio', age: 44 })
289 *
290 * @since 1.0.0
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 * Returns a `Lens` from a nullable (`A | null | undefined`) prop
300 *
301 * @example
302 * import { Lens } from 'monocle-ts'
303 *
304 * interface Outer {
305 * inner?: Inner
306 * }
307 *
308 * interface Inner {
309 * value: number
310 * foo: string
311 * }
312 *
313 * const inner = Lens.fromNullableProp<Outer>()('inner', { value: 0, foo: 'foo' })
314 * const value = Lens.fromProp<Inner>()('value')
315 * const lens = inner.compose(value)
316 *
317 * assert.deepStrictEqual(lens.set(1)({}), { inner: { value: 1, foo: 'foo' } })
318 * assert.strictEqual(lens.get({}), 0)
319 * assert.deepStrictEqual(lens.set(1)({ inner: { value: 1, foo: 'bar' } }), { inner: { value: 1, foo: 'bar' } })
320 * assert.strictEqual(lens.get({ inner: { value: 1, foo: 'bar' } }), 1)
321 *
322 * @since 1.0.0
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 * @since 1.0.0
339 */
340 Lens.prototype.modify = function (f) {
341 return lens.modify(f)(this);
342 };
343 /**
344 * view a `Lens` as a Optional
345 *
346 * @since 1.0.0
347 */
348 Lens.prototype.asOptional = function () {
349 return fromOptional(lens.asOptional(this));
350 };
351 /**
352 * view a `Lens` as a `Traversal`
353 *
354 * @since 1.0.0
355 */
356 Lens.prototype.asTraversal = function () {
357 return fromTraversal(lens.asTraversal(this));
358 };
359 /**
360 * view a `Lens` as a `Setter`
361 *
362 * @since 1.0.0
363 */
364 Lens.prototype.asSetter = function () {
365 var _this = this;
366 return new Setter(function (f) { return _this.modify(f); });
367 };
368 /**
369 * view a `Lens` as a `Getter`
370 *
371 * @since 1.0.0
372 */
373 Lens.prototype.asGetter = function () {
374 var _this = this;
375 return new Getter(function (s) { return _this.get(s); });
376 };
377 /**
378 * view a `Lens` as a `Fold`
379 *
380 * @since 1.0.0
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 * compose a `Lens` with a `Lens`
388 *
389 * @since 1.0.0
390 */
391 Lens.prototype.compose = function (ab) {
392 return fromLens(lens.compose(ab)(this));
393 };
394 /**
395 * Alias of `compose`
396 *
397 * @since 1.0.0
398 */
399 Lens.prototype.composeLens = function (ab) {
400 return this.compose(ab);
401 };
402 /**
403 * compose a `Lens` with a `Getter`
404 *
405 * @since 1.0.0
406 */
407 Lens.prototype.composeGetter = function (ab) {
408 return this.asGetter().compose(ab);
409 };
410 /**
411 * compose a `Lens` with a `Fold`
412 *
413 * @since 1.0.0
414 */
415 Lens.prototype.composeFold = function (ab) {
416 return this.asFold().compose(ab);
417 };
418 /**
419 * compose a `Lens` with an `Optional`
420 *
421 * @since 1.0.0
422 */
423 Lens.prototype.composeOptional = function (ab) {
424 return fromOptional((0, pipeable_1.pipe)(this, lens.asOptional, optional.compose(ab)));
425 };
426 /**
427 * compose a `Lens` with an `Traversal`
428 *
429 * @since 1.0.0
430 */
431 Lens.prototype.composeTraversal = function (ab) {
432 return fromTraversal((0, pipeable_1.pipe)(this, lens.asTraversal, traversal.compose(ab)));
433 };
434 /**
435 * compose a `Lens` with an `Setter`
436 *
437 * @since 1.0.0
438 */
439 Lens.prototype.composeSetter = function (ab) {
440 return this.asSetter().compose(ab);
441 };
442 /**
443 * compose a `Lens` with an `Iso`
444 *
445 * @since 1.0.0
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 * compose a `Lens` with a `Prism`
452 *
453 * @since 1.0.0
454 */
455 Lens.prototype.composePrism = function (ab) {
456 return fromOptional(lens.composePrism(ab)(this));
457 };
458 return Lens;
459}());
460exports.Lens = Lens;
461/**
462 * Laws:
463 * 1. `pipe(getOption(s), fold(() => s, reverseGet)) = s`
464 * 2. `getOption(reverseGet(a)) = some(a)`
465 *
466 * @category constructor
467 * @since 1.0.0
468 */
469var Prism = /** @class */ (function () {
470 function Prism(getOption, reverseGet) {
471 this.getOption = getOption;
472 this.reverseGet = reverseGet;
473 /**
474 * @since 1.0.0
475 */
476 this._tag = 'Prism';
477 }
478 Prism.fromPredicate = function (predicate) {
479 return fromPrism(prism.fromPredicate(predicate));
480 };
481 /**
482 * @since 1.0.0
483 */
484 Prism.some = function () {
485 return somePrism;
486 };
487 /**
488 * @since 1.0.0
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 * @since 1.0.0
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 * set the target of a `Prism` with a value
516 *
517 * @since 1.0.0
518 */
519 Prism.prototype.set = function (a) {
520 return this.modify(function () { return a; });
521 };
522 /**
523 * view a `Prism` as a `Optional`
524 *
525 * @since 1.0.0
526 */
527 Prism.prototype.asOptional = function () {
528 return fromOptional(prism.asOptional(this));
529 };
530 /**
531 * view a `Prism` as a `Traversal`
532 *
533 * @since 1.0.0
534 */
535 Prism.prototype.asTraversal = function () {
536 return fromTraversal(prism.asTraversal(this));
537 };
538 /**
539 * view a `Prism` as a `Setter`
540 *
541 * @since 1.0.0
542 */
543 Prism.prototype.asSetter = function () {
544 var _this = this;
545 return new Setter(function (f) { return _this.modify(f); });
546 };
547 /**
548 * view a `Prism` as a `Fold`
549 *
550 * @since 1.0.0
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 * compose a `Prism` with a `Prism`
561 *
562 * @since 1.0.0
563 */
564 Prism.prototype.compose = function (ab) {
565 return fromPrism(prism.compose(ab)(this));
566 };
567 /**
568 * Alias of `compose`
569 *
570 * @since 1.0.0
571 */
572 Prism.prototype.composePrism = function (ab) {
573 return this.compose(ab);
574 };
575 /**
576 * compose a `Prism` with a `Optional`
577 *
578 * @since 1.0.0
579 */
580 Prism.prototype.composeOptional = function (ab) {
581 return fromOptional((0, pipeable_1.pipe)(this, prism.asOptional, optional.compose(ab)));
582 };
583 /**
584 * compose a `Prism` with a `Traversal`
585 *
586 * @since 1.0.0
587 */
588 Prism.prototype.composeTraversal = function (ab) {
589 return fromTraversal((0, pipeable_1.pipe)(this, prism.asTraversal, traversal.compose(ab)));
590 };
591 /**
592 * compose a `Prism` with a `Fold`
593 *
594 * @since 1.0.0
595 */
596 Prism.prototype.composeFold = function (ab) {
597 return this.asFold().compose(ab);
598 };
599 /**
600 * compose a `Prism` with a `Setter`
601 *
602 * @since 1.0.0
603 */
604 Prism.prototype.composeSetter = function (ab) {
605 return this.asSetter().compose(ab);
606 };
607 /**
608 * compose a `Prism` with a `Iso`
609 *
610 * @since 1.0.0
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 * compose a `Prism` with a `Lens`
617 *
618 * @since 1.0.0
619 */
620 Prism.prototype.composeLens = function (ab) {
621 return fromOptional(prism.composeLens(ab)(this));
622 };
623 /**
624 * compose a `Prism` with a `Getter`
625 *
626 * @since 1.0.0
627 */
628 Prism.prototype.composeGetter = function (ab) {
629 return this.asFold().compose(ab.asFold());
630 };
631 return Prism;
632}());
633exports.Prism = Prism;
634var somePrism =
635/*#__PURE__*/
636new Prism(function_1.identity, Option_1.some);
637/**
638 * Laws:
639 * 1. `pipe(getOption(s), fold(() => s, a => set(a)(s))) = s`
640 * 2. `getOption(set(a)(s)) = pipe(getOption(s), map(_ => a))`
641 * 3. `set(a)(set(a)(s)) = set(a)(s)`
642 *
643 * @category constructor
644 * @since 1.0.0
645 */
646var Optional = /** @class */ (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) // some('555-1234')
692 * numberFromResponse.getOption(response2) // none
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 * @example
705 * import { Optional } from 'monocle-ts'
706 *
707 * interface S {
708 * a: number | undefined | null
709 * }
710 *
711 * const optional = Optional.fromNullableProp<S>()('a')
712 *
713 * const s1: S = { a: undefined }
714 * const s2: S = { a: null }
715 * const s3: S = { a: 1 }
716 *
717 * assert.deepStrictEqual(optional.set(2)(s1), s1)
718 * assert.deepStrictEqual(optional.set(2)(s2), s2)
719 * assert.deepStrictEqual(optional.set(2)(s3), { a: 2 })
720 *
721 * @since 1.0.0
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 * Returns an `Optional` from an option (`Option<A>`) prop
730 *
731 * @example
732 * import { Optional } from 'monocle-ts'
733 * import * as O from 'fp-ts/Option'
734 *
735 * interface S {
736 * a: O.Option<number>
737 * }
738 *
739 * const optional = Optional.fromOptionProp<S>()('a')
740 * const s1: S = { a: O.none }
741 * const s2: S = { a: O.some(1) }
742 * assert.deepStrictEqual(optional.set(2)(s1), s1)
743 * assert.deepStrictEqual(optional.set(2)(s2), { a: O.some(2) })
744 *
745 * @since 1.0.0
746 */
747 Optional.fromOptionProp = function () {
748 var formProp = Lens.fromProp();
749 return function (prop) { return formProp(prop).composePrism(somePrism); };
750 };
751 /**
752 * @since 1.0.0
753 */
754 Optional.prototype.modify = function (f) {
755 return optional.modify(f)(this);
756 };
757 /**
758 * @since 1.0.0
759 */
760 Optional.prototype.modifyOption = function (f) {
761 return optional.modifyOption(f)(this);
762 };
763 /**
764 * view a `Optional` as a `Traversal`
765 *
766 * @since 1.0.0
767 */
768 Optional.prototype.asTraversal = function () {
769 return fromTraversal(optional.asTraversal(this));
770 };
771 /**
772 * view an `Optional` as a `Fold`
773 *
774 * @since 1.0.0
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 * view an `Optional` as a `Setter`
785 *
786 * @since 1.0.0
787 */
788 Optional.prototype.asSetter = function () {
789 var _this = this;
790 return new Setter(function (f) { return _this.modify(f); });
791 };
792 /**
793 * compose a `Optional` with a `Optional`
794 *
795 * @since 1.0.0
796 */
797 Optional.prototype.compose = function (ab) {
798 return fromOptional(optional.compose(ab)(this));
799 };
800 /**
801 * Alias of `compose`
802 *
803 * @since 1.0.0
804 */
805 Optional.prototype.composeOptional = function (ab) {
806 return this.compose(ab);
807 };
808 /**
809 * compose an `Optional` with a `Traversal`
810 *
811 * @since 1.0.0
812 */
813 Optional.prototype.composeTraversal = function (ab) {
814 return fromTraversal((0, pipeable_1.pipe)(this, optional.asTraversal, traversal.compose(ab)));
815 };
816 /**
817 * compose an `Optional` with a `Fold`
818 *
819 * @since 1.0.0
820 */
821 Optional.prototype.composeFold = function (ab) {
822 return this.asFold().compose(ab);
823 };
824 /**
825 * compose an `Optional` with a `Setter`
826 *
827 * @since 1.0.0
828 */
829 Optional.prototype.composeSetter = function (ab) {
830 return this.asSetter().compose(ab);
831 };
832 /**
833 * compose an `Optional` with a `Lens`
834 *
835 * @since 1.0.0
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 * compose an `Optional` with a `Prism`
842 *
843 * @since 1.0.0
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 * compose an `Optional` with a `Iso`
850 *
851 * @since 1.0.0
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 * compose an `Optional` with a `Getter`
858 *
859 * @since 1.0.0
860 */
861 Optional.prototype.composeGetter = function (ab) {
862 return this.asFold().compose(ab.asFold());
863 };
864 return Optional;
865}());
866exports.Optional = Optional;
867/**
868 * @category constructor
869 * @since 1.0.0
870 */
871var Traversal = /** @class */ (function () {
872 function Traversal(
873 // Van Laarhoven representation
874 modifyF) {
875 this.modifyF = modifyF;
876 /**
877 * @since 1.0.0
878 */
879 this._tag = 'Traversal';
880 }
881 /**
882 * @since 1.0.0
883 */
884 Traversal.prototype.modify = function (f) {
885 return traversal.modify(f)(this);
886 };
887 /**
888 * @since 1.0.0
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 * view a `Traversal` as a `Fold`
898 *
899 * @since 1.0.0
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 * view a `Traversal` as a `Setter`
909 *
910 * @since 1.0.0
911 */
912 Traversal.prototype.asSetter = function () {
913 var _this = this;
914 return new Setter(function (f) { return _this.modify(f); });
915 };
916 /**
917 * compose a `Traversal` with a `Traversal`
918 *
919 * @since 1.0.0
920 */
921 Traversal.prototype.compose = function (ab) {
922 return fromTraversal(traversal.compose(ab)(this));
923 };
924 /**
925 * Alias of `compose`
926 *
927 * @since 1.0.0
928 */
929 Traversal.prototype.composeTraversal = function (ab) {
930 return this.compose(ab);
931 };
932 /**
933 * compose a `Traversal` with a `Fold`
934 *
935 * @since 1.0.0
936 */
937 Traversal.prototype.composeFold = function (ab) {
938 return this.asFold().compose(ab);
939 };
940 /**
941 * compose a `Traversal` with a `Setter`
942 *
943 * @since 1.0.0
944 */
945 Traversal.prototype.composeSetter = function (ab) {
946 return this.asSetter().compose(ab);
947 };
948 /**
949 * compose a `Traversal` with a `Optional`
950 *
951 * @since 1.0.0
952 */
953 Traversal.prototype.composeOptional = function (ab) {
954 return this.compose(ab.asTraversal());
955 };
956 /**
957 * compose a `Traversal` with a `Lens`
958 *
959 * @since 1.0.0
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 * compose a `Traversal` with a `Prism`
966 *
967 * @since 1.0.0
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 * compose a `Traversal` with a `Iso`
974 *
975 * @since 1.0.0
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 * compose a `Traversal` with a `Getter`
982 *
983 * @since 1.0.0
984 */
985 Traversal.prototype.composeGetter = function (ab) {
986 return this.asFold().compose(ab.asFold());
987 };
988 return Traversal;
989}());
990exports.Traversal = Traversal;
991/**
992 * @category constructor
993 * @since 1.2.0
994 */
995var At = /** @class */ (function () {
996 function At(at) {
997 this.at = at;
998 /**
999 * @since 1.0.0
1000 */
1001 this._tag = 'At';
1002 }
1003 /**
1004 * lift an instance of `At` using an `Iso`
1005 *
1006 * @since 1.2.0
1007 */
1008 At.prototype.fromIso = function (iso) {
1009 return fromAt(at.fromIso(iso)(this));
1010 };
1011 return At;
1012}());
1013exports.At = At;
1014/**
1015 * @category constructor
1016 * @since 1.2.0
1017 */
1018var Index = /** @class */ (function () {
1019 function Index(index) {
1020 this.index = index;
1021 /**
1022 * @since 1.0.0
1023 */
1024 this._tag = 'Index';
1025 }
1026 /**
1027 * @since 1.2.0
1028 */
1029 Index.fromAt = function (at) {
1030 return fromIndex(index.fromAt(at));
1031 };
1032 /**
1033 * lift an instance of `Index` using an `Iso`
1034 *
1035 * @since 1.2.0
1036 */
1037 Index.prototype.fromIso = function (iso) {
1038 return fromIndex(index.fromIso(iso)(this));
1039 };
1040 return Index;
1041}());
1042exports.Index = Index;
1043/**
1044 * @category constructor
1045 * @since 1.0.0
1046 */
1047var Getter = /** @class */ (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 * compose a `Getter` with a `Getter`
1066 *
1067 * @since 1.0.0
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 * Alias of `compose`
1075 *
1076 * @since 1.0.0
1077 */
1078 Getter.prototype.composeGetter = function (ab) {
1079 return this.compose(ab);
1080 };
1081 /**
1082 * compose a `Getter` with a `Fold`
1083 *
1084 * @since 1.0.0
1085 */
1086 Getter.prototype.composeFold = function (ab) {
1087 return this.asFold().compose(ab);
1088 };
1089 /**
1090 * compose a `Getter` with a `Lens`
1091 *
1092 * @since 1.0.0
1093 */
1094 Getter.prototype.composeLens = function (ab) {
1095 return this.compose(ab.asGetter());
1096 };
1097 /**
1098 * compose a `Getter` with a `Iso`
1099 *
1100 * @since 1.0.0
1101 */
1102 Getter.prototype.composeIso = function (ab) {
1103 return this.compose(ab.asGetter());
1104 };
1105 /**
1106 * compose a `Getter` with a `Optional`
1107 *
1108 * @since 1.0.0
1109 */
1110 Getter.prototype.composeTraversal = function (ab) {
1111 return this.asFold().compose(ab.asFold());
1112 };
1113 /**
1114 * compose a `Getter` with a `Optional`
1115 *
1116 * @since 1.0.0
1117 */
1118 Getter.prototype.composeOptional = function (ab) {
1119 return this.asFold().compose(ab.asFold());
1120 };
1121 /**
1122 * compose a `Getter` with a `Prism`
1123 *
1124 * @since 1.0.0
1125 */
1126 Getter.prototype.composePrism = function (ab) {
1127 return this.asFold().compose(ab.asFold());
1128 };
1129 return Getter;
1130}());
1131exports.Getter = Getter;
1132/**
1133 * @category constructor
1134 * @since 1.0.0
1135 */
1136var Fold = /** @class */ (function () {
1137 function Fold(foldMap) {
1138 this.foldMap = foldMap;
1139 /**
1140 * @since 1.0.0
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 * compose a `Fold` with a `Fold`
1150 *
1151 * @since 1.0.0
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 * Alias of `compose`
1159 *
1160 * @since 1.0.0
1161 */
1162 Fold.prototype.composeFold = function (ab) {
1163 return this.compose(ab);
1164 };
1165 /**
1166 * compose a `Fold` with a `Getter`
1167 *
1168 * @since 1.0.0
1169 */
1170 Fold.prototype.composeGetter = function (ab) {
1171 return this.compose(ab.asFold());
1172 };
1173 /**
1174 * compose a `Fold` with a `Traversal`
1175 *
1176 * @since 1.0.0
1177 */
1178 Fold.prototype.composeTraversal = function (ab) {
1179 return this.compose(ab.asFold());
1180 };
1181 /**
1182 * compose a `Fold` with a `Optional`
1183 *
1184 * @since 1.0.0
1185 */
1186 Fold.prototype.composeOptional = function (ab) {
1187 return this.compose(ab.asFold());
1188 };
1189 /**
1190 * compose a `Fold` with a `Lens`
1191 *
1192 * @since 1.0.0
1193 */
1194 Fold.prototype.composeLens = function (ab) {
1195 return this.compose(ab.asFold());
1196 };
1197 /**
1198 * compose a `Fold` with a `Prism`
1199 *
1200 * @since 1.0.0
1201 */
1202 Fold.prototype.composePrism = function (ab) {
1203 return this.compose(ab.asFold());
1204 };
1205 /**
1206 * compose a `Fold` with a `Iso`
1207 *
1208 * @since 1.0.0
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 * get the first target of a `Fold`
1218 *
1219 * @since 1.0.0
1220 */
1221 Fold.prototype.headOption = function (s) {
1222 return this.find(function () { return true; })(s);
1223 };
1224 return Fold;
1225}());
1226exports.Fold = Fold;
1227/**
1228 * @category constructor
1229 * @since 1.0.0
1230 */
1231var Setter = /** @class */ (function () {
1232 function Setter(modify) {
1233 this.modify = modify;
1234 /**
1235 * @since 1.0.0
1236 */
1237 this._tag = 'Setter';
1238 }
1239 /**
1240 * @since 1.0.0
1241 */
1242 Setter.prototype.set = function (a) {
1243 return this.modify((0, function_1.constant)(a));
1244 };
1245 /**
1246 * compose a `Setter` with a `Setter`
1247 *
1248 * @since 1.0.0
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 * Alias of `compose`
1256 *
1257 * @since 1.0.0
1258 */
1259 Setter.prototype.composeSetter = function (ab) {
1260 return this.compose(ab);
1261 };
1262 /**
1263 * compose a `Setter` with a `Traversal`
1264 *
1265 * @since 1.0.0
1266 */
1267 Setter.prototype.composeTraversal = function (ab) {
1268 return this.compose(ab.asSetter());
1269 };
1270 /**
1271 * compose a `Setter` with a `Optional`
1272 *
1273 * @since 1.0.0
1274 */
1275 Setter.prototype.composeOptional = function (ab) {
1276 return this.compose(ab.asSetter());
1277 };
1278 /**
1279 * compose a `Setter` with a `Lens`
1280 *
1281 * @since 1.0.0
1282 */
1283 Setter.prototype.composeLens = function (ab) {
1284 return this.compose(ab.asSetter());
1285 };
1286 /**
1287 * compose a `Setter` with a `Prism`
1288 *
1289 * @since 1.0.0
1290 */
1291 Setter.prototype.composePrism = function (ab) {
1292 return this.compose(ab.asSetter());
1293 };
1294 /**
1295 * compose a `Setter` with a `Iso`
1296 *
1297 * @since 1.0.0
1298 */
1299 Setter.prototype.composeIso = function (ab) {
1300 return this.compose(ab.asSetter());
1301 };
1302 return Setter;
1303}());
1304exports.Setter = Setter;
1305function fromTraversable(T) {
1306 var f = traversal.fromTraversable(T);
1307 return function () { return fromTraversal(f()); };
1308}
1309exports.fromTraversable = fromTraversable;
1310function 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}
1318exports.fromFoldable = fromFoldable;