fix(Angular): properly compare RegExp with other objects for equality

Fixes #11204

Closes #11205
This commit is contained in:
Pawel Kozlowski
2015-02-27 16:13:51 +01:00
parent 01161a0e9f
commit b8e8f9af78
2 changed files with 15 additions and 3 deletions
+4 -3
View File
@@ -850,10 +850,11 @@ function equals(o1, o2) {
} else if (isDate(o1)) {
if (!isDate(o2)) return false;
return equals(o1.getTime(), o2.getTime());
} else if (isRegExp(o1) && isRegExp(o2)) {
return o1.toString() == o2.toString();
} else if (isRegExp(o1)) {
return isRegExp(o2) ? o1.toString() == o2.toString() : false;
} else {
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2) || isArray(o2)) return false;
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2) ||
isArray(o2) || isDate(o2) || isRegExp(o2)) return false;
keySet = {};
for (key in o1) {
if (key.charAt(0) === '$' || isFunction(o1[key])) continue;
+11
View File
@@ -367,6 +367,7 @@ describe('angular', function() {
expect(equals(new Date(undefined), new Date(0))).toBe(false);
expect(equals(new Date(undefined), new Date(null))).toBe(false);
expect(equals(new Date(undefined), new Date('wrong'))).toBe(true);
expect(equals(new Date(), /abc/)).toBe(false);
});
it('should correctly test for keys that are present on Object.prototype', function() {
@@ -384,12 +385,22 @@ describe('angular', function() {
expect(equals(/abc/, /def/)).toBe(false);
expect(equals(/^abc/, /abc/)).toBe(false);
expect(equals(/^abc/, '/^abc/')).toBe(false);
expect(equals(/abc/, new Date())).toBe(false);
});
it('should return false when comparing an object and an array', function() {
expect(equals({}, [])).toBe(false);
expect(equals([], {})).toBe(false);
});
it('should return false when comparing an object and a RegExp', function() {
expect(equals({}, /abc/)).toBe(false);
expect(equals({}, new RegExp('abc', 'i'))).toBe(false);
});
it('should return false when comparing an object and a Date', function() {
expect(equals({}, new Date())).toBe(false);
});
});