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

Commit

Permalink
fix(angular.equals): consistently compare undefined object props
Browse files Browse the repository at this point in the history
previously:

a = {};
b = {x:undefined};
angular.equals(a, b) == angular.equals(b, a) // returns false.

both should return false because these objects are not equal.

unlike in implemented in #1695 the comparison should be stricter
and return false not true. if we need to relax this in the future
we can always do so.

Closes #1648
  • Loading branch information
IgorMinar committed Jan 18, 2013
1 parent d5a1336 commit a6b2ee7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,16 +620,23 @@ function equals(o1, o2) {
} else {
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2)) return false;
keySet = {};
length = 0;
for(key in o1) {
if (key.charAt(0) !== '$' && !isFunction(o1[key]) && !equals(o1[key], o2[key])) {
return false;
}
if (key.charAt(0) === '$') continue;

if (!isFunction(o1[key]) && !equals(o1[key], o2[key])) return false;

length++;
keySet[key] = true;
}
for(key in o2) {
if (!keySet[key] && key.charAt(0) !== '$' && !isFunction(o2[key])) return false;
if (key.charAt(0) === '$') {
continue;
}
if (!keySet[key] && !isFunction(o2[key])) return false;
length--;
}
return true;
return length === 0;
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ describe('angular', function() {
expect(equals(['misko'], ['misko', 'adam'])).toEqual(false);
});

it('should ignore undefined member variables', function() {
var obj1 = {name: 'misko'},
obj2 = {name: 'misko', undefinedvar: undefined};

expect(equals(obj1, obj2)).toBe(false);
expect(equals(obj2, obj1)).toBe(false);
});

it('should ignore $ member variables', function() {
expect(equals({name:'misko', $id:1}, {name:'misko', $id:2})).toEqual(true);
expect(equals({name:'misko'}, {name:'misko', $id:2})).toEqual(true);
Expand Down

0 comments on commit a6b2ee7

Please sign in to comment.