@n3okill/utils
Version:
Many javascript helpers
28 lines • 1.41 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProperty = getProperty;
const isString_1 = require("../type/isString");
const _internal_1 = require("./_internal");
/**
* Return the value of a property in the object
* @param obj The object to search for the property
* @param {string | Array<string>} name The name of the property can be on the form "a.b.c[1].d"
* @param defaultValue Default value to be returned if the property don't exist
* @returns {any} Value of the property or defaultValue
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getProperty(obj, name, defaultValue) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
let o = obj;
if ((0, isString_1.isString)(name) && o && Object.prototype.hasOwnProperty.call(o, name)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, security/detect-object-injection
return o[name];
}
const parts = (0, _internal_1.getNameParts)((0, isString_1.isString)(name) ? name.split(".") : name);
while (o && parts.length) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
o = o[parts.shift()];
}
return parts.length === 0 ? o : defaultValue !== undefined ? defaultValue : undefined;
}
//# sourceMappingURL=getProperty.js.map
;