UNPKG

@modern-kit/utils

Version:
65 lines (60 loc) 2.03 kB
'use strict'; var validatorIsNumber = require('../isNumber/index.cjs'); var validatorIsFunction = require('../isFunction/index.cjs'); 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 (validatorIsNumber.isNumber(source) && validatorIsNumber.isNumber(target) && isNaN(source) && isNaN(target)) { return true; } if (validatorIsFunction.isFunction(source) && validatorIsFunction.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); } exports.isEqual = isEqual; //# sourceMappingURL=index.cjs.map