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

Commit f22e1fc

Browse files
fix(Angular): properly compare RegExp with other objects for equality
Fixes #11204 Closes #11205
1 parent 80f139b commit f22e1fc

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/Angular.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -860,10 +860,11 @@ function equals(o1, o2) {
860860
} else if (isDate(o1)) {
861861
if (!isDate(o2)) return false;
862862
return equals(o1.getTime(), o2.getTime());
863-
} else if (isRegExp(o1) && isRegExp(o2)) {
864-
return o1.toString() == o2.toString();
863+
} else if (isRegExp(o1)) {
864+
return isRegExp(o2) ? o1.toString() == o2.toString() : false;
865865
} else {
866-
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2) || isArray(o2)) return false;
866+
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2) ||
867+
isArray(o2) || isDate(o2) || isRegExp(o2)) return false;
867868
keySet = {};
868869
for (key in o1) {
869870
if (key.charAt(0) === '$' || isFunction(o1[key])) continue;

test/AngularSpec.js

+11
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ describe('angular', function() {
547547
expect(equals(new Date(undefined), new Date(0))).toBe(false);
548548
expect(equals(new Date(undefined), new Date(null))).toBe(false);
549549
expect(equals(new Date(undefined), new Date('wrong'))).toBe(true);
550+
expect(equals(new Date(), /abc/)).toBe(false);
550551
});
551552

552553
it('should correctly test for keys that are present on Object.prototype', function() {
@@ -564,12 +565,22 @@ describe('angular', function() {
564565
expect(equals(/abc/, /def/)).toBe(false);
565566
expect(equals(/^abc/, /abc/)).toBe(false);
566567
expect(equals(/^abc/, '/^abc/')).toBe(false);
568+
expect(equals(/abc/, new Date())).toBe(false);
567569
});
568570

569571
it('should return false when comparing an object and an array', function() {
570572
expect(equals({}, [])).toBe(false);
571573
expect(equals([], {})).toBe(false);
572574
});
575+
576+
it('should return false when comparing an object and a RegExp', function() {
577+
expect(equals({}, /abc/)).toBe(false);
578+
expect(equals({}, new RegExp('abc', 'i'))).toBe(false);
579+
});
580+
581+
it('should return false when comparing an object and a Date', function() {
582+
expect(equals({}, new Date())).toBe(false);
583+
});
573584
});
574585

575586

0 commit comments

Comments
 (0)