devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
128 lines (125 loc) • 4.95 kB
JavaScript
/**
* DevExtreme (cjs/__internal/core/utils/object.js)
* Version: 26.1.6
* Build date: Fri Oct 16 2026
*
* Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.clone = void 0;
exports.deepExtendArraySafe = deepExtendArraySafe;
exports.orderEach = exports.newAssign = exports.legacyAssign = exports.isCollectionLike = void 0;
var _type = require("../../../core/utils/type");
var _variable_wrapper = _interopRequireDefault(require("../../../core/utils/variable_wrapper"));
function _interopRequireDefault(e) {
return e && e.__esModule ? e : {
default: e
}
}
const clone = exports.clone = function() {
function Clone() {}
return function(obj) {
Clone.prototype = obj;
return new Clone
}
}();
const isBlobLike = function(value) {
return "undefined" !== typeof Blob && value instanceof Blob
};
const isCollectionLike = exports.isCollectionLike = function(value) {
return value instanceof Set || value instanceof Map || value instanceof WeakSet || value instanceof WeakMap
};
const orderEach = exports.orderEach = function(map, func) {
const keys = [];
for (const key in map) {
if (Object.prototype.hasOwnProperty.call(map, key)) {
keys.push(key)
}
}
keys.sort((x, y) => {
const isNumberX = (0, _type.isNumeric)(x);
const isNumberY = (0, _type.isNumeric)(y);
if (isNumberX && isNumberY) {
return x - y
}
if (isNumberX && !isNumberY) {
return -1
}
if (!isNumberX && isNumberY) {
return 1
}
if (x < y) {
return -1
}
if (x > y) {
return 1
}
return 0
});
for (const key of keys) {
func(key, map[key])
}
};
const getDeepCopyTarget = item => {
if ((0, _type.isObject)(item)) {
if (Array.isArray(item)) {
return []
}
if (isCollectionLike(item)) {
return item
}
if (!(0, _type.isPlainObject)(item)) {
return Object.create(Object.getPrototypeOf(item))
}
return {}
}
return item
};
const legacyAssign = exports.legacyAssign = function(target, property, value, extendComplexObject, assignByReference, shouldCopyUndefined) {
if (!assignByReference && _variable_wrapper.default.isWrapped(target[property])) {
_variable_wrapper.default.assign(target[property], value)
} else {
target[property] = value
}
};
function deepExtendArraySafe(target, changes, extendComplexObject, assignByReference, shouldCopyUndefined, useNewAssign) {
const assignFunc = useNewAssign ? newAssign : legacyAssign;
if (isBlobLike(changes) || isCollectionLike(changes)) {
return changes
}
const targetObject = target;
const changesObject = changes;
for (const name in changesObject) {
const prevValue = targetObject[name];
let newValue = changesObject[name];
if ("__proto__" === name || "constructor" === name || target === newValue) {
continue
}
if ((0, _type.isPlainObject)(newValue)) {
const goDeeper = extendComplexObject ? (0, _type.isObject)(prevValue) : (0, _type.isPlainObject)(prevValue);
newValue = deepExtendArraySafe(goDeeper ? prevValue : {}, newValue, extendComplexObject, assignByReference, shouldCopyUndefined)
}
const isDeepCopyArray = Array.isArray(newValue) && !assignByReference && prevValue !== newValue;
const hasDifferentNewValue = (!!shouldCopyUndefined || void 0 !== newValue) && prevValue !== newValue || shouldCopyUndefined && void 0 === prevValue;
if (isDeepCopyArray || hasDifferentNewValue) {
assignFunc(targetObject, name, newValue, extendComplexObject, assignByReference, shouldCopyUndefined)
}
}
return target
}
const newAssign = exports.newAssign = function newAssign(target, property, value, extendComplexObject, assignByReference, shouldCopyUndefined) {
const goDeeper = extendComplexObject ? (0, _type.isObject)(target) : (0, _type.isPlainObject)(target);
if (!assignByReference && _variable_wrapper.default.isWrapped(target[property])) {
_variable_wrapper.default.assign(target[property], value)
} else if (!assignByReference && Array.isArray(value)) {
target[property] = value.map(item => deepExtendArraySafe(getDeepCopyTarget(item), item, extendComplexObject, assignByReference, shouldCopyUndefined))
} else if (!assignByReference && goDeeper) {
target[property] = deepExtendArraySafe(getDeepCopyTarget(value), value, extendComplexObject, assignByReference, shouldCopyUndefined, newAssign)
} else {
target[property] = value
}
};