1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports["default"] = void 0;
|
---|
5 | var _ramda = require("ramda");
|
---|
6 | // This implementation was highly inspired by the implementations
|
---|
7 | // in ramda-lens library.
|
---|
8 | //
|
---|
9 | // https://github.com/ramda/ramda-lens
|
---|
10 |
|
---|
11 | // isomorphic :: ((a -> b), (b -> a)) -> Isomorphism
|
---|
12 | // Isomorphism = x -> y
|
---|
13 | var isomorphic = function isomorphic(to, from) {
|
---|
14 | var isomorphism = function isomorphism(x) {
|
---|
15 | return to(x);
|
---|
16 | };
|
---|
17 | isomorphism.from = from;
|
---|
18 | return isomorphism;
|
---|
19 | };
|
---|
20 |
|
---|
21 | // isomorphisms :: ((a -> b), (b -> a)) -> (a -> b)
|
---|
22 | var isomorphisms = function isomorphisms(to, from) {
|
---|
23 | return isomorphic((0, _ramda.curry)(function (toFunctorFn, target) {
|
---|
24 | return (0, _ramda.map)(from, toFunctorFn(to(target)));
|
---|
25 | }), (0, _ramda.curry)(function (toFunctorFn, target) {
|
---|
26 | return (0, _ramda.map)(to, toFunctorFn(from(target)));
|
---|
27 | }));
|
---|
28 | };
|
---|
29 |
|
---|
30 | // from :: Isomorphism -> a -> b
|
---|
31 | var from = (0, _ramda.curry)(function (isomorphism, x) {
|
---|
32 | return isomorphism.from(x);
|
---|
33 | });
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Defines an isomorphism that will work like a lens. It takes two functions.
|
---|
37 | * The function that converts and the function that recovers.
|
---|
38 | *
|
---|
39 | * @func lensIso
|
---|
40 | * @memberOf RA
|
---|
41 | * @since {@link https://char0n.github.io/ramda-adjunct/1.19.0|1.19.0}
|
---|
42 | * @category Relation
|
---|
43 | * @typedef Lens s a = Functor f => (a -> f a) -> s -> f s
|
---|
44 | * @sig (s -> a) -> (a -> s) -> Lens s a
|
---|
45 | * @param {!function} to The function that converts
|
---|
46 | * @param {!function} from The function that recovers
|
---|
47 | * @return {!function} The isomorphic lens
|
---|
48 | * @see {@link http://ramdajs.com/docs/#lens|R.lens}
|
---|
49 | *
|
---|
50 | * @example
|
---|
51 | *
|
---|
52 | * const lensJSON = RA.lensIso(JSON.parse, JSON.stringify);
|
---|
53 | *
|
---|
54 | * R.over(lensJSON, assoc('b', 2), '{"a":1}'); //=> '{"a":1,"b":2}'
|
---|
55 | * R.over(RA.lensIso.from(lensJSON), R.replace('}', ',"b":2}'), { a: 1 }); // => { a: 1, b: 2 }
|
---|
56 | */
|
---|
57 | var lensIso = (0, _ramda.curry)(isomorphisms);
|
---|
58 | lensIso.from = from;
|
---|
59 | var _default = lensIso;
|
---|
60 | exports["default"] = _default; |
---|