@newdash/newdash
Version:
javascript/typescript utility library
47 lines (46 loc) • 1.86 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assignValue_1 = __importDefault(require("./assignValue"));
const castPath_1 = __importDefault(require("./castPath"));
const isIndex_1 = __importDefault(require("./isIndex"));
const isObject_1 = __importDefault(require("../isObject"));
const toKey_1 = __importDefault(require("./toKey"));
/**
* The base implementation of `set`.
*
* @private
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {*} value The value to set.
* @param {Function} [customizer] The function to customize path creation.
* @returns {Object} Returns `object`.
*/
function baseSet(object, path, value, customizer) {
if (!(0, isObject_1.default)(object)) {
return object;
}
path = (0, castPath_1.default)(path, object);
var index = -1, length = path.length, lastIndex = length - 1, nested = object;
while (nested != null && ++index < length) {
var key = (0, toKey_1.default)(path[index]), newValue = value;
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
return object;
}
if (index != lastIndex) {
var objValue = nested[key];
newValue = customizer ? customizer(objValue, key, nested) : undefined;
if (newValue === undefined) {
newValue = (0, isObject_1.default)(objValue)
? objValue
: ((0, isIndex_1.default)(path[index + 1]) ? [] : {});
}
}
(0, assignValue_1.default)(nested, key, newValue);
nested = nested[key];
}
return object;
}
exports.default = baseSet;