@netlify/content-engine
Version:
35 lines • 1.15 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getValueAt = getValueAt;
function getValueAt(obj, selector, options = {
keepNonMatches: false,
}) {
const selectors = typeof selector === `string` ? selector.split(`.`) : selector;
return get(obj, selectors, options);
}
function get(obj, selectors, options) {
if (typeof obj !== `object` || obj === null)
return undefined;
if (Array.isArray(obj))
return getArray(obj, selectors, options);
const [key, ...rest] = selectors;
const value = obj[key];
if (!value && options?.keepNonMatches) {
return obj;
}
if (!rest.length)
return value;
if (Array.isArray(value))
return getArray(value, rest, options);
if (value && typeof value === `object`)
return get(value, rest, options);
return undefined;
}
function getArray(arr, selectors, options) {
return arr
.map((value) => Array.isArray(value)
? getArray(value, selectors, options)
: get(value, selectors, options))
.filter((v) => v !== undefined);
}
//# sourceMappingURL=get-value-at.js.map
;