From 3c2e1c5e4d12529b1d69a6173c38097527dccc4f Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Mon, 21 Jan 2013 22:00:15 -0800 Subject: [PATCH] fix(angular.equals): relax the comparison for undefined properties in 5ae63fd3 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 5ae63fd3 in 50% of the cases, changing the behavior in this way should not introduce any significant issues. Closes #1648 --- src/Angular.js | 19 +++++++------------ test/AngularSpec.js | 6 +++--- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index 21b3ef070eef..a7b3b98c1e60 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -620,23 +620,18 @@ 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) === '$') continue; - - if (!isFunction(o1[key]) && !equals(o1[key], o2[key])) return false; - - length++; + if (key.charAt(0) === '$' || isFunction(o1[key])) continue; + if (!equals(o1[key], o2[key])) return false; keySet[key] = true; } for(key in o2) { - if (key.charAt(0) === '$') { - continue; - } - if (!keySet[key] && !isFunction(o2[key])) return false; - length--; + if (!keySet[key] && + key.charAt(0) !== '$' && + o2[key] !== undefined && + !isFunction(o2[key])) return false; } - return length === 0; + return true; } } } diff --git a/test/AngularSpec.js b/test/AngularSpec.js index f5638b9c5288..09bc902fec73 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -126,12 +126,12 @@ describe('angular', function() { 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'}, obj2 = {name: 'misko', undefinedvar: undefined}; - expect(equals(obj1, obj2)).toBe(false); - expect(equals(obj2, obj1)).toBe(false); + expect(equals(obj1, obj2)).toBe(true); + expect(equals(obj2, obj1)).toBe(true); }); it('should ignore $ member variables', function() {