UNPKG

@zodash/get

Version:

Get the value at path of object.

40 lines 1.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.get = void 0; const to_path_1 = require("@zodash/to-path"); const is_1 = require("@zcorky/is"); /** * Get the value at path of object. * If the resolved values is undefined, the defaultValue is returnted in its place. * * @param value The object to query. * @param path The path of the property to get. * @param defaultValue The value returned for undefined resolved values. */ function get(value, path, defaultValue) { const _v = getValue(value, to_path_1.toPath(path)); return !is_1.undefined(_v) ? _v : defaultValue; } exports.get = get; function getValue(parent, paths, currentIndex = 0) { if (is_1.undefined(parent) || is_1.nul(parent)) return undefined; const token = paths[currentIndex]; const nextToken = paths[currentIndex + 1]; // object if (token !== '[]') { if (is_1.undefined(nextToken)) { return parent[token]; } return getValue(parent[token], paths, currentIndex + 1); } // array if (!is_1.array(parent)) { return undefined; } if (is_1.undefined(nextToken)) { return parent; } return parent.map((v) => getValue(v, paths, currentIndex + 1)); } //# sourceMappingURL=get.js.map