UNPKG

get-value

Version:

Use property paths like 'a.b.c' to get a nested value from an object. Even works when keys have dots in them (no other dot-prop library we tested does this, or does it correctly).

110 lines (109 loc) 3.36 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // index.ts var isObject = /* @__PURE__ */ __name((v) => v !== null && typeof v === "object", "isObject"); var join = /* @__PURE__ */ __name((segs, joinChar, options) => { if (typeof options.join === "function") { return options.join(segs); } return segs[0] + joinChar + segs[1]; }, "join"); var split = /* @__PURE__ */ __name((path, splitChar, options) => { if (typeof options.split === "function") { return options.split(path); } return path.split(splitChar); }, "split"); var isValid = /* @__PURE__ */ __name((key, target = {}, options) => { if (typeof options?.isValid === "function") { return options.isValid(key, target); } return true; }, "isValid"); var isValidObject = /* @__PURE__ */ __name((v) => { return isObject(v) || typeof v === "function"; }, "isValidObject"); var getValue = /* @__PURE__ */ __name((target, path, options = {}) => { if (!isObject(options)) { options = { default: options }; } if (!isValidObject(target)) { return typeof options.default !== "undefined" ? options.default : target; } if (typeof path === "number") { path = String(path); } const pathIsArray = Array.isArray(path); const pathIsString = typeof path === "string"; const splitChar = options.separator || "."; const joinChar = options.joinChar || (typeof splitChar === "string" ? splitChar : "."); if (!pathIsString && !pathIsArray) { return target; } let value = target[path]; if (value === void 0 && typeof target.get === "function") { value = target.get(path); } if (value !== void 0) { return isValid(path, target, options) ? value : options.default; } const segs = pathIsArray ? path : split(path, splitChar, options); const len = segs.length; let idx = 0; do { let prop = segs[idx]; if (typeof prop !== "string") { prop = String(prop); } while (prop && prop.slice(-1) === "\\") { prop = join([prop.slice(0, -1), segs[++idx] || ""], joinChar, options); } value = target[prop]; if (value === void 0 && typeof target.get === "function") { value = target.get(prop); } if (value !== void 0) { if (!isValid(prop, target, options)) { return options.default; } target = value; } else { let hasProp = false; let n = idx + 1; while (n < len) { prop = join([prop, segs[n++]], joinChar, options); value = target[prop]; if (value === void 0 && typeof target.get === "function") { value = target.get(prop); } if (hasProp = value !== void 0) { if (!isValid(prop, target, options)) { return options.default; } target = value; idx = n - 1; break; } } if (!hasProp) { return options.default; } } } while (++idx < len && isValidObject(target)); if (idx === len) { return target; } return options.default; }, "getValue"); var index_default = getValue; export { index_default as default, getValue }; /*! * get-value <https://github.com/jonschlinkert/get-value> * * Copyright (c) 2014-present, Jon Schlinkert. * Released under the MIT License. */ //# sourceMappingURL=index.mjs.map