UNPKG

object-hierarchy-access

Version:

Get/Set value from/to JS object hierarchy properties

28 lines (27 loc) 937 B
import { getPropNames } from './utility/common'; import { normalizeDescriptor, getMappedNameValue } from './utility/select'; function find(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 { mappedValue } = getMappedNameValue(current, name, descriptor); if (index < lastIndex) { find(mappedValue, result, hierarchies, index + 1); } else { result.push(mappedValue); } } }); } function pick(target, ...hierarchyProps) { const result = []; const current = target; if (current !== undefined && current !== null) { find(current, result, hierarchyProps, 0); } return result; } export { pick };