@adaptabletools/adaptable-cjs
Version:
Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
39 lines (38 loc) • 1.17 kB
JavaScript
;
/**
* Gets the value at path of object. If the resolved value is undefined,
* the defaultValue is returned in its place.
* Drop-in replacement for lodash/get.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = get;
const rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g;
function toPath(path) {
if (Array.isArray(path)) {
return path;
}
const result = [];
const str = String(path);
str.replace(rePropName, (match, number, quote, subString) => {
result.push(quote ? subString.replace(/\\(\\)?/g, '$1') : number || match);
return match;
});
return result;
}
function get(object, path, defaultValue) {
if (typeof path === 'string' && path === '') {
return defaultValue;
}
const keys = toPath(path);
if (keys.length === 0) {
return defaultValue;
}
let result = object;
for (const key of keys) {
if (result == null) {
return defaultValue;
}
result = result[key];
}
return result === undefined ? defaultValue : result;
}