This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix(equals): handle objects with circular references #11653
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -859,7 +859,12 @@ function shallowCopy(src, dst) { | |
|
||
return dst || src; | ||
} | ||
|
||
function equalsCacheContains(cache, o1, o2) { | ||
for (var i = 0, ii = cache.length; i < ii; i += 2) { | ||
if (cache[i] === o1 && cache[i + 1] === o2) return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @ngdoc function | ||
|
@@ -890,24 +895,27 @@ function shallowCopy(src, dst) { | |
* @param {*} o2 Object or value to compare. | ||
* @returns {boolean} True if arguments are equal. | ||
*/ | ||
function equals(o1, o2) { | ||
function equals(o1, o2, cache) { | ||
if (o1 === o2) return true; | ||
if (o1 === null || o2 === null) return false; | ||
if (o1 !== o1 && o2 !== o2) return true; // NaN === NaN | ||
cache = cache || []; | ||
if (equalsCacheContains(cache, o1, o2)) return true; | ||
cache.push(o1, o2); | ||
var t1 = typeof o1, t2 = typeof o2, length, key, keySet; | ||
if (t1 == t2) { | ||
if (t1 == 'object') { | ||
if (isArray(o1)) { | ||
if (!isArray(o2)) return false; | ||
if ((length = o1.length) == o2.length) { | ||
for (key = 0; key < length; key++) { | ||
if (!equals(o1[key], o2[key])) return false; | ||
if (!equals(o1[key], o2[key], cache)) return false; | ||
} | ||
return true; | ||
} | ||
} else if (isDate(o1)) { | ||
if (!isDate(o2)) return false; | ||
return equals(o1.getTime(), o2.getTime()); | ||
return equals(o1.getTime(), o2.getTime(), cache); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated to this PR, but since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is to support dates where |
||
} else if (isRegExp(o1)) { | ||
return isRegExp(o2) ? o1.toString() == o2.toString() : false; | ||
} else { | ||
|
@@ -916,7 +924,7 @@ function equals(o1, o2) { | |
keySet = {}; | ||
for (key in o1) { | ||
if (key.charAt(0) === '$' || isFunction(o1[key])) continue; | ||
if (!equals(o1[key], o2[key])) return false; | ||
if (!equals(o1[key], o2[key], cache)) return false; | ||
keySet[key] = true; | ||
} | ||
for (key in o2) { | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be faster (not tested):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not safe as is (e.g.
index
has to be an even number).