froebel
Version:
TypeScript utility library
29 lines (24 loc) • 937 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.none = exports.default = void 0;
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'
* )
* ```
*/
exports.none = none;
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));
var _default = select;
exports.default = _default;
module.exports = Object.assign(exports.default || {}, exports);