source: node_modules/ramda-adjunct/es/lensIso.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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