mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-27 15:04:02 +00:00
fix(angular.equals): relax the comparison for undefined properties
in5ae63fd3the 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 the5ae63fd3in 50% of the cases, changing the behavior in this way should not introduce any significant issues. Closes #1648
This commit is contained in:
parent
cdf7781878
commit
3c2e1c5e4d
2 changed files with 10 additions and 15 deletions
|
|
@ -620,23 +620,18 @@ function equals(o1, o2) {
|
||||||
} else {
|
} else {
|
||||||
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2)) return false;
|
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2)) return false;
|
||||||
keySet = {};
|
keySet = {};
|
||||||
length = 0;
|
|
||||||
for(key in o1) {
|
for(key in o1) {
|
||||||
if (key.charAt(0) === '$') continue;
|
if (key.charAt(0) === '$' || isFunction(o1[key])) continue;
|
||||||
|
if (!equals(o1[key], o2[key])) return false;
|
||||||
if (!isFunction(o1[key]) && !equals(o1[key], o2[key])) return false;
|
|
||||||
|
|
||||||
length++;
|
|
||||||
keySet[key] = true;
|
keySet[key] = true;
|
||||||
}
|
}
|
||||||
for(key in o2) {
|
for(key in o2) {
|
||||||
if (key.charAt(0) === '$') {
|
if (!keySet[key] &&
|
||||||
continue;
|
key.charAt(0) !== '$' &&
|
||||||
}
|
o2[key] !== undefined &&
|
||||||
if (!keySet[key] && !isFunction(o2[key])) return false;
|
!isFunction(o2[key])) return false;
|
||||||
length--;
|
|
||||||
}
|
}
|
||||||
return length === 0;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -126,12 +126,12 @@ describe('angular', function() {
|
||||||
expect(equals(['misko'], ['misko', 'adam'])).toEqual(false);
|
expect(equals(['misko'], ['misko', 'adam'])).toEqual(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should ignore undefined member variables', function() {
|
it('should ignore undefined member variables during comparison', function() {
|
||||||
var obj1 = {name: 'misko'},
|
var obj1 = {name: 'misko'},
|
||||||
obj2 = {name: 'misko', undefinedvar: undefined};
|
obj2 = {name: 'misko', undefinedvar: undefined};
|
||||||
|
|
||||||
expect(equals(obj1, obj2)).toBe(false);
|
expect(equals(obj1, obj2)).toBe(true);
|
||||||
expect(equals(obj2, obj1)).toBe(false);
|
expect(equals(obj2, obj1)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should ignore $ member variables', function() {
|
it('should ignore $ member variables', function() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue