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

fix(equals): {} and [] should not be considered equivalent #2852

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
12 changes: 11 additions & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,18 +670,26 @@ function equals(o1, o2) {
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') {
if (isArray(o1)) {

var a1 = isArray(o1), a2 = isArray(o2);
if (a1 != a2) {
return false

} else if (a1) {
if ((length = o1.length) == o2.length) {
for(key=0; key<length; key++) {
if (!equals(o1[key], o2[key])) return false;
}
return true;
}

} else if (isDate(o1)) {
return isDate(o2) && o1.getTime() == o2.getTime();

} else {
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2)) return false;
keySet = {};
Expand All @@ -690,12 +698,14 @@ function equals(o1, o2) {
if (!equals(o1[key], o2[key])) return false;
keySet[key] = true;
}

for(key in o2) {
if (!keySet[key] &&
key.charAt(0) !== '$' &&
o2[key] !== undefined &&
!isFunction(o2[key])) return false;
}

return true;
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ describe('angular', function() {
expect(equals(new Date(0), 0)).toBe(false);
expect(equals(0, new Date(0))).toBe(false);
});

it('should return false when comparing an object and an array', function() {
expect(equals({}, [])).toBe(false);
expect(equals([], {})).toBe(false);
});
});

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