From c75e05b2fcbd08ec77a2ac85837f9f58829a44f6 Mon Sep 17 00:00:00 2001 From: "bozo.kopic" Date: Sun, 2 Jul 2017 02:45:51 +0200 Subject: build environment and basic folder structure --- src_js/hatter/lenses.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src_js/hatter/lenses.js (limited to 'src_js/hatter/lenses.js') diff --git a/src_js/hatter/lenses.js b/src_js/hatter/lenses.js new file mode 100644 index 0000000..39da314 --- /dev/null +++ b/src_js/hatter/lenses.js @@ -0,0 +1,21 @@ +import R from 'ramda'; + + +export const index = R.lensIndex; + +export const prop = R.lensProp; + +export function path(...xs) { + return R.reduce((acc, i) => R.compose(acc, pathParamToLens(i)), + R.identity, xs); +} + +function pathParamToLens(x) { + switch (typeof(x)) { + case 'function': return x; + case 'number': return index(x); + case 'string': return prop(x); + case 'object': if (Array.isArray(x)) return R.apply(path, x); + } + throw 'Invalid path parameter'; +} -- cgit v1.2.3-70-g09d2