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

Commit 1dcafd1

Browse files
appsforartistsjeffbcross
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 4a7b6a4 commit 1dcafd1

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
@@ -687,6 +687,7 @@ function equals(o1, o2) {
687687
if (t1 == t2) {
688688
if (t1 == 'object') {
689689
if (isArray(o1)) {
690+
if (!isArray(o2)) return false;
690691
if ((length = o1.length) == o2.length) {
691692
for(key=0; key<length; key++) {
692693
if (!equals(o1[key], o2[key])) return false;
@@ -698,7 +699,7 @@ function equals(o1, o2) {
698699
} else if (isRegExp(o1) && isRegExp(o2)) {
699700
return o1.toString() == o2.toString();
700701
} else {
701-
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2)) return false;
702+
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2) || isArray(o2)) return false;
702703
keySet = {};
703704
for(key in o1) {
704705
if (key.charAt(0) === '$' || isFunction(o1[key])) continue;

test/AngularSpec.js

+5
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ describe('angular', function() {
289289
expect(equals(/^abc/, /abc/)).toBe(false);
290290
expect(equals(/^abc/, '/^abc/')).toBe(false);
291291
});
292+
293+
it('should return false when comparing an object and an array', function() {
294+
expect(equals({}, [])).toBe(false);
295+
expect(equals([], {})).toBe(false);
296+
});
292297
});
293298

294299
describe('size', function() {

0 commit comments

Comments
 (0)