object-hierarchy-access
Version:
Get/Set value from/to JS object hierarchy properties
32 lines (31 loc) • 1.21 kB
JavaScript
import { getPropNames, cloneContainer } from './utility/common';
import { normalizeDescriptor, getMappedNameValue } from './utility/select';
function generate(current, result, hierarchies, index) {
const descriptor = normalizeDescriptor(hierarchies[index]);
const names = getPropNames(current, descriptor);
const lastIndex = hierarchies.length - 1;
names.forEach(name => {
if (name in current) {
const { mappedName, mappedValue } = getMappedNameValue(current, name, descriptor);
if (index < lastIndex) {
result[mappedName] = cloneContainer(mappedValue);
}
else {
result[mappedName] = mappedValue;
}
if (index < lastIndex && typeof mappedValue === 'object' && mappedValue !== null) {
generate(mappedValue, result[mappedName], hierarchies, index + 1);
}
}
});
}
function select(target, ...hierarchyProps) {
let result;
const current = target;
if (current !== undefined && current !== null) {
result = cloneContainer(current);
generate(current, result, hierarchyProps, 0);
}
return result;
}
export { select };