froebel
Version:
TypeScript utility library
18 lines (16 loc) • 708 B
JavaScript
export const none = Symbol("value.none");
/**
* Returns the value in `obj` at `path`. If the given path does not exist,
* the symbol `none` is returned.
*
* @example
* ```
* // -> 'something'
* select(
* { a: { deeply: [{ nested: { object: 'something' } }] } },
* 'a', 'deeply', 0, 'nested', 'object'
* )
* ```
*/
const select = (obj, ...path) => path.length === 0 ? obj : obj instanceof Map ? obj.has(path[0]) ? select(obj.get(path[0]), ...path.slice(1)) : none : typeof obj !== "object" || obj === null || typeof path[0] !== "string" && typeof path[0] !== "number" && typeof path[0] !== "symbol" || !(path[0] in obj) ? none : select(obj[path[0]], ...path.slice(1));
export default select;