aboutsummaryrefslogtreecommitdiff
path: root/src_js/hatter/lenses.js
blob: 39da3140744285990abf5174caf629bafebe09bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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';
}