@newdash/newdash
Version:
javascript/typescript utility library
36 lines (35 loc) • 1.21 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const castPath_1 = __importDefault(require("./castPath"));
const last_1 = __importDefault(require("../last"));
const parent_1 = __importDefault(require("./parent"));
const toKey_1 = __importDefault(require("./toKey"));
/**
* The base implementation of `unset`.
*
* @private
* @ignore
* @param {Object} object The object to modify.
* @param {Array|string} path The property path to unset.
* @returns {boolean} Returns `true` if the property is deleted, else `false`.
*/
function baseUnset(object, path) {
path = (0, castPath_1.default)(path, object);
object = (0, parent_1.default)(object, path);
try {
const rt = object == null || delete object[(0, toKey_1.default)((0, last_1.default)(path))];
return rt;
}
catch (error) {
if (error instanceof TypeError) {
if (error.message.includes("Cannot delete property")) {
return false;
}
}
throw error;
}
}
exports.default = baseUnset;