@n3okill/utils
Version:
Many javascript helpers
33 lines • 1.73 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeProperty = removeProperty;
const isString_1 = require("../type/isString");
const _internal_1 = require("./_internal");
/**
* Remove a property from the object
* @param obj The object where to remove the property
* @param {string | Array<string>} name The name of the property can be on the form "a.b.c[1].d"
* @returns {any} The value of the removed property if defined
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function removeProperty(obj, name) {
if ((0, isString_1.isString)(name) && obj && Object.prototype.hasOwnProperty.call(obj, name)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, security/detect-object-injection
const value = obj[name];
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, security/detect-object-injection
delete obj[name];
return value;
}
const parts = (0, _internal_1.getNameParts)((0, isString_1.isString)(name) ? name.split(".") : name);
const last = parts.pop();
while (obj && parts.length) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
obj = obj[parts.shift()];
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, security/detect-object-injection
const value = obj[last];
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, security/detect-object-injection
delete obj[last];
return value;
}
//# sourceMappingURL=removeProperty.js.map
;