@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
38 lines • 1.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.get = get;
/**
* https://github.com/Shopify/polaris/blob/main/polaris-react/src/utilities/get.ts#L3
*/
const OBJECT_NOTATION_MATCHER = /(\w+)/g;
function get(keypath, objs) {
const keys = Array.isArray(keypath) ? keypath : getKeypath(keypath);
for (const obj of objs) {
if (!obj) {
continue;
}
let acc = obj;
for (let i = 0; i < keys.length; i++) {
const val = acc[keys[i]];
if (val === undefined) {
continue;
}
acc = val;
}
if (typeof acc === "string") {
return acc;
}
}
throw new Error(`Error translating key. Keypath '${keypath}' does not resolve to a string.`);
}
function getKeypath(str) {
const path = [];
let result = OBJECT_NOTATION_MATCHER.exec(str);
while (result) {
const [, first, second] = result;
path.push(first || second);
result = OBJECT_NOTATION_MATCHER.exec(str);
}
return path;
}
//# sourceMappingURL=get.js.map