hd-utils
Version:
A handy utils for modern JS developers
82 lines (81 loc) • 2.94 kB
JavaScript
//@ts-nocheck
var envHasBigInt64Array = typeof BigInt64Array !== 'undefined';
/**
* @description The fastest deep equal with ES6 Map, Set and Typed arrays support.
* the same as fast-deep-equal package.
* @example console.log(equal({foo: 'bar'}, {foo: 'bar'})); // true
*
*/
export default function fastDeepEqualReact(a, b) {
if (a === b)
return true;
if (a && b && typeof a == 'object' && typeof b == 'object') {
if (a.constructor !== b.constructor)
return false;
var length, i, keys;
if (Array.isArray(a) && Array.isArray(b)) {
length = a.length;
if (length != b.length)
return false;
for (i = length; i-- !== 0;)
if (!fastDeepEqualReact(a[i], b[i]))
return false;
return true;
}
if (a instanceof Map && b instanceof Map) {
if (a.size !== b.size)
return false;
for (i of a.entries())
if (!b.has(i[0]))
return false;
for (i of a.entries())
if (!fastDeepEqualReact(i[1], b.get(i[0])))
return false;
return true;
}
if (a instanceof Set && b instanceof Set) {
if (a.size !== b.size)
return false;
for (i of a.entries())
if (!b.has(i[0]))
return false;
return true;
}
if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
length = a.length;
if (length != b.length)
return false;
for (i = length; i-- !== 0;)
if (a[i] !== b[i])
return false;
return true;
}
if (a.constructor === RegExp)
return a.source === b.source && a.flags === b.flags;
if (a.valueOf !== Object.prototype.valueOf)
return a.valueOf() === b.valueOf();
if (a.toString !== Object.prototype.toString)
return a.toString() === b.toString();
keys = Object.keys(a);
length = keys.length;
if (length !== Object.keys(b).length)
return false;
for (i = length; i-- !== 0;)
if (!Object.prototype.hasOwnProperty.call(b, keys[i]))
return false;
for (i = length; i-- !== 0;) {
var key = keys[i];
if (key === '_owner' && a.$$typeof) {
// React-specific: avoid traversing React elements' _owner.
// _owner contains circular references
// and is not needed when comparing the actual elements (and not their owners)
continue;
}
if (!fastDeepEqualReact(a[key], b[key]))
return false;
}
return true;
}
// true if both NaN, false otherwise
return a !== a && b !== b;
}