Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
inlining react-fast-compare to get around rollup configuration issues
- Loading branch information
1 parent
24f85f8
commit f85fe6d
Showing
4 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use strict'; | ||
|
||
var isArray = Array.isArray; | ||
var keyList = Object.keys; | ||
var hasProp = Object.prototype.hasOwnProperty; | ||
|
||
function equal(a, b) { | ||
// fast-deep-equal index.js 2.0.1 | ||
if (a === b) return true; | ||
|
||
if (a && b && typeof a == 'object' && typeof b == 'object') { | ||
var arrA = isArray(a) | ||
, arrB = isArray(b) | ||
, i | ||
, length | ||
, key; | ||
|
||
if (arrA && arrB) { | ||
length = a.length; | ||
if (length != b.length) return false; | ||
for (i = length; i-- !== 0;) { | ||
if (!equal(a[i], b[i])) return false; | ||
} | ||
return true; | ||
} | ||
|
||
if (arrA != arrB) return false; | ||
|
||
var dateA = a instanceof Date | ||
, dateB = b instanceof Date; | ||
if (dateA != dateB) return false; | ||
if (dateA && dateB) return a.getTime() == b.getTime(); | ||
|
||
var regexpA = a instanceof RegExp | ||
, regexpB = b instanceof RegExp; | ||
if (regexpA != regexpB) return false; | ||
if (regexpA && regexpB) return a.toString() == b.toString(); | ||
|
||
var keys = keyList(a); | ||
length = keys.length; | ||
|
||
if (length !== keyList(b).length) { | ||
return false; | ||
} | ||
|
||
for (i = length; i-- !== 0;) { | ||
if (!hasProp.call(b, keys[i])) return false; | ||
} | ||
// end fast-deep-equal | ||
|
||
// Custom handling for React | ||
for (i = length; i-- !== 0;) { | ||
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) | ||
// .$$typeof and ._store on just reasonable markers of a react element | ||
continue; | ||
} else { | ||
// all other properties should be traversed as usual | ||
if (!equal(a[key], b[key])) return false; | ||
} | ||
} | ||
|
||
// fast-deep-equal index.js 2.0.1 | ||
return true; | ||
} | ||
|
||
return a!==a && b!==b; | ||
} | ||
// end fast-deep-equal | ||
|
||
export default function exportedEqual(a, b) { | ||
try { | ||
return equal(a, b); | ||
} catch (error) { | ||
if (error.message && error.message.match(/stack|recursion/i)) { | ||
// warn on circular references, don't crash | ||
// browsers give this different errors name and messages: | ||
// chrome/safari: "RangeError", "Maximum call stack size exceeded" | ||
// firefox: "InternalError", too much recursion" | ||
// edge: "Error", "Out of stack space" | ||
console.warn('Warning: react-fast-compare does not handle circular references.', error.name, error.message); | ||
return false; | ||
} | ||
// some other error. we should definitely know about these | ||
throw error; | ||
} | ||
}; |