immutable-path-resolver
Version:
Dynamically resolve paths through ImmutableJS data structures
18 lines • 518 B
JavaScript
module.exports = function resolve (state, pathSpec, path=[]) {
if (!state.hasIn(path)) {
return null;
} else if (!pathSpec.length) {
return path;
} else {
if (typeof pathSpec[0] === 'function') {
const value = pathSpec.shift()(state.getIn(path));
if (!~value) {
return null;
}
path.push(value);
} else {
path.push(pathSpec.shift());
}
return resolve(state, pathSpec, path);
}
};