diff options
Diffstat (limited to 'src_js/hatter/lenses.js')
| -rw-r--r-- | src_js/hatter/lenses.js | 21 |
1 files changed, 21 insertions, 0 deletions
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'; +} |
