Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Make angular.equals support objects with circular references #7839

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,18 +879,38 @@ 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, seen_list, comparisons) {
if (o1 === o2) return true;
if (o1 === null || o2 === null) return false;
if (o1 !== o1 && o2 !== o2) return true; // NaN === NaN
var t1 = typeof o1, t2 = typeof o2, length, key, keySet;
if (t1 == t2) {
if (t1 == 'object') {
seen_list || (seen_list = []);
comparisons || (comparisons = []);
var i1 = seen_list.indexOf(o1);
var i2 = seen_list.indexOf(o2);
if (i1 != -1 && comparisons[i1].indexOf(o2) != -1) {
return true;
} else {
if (i1 == -1) {
seen_list.push(o1);
comparisons.push([o2]);
} else {
comparisons[i1].push(o2);
}
if (i2 == -1) {
seen_list.push(o2);
comparisons.push([o1]);
} else {
comparisons[i2].push(o1);
}
}
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], seen_list, comparisons)) return false;
}
return true;
}
Expand All @@ -903,7 +923,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], seen_list, comparisons)) return false;
keySet[key] = true;
}
for(key in o2) {
Expand Down
10 changes: 10 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,16 @@ describe('angular', function() {
expect(equals({}, [])).toBe(false);
expect(equals([], {})).toBe(false);
});

it('should handle circular references', function() {
var a = {a: null};
a.a = a;
var aCopy = copy(a, null);
expect(aCopy.a).toBe(aCopy);
expect(a).not.toBe(aCopy);
expect(equals(a, aCopy)).toBe(true);
});

});

describe('size', function() {
Expand Down