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

Commit 3c2e1c5

Browse files
committed
fix(angular.equals): relax the comparison for undefined properties
in 5ae63fd the comparison was made consistent but strict, so that angular.equals({}, {foo: undefined}) // always returns false this turns out to cause issues for data that is being roundtripped via network and serialized via JSON because JSON.stringify serializes {foo: undefined} as {}. Since angular.equals() behaved like this before the 5ae63fd in 50% of the cases, changing the behavior in this way should not introduce any significant issues. Closes #1648
1 parent cdf7781 commit 3c2e1c5

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

src/Angular.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -620,23 +620,18 @@ function equals(o1, o2) {
620620
} else {
621621
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2)) return false;
622622
keySet = {};
623-
length = 0;
624623
for(key in o1) {
625-
if (key.charAt(0) === '$') continue;
626-
627-
if (!isFunction(o1[key]) && !equals(o1[key], o2[key])) return false;
628-
629-
length++;
624+
if (key.charAt(0) === '$' || isFunction(o1[key])) continue;
625+
if (!equals(o1[key], o2[key])) return false;
630626
keySet[key] = true;
631627
}
632628
for(key in o2) {
633-
if (key.charAt(0) === '$') {
634-
continue;
635-
}
636-
if (!keySet[key] && !isFunction(o2[key])) return false;
637-
length--;
629+
if (!keySet[key] &&
630+
key.charAt(0) !== '$' &&
631+
o2[key] !== undefined &&
632+
!isFunction(o2[key])) return false;
638633
}
639-
return length === 0;
634+
return true;
640635
}
641636
}
642637
}

test/AngularSpec.js

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

129-
it('should ignore undefined member variables', function() {
129+
it('should ignore undefined member variables during comparison', function() {
130130
var obj1 = {name: 'misko'},
131131
obj2 = {name: 'misko', undefinedvar: undefined};
132132

133-
expect(equals(obj1, obj2)).toBe(false);
134-
expect(equals(obj2, obj1)).toBe(false);
133+
expect(equals(obj1, obj2)).toBe(true);
134+
expect(equals(obj2, obj1)).toBe(true);
135135
});
136136

137137
it('should ignore $ member variables', function() {

0 commit comments

Comments
 (0)