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

Commit 5ae63fd

Browse files
committed
fix(angular.equals): consistently compare undefined object props
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
1 parent 8b44324 commit 5ae63fd

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/Angular.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -620,16 +620,23 @@ function equals(o1, o2) {
620620
} else {
621621
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2)) return false;
622622
keySet = {};
623+
length = 0;
623624
for(key in o1) {
624-
if (key.charAt(0) !== '$' && !isFunction(o1[key]) && !equals(o1[key], o2[key])) {
625-
return false;
626-
}
625+
if (key.charAt(0) === '$') continue;
626+
627+
if (!isFunction(o1[key]) && !equals(o1[key], o2[key])) return false;
628+
629+
length++;
627630
keySet[key] = true;
628631
}
629632
for(key in o2) {
630-
if (!keySet[key] && key.charAt(0) !== '$' && !isFunction(o2[key])) return false;
633+
if (key.charAt(0) === '$') {
634+
continue;
635+
}
636+
if (!keySet[key] && !isFunction(o2[key])) return false;
637+
length--;
631638
}
632-
return true;
639+
return length === 0;
633640
}
634641
}
635642
}

test/AngularSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ describe('angular', function() {
126126
expect(equals(['misko'], ['misko', 'adam'])).toEqual(false);
127127
});
128128

129+
it('should ignore undefined member variables', function() {
130+
var obj1 = {name: 'misko'},
131+
obj2 = {name: 'misko', undefinedvar: undefined};
132+
133+
expect(equals(obj1, obj2)).toBe(false);
134+
expect(equals(obj2, obj1)).toBe(false);
135+
});
136+
129137
it('should ignore $ member variables', function() {
130138
expect(equals({name:'misko', $id:1}, {name:'misko', $id:2})).toEqual(true);
131139
expect(equals({name:'misko'}, {name:'misko', $id:2})).toEqual(true);

0 commit comments

Comments
 (0)