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

Commit da1f7c7

Browse files
appsforartistsbtford
authored andcommitted
fix(equals): {} and [] should not be considered equivalent
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 89366bd commit da1f7c7

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/Angular.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ function equals(o1, o2) {
665665
if (t1 == t2) {
666666
if (t1 == 'object') {
667667
if (isArray(o1)) {
668+
if (!isArray(o2)) return false;
668669
if ((length = o1.length) == o2.length) {
669670
for(key=0; key<length; key++) {
670671
if (!equals(o1[key], o2[key])) return false;
@@ -676,7 +677,7 @@ function equals(o1, o2) {
676677
} else if (isRegExp(o1) && isRegExp(o2)) {
677678
return o1.toString() == o2.toString();
678679
} else {
679-
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2)) return false;
680+
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2) || isArray(o2)) return false;
680681
keySet = {};
681682
for(key in o1) {
682683
if (key.charAt(0) === '$' || isFunction(o1[key])) continue;

test/AngularSpec.js

+5
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ describe('angular', function() {
302302
expect(equals(/^abc/, /abc/)).toBe(false);
303303
expect(equals(/^abc/, '/^abc/')).toBe(false);
304304
});
305+
306+
it('should return false when comparing an object and an array', function() {
307+
expect(equals({}, [])).toBe(false);
308+
expect(equals([], {})).toBe(false);
309+
});
305310
});
306311

307312
describe('size', function() {

0 commit comments

Comments
 (0)