@modern-kit/utils
Version:
63 lines (59 loc) • 1.92 kB
JavaScript
import { isNumber } from '../isNumber/index.mjs';
import { isFunction } from '../isFunction/index.mjs';
const compareObjectOrArray = (source, target, visited) => {
const sourceKeys = Object.keys(source);
const targetKeys = Object.keys(target);
if (sourceKeys.length !== targetKeys.length) {
return false;
}
for (let i = 0; i < sourceKeys.length; i++) {
const key = sourceKeys[i];
if (!targetKeys.includes(key) || !isEqualInternal(source[key], target[key], visited)) {
return false;
}
}
return true;
};
const isEqualInternal = (source, target, visited) => {
if (source === target) {
return true;
}
if (isNumber(source) && isNumber(target) && isNaN(source) && isNaN(target)) {
return true;
}
if (isFunction(source) && isFunction(target)) {
return source.toString() === target.toString();
}
if (typeof source !== "object" || typeof target !== "object" || source === null || target === null) {
return false;
}
if (visited.has(source) && visited.get(source) === target) {
return true;
}
visited.set(source, target);
if (source.constructor !== target.constructor) {
return false;
}
if (source instanceof Set) {
if (source.size !== target.size) return false;
const sourceSetToArr = [...source];
const targetSetToArr = [...target];
return compareObjectOrArray(sourceSetToArr, targetSetToArr, visited);
}
if (source instanceof Map) {
if (source.size !== target.size) return false;
for (const [key, value] of source) {
if (!target.has(key) || !isEqualInternal(value, target.get(key), visited)) {
return false;
}
}
return true;
}
return compareObjectOrArray(source, target, visited);
};
function isEqual(source, target) {
const visited = /* @__PURE__ */ new WeakMap();
return isEqualInternal(source, target, visited);
}
export { isEqual };
//# sourceMappingURL=index.mjs.map