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

Commit bc72b42

Browse files
fix(equals): {} and [] should not be considered equalivalent
angular.equals was returning inconsistent values for the comparison between {} and []: angular.equals({}, []) // true angular.equals([], {}]) // false Since these object are not of the same type, they should not be considered equivalent.
1 parent 9ecff34 commit bc72b42

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/Angular.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -670,18 +670,26 @@ function equals(o1, o2) {
670670
if (o1 === o2) return true;
671671
if (o1 === null || o2 === null) return false;
672672
if (o1 !== o1 && o2 !== o2) return true; // NaN === NaN
673+
673674
var t1 = typeof o1, t2 = typeof o2, length, key, keySet;
674675
if (t1 == t2) {
675676
if (t1 == 'object') {
676-
if (isArray(o1)) {
677+
678+
var a1 = isArray(o1), a2 = isArray(o2);
679+
if (a1 != a2) {
680+
return false
681+
682+
} else if (a1) {
677683
if ((length = o1.length) == o2.length) {
678684
for(key=0; key<length; key++) {
679685
if (!equals(o1[key], o2[key])) return false;
680686
}
681687
return true;
682688
}
689+
683690
} else if (isDate(o1)) {
684691
return isDate(o2) && o1.getTime() == o2.getTime();
692+
685693
} else {
686694
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2)) return false;
687695
keySet = {};
@@ -690,12 +698,14 @@ function equals(o1, o2) {
690698
if (!equals(o1[key], o2[key])) return false;
691699
keySet[key] = true;
692700
}
701+
693702
for(key in o2) {
694703
if (!keySet[key] &&
695704
key.charAt(0) !== '$' &&
696705
o2[key] !== undefined &&
697706
!isFunction(o2[key])) return false;
698707
}
708+
699709
return true;
700710
}
701711
}

test/AngularSpec.js

+5
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ describe('angular', function() {
270270
expect(equals(new Date(0), 0)).toBe(false);
271271
expect(equals(0, new Date(0))).toBe(false);
272272
});
273+
274+
it('should return false when comparing an object and an array', function() {
275+
expect(equals({}, [])).toBe(false);
276+
expect(equals([], {})).toBe(false);
277+
});
273278
});
274279

275280
describe('size', function() {

0 commit comments

Comments
 (0)