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

Commit f80730f

Browse files
AndyHitchmanIgorMinar
authored andcommitted
fix(angular.copy): change angular.copy to correcly clone RegExp
angular.copy previously copied RegExp as an empty object. Change detects RegExp instance and clones into new RegExp. This change is based on a previous fix to allow Date to be copied. Closes #3473 Closes #3474
1 parent 43997c1 commit f80730f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Angular.js

+2
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,8 @@ function copy(source, destination){
611611
destination = copy(source, []);
612612
} else if (isDate(source)) {
613613
destination = new Date(source.getTime());
614+
} else if (isRegExp(source)) {
615+
destination = new RegExp(source.source);
614616
} else if (isObject(source)) {
615617
destination = copy(source, {});
616618
}

test/AngularSpec.js

+23
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ describe('angular', function() {
3131
expect(copy(date) === date).toBeFalsy();
3232
});
3333

34+
it("should copy RegExp", function() {
35+
var re = new RegExp(".*");
36+
expect(copy(re) instanceof RegExp).toBeTruthy();
37+
expect(copy(re).source).toBe(".*");
38+
expect(copy(re) === re).toBe(false);
39+
});
40+
41+
it("should copy literal RegExp", function() {
42+
var re = /.*/;
43+
expect(copy(re) instanceof RegExp).toBeTruthy();
44+
expect(copy(re).source).toEqual(".*");
45+
expect(copy(re) === re).toBeFalsy();
46+
});
47+
48+
it("should deeply copy literal RegExp", function() {
49+
var objWithRegExp = {
50+
re: /.*/
51+
};
52+
expect(copy(objWithRegExp).re instanceof RegExp).toBeTruthy();
53+
expect(copy(objWithRegExp).re.source).toEqual(".*");
54+
expect(copy(objWithRegExp.re) === objWithRegExp.re).toBeFalsy();
55+
});
56+
3457
it("should deeply copy an array into an existing array", function() {
3558
var src = [1, {name:"value"}];
3659
var dst = [{key:"v"}];

0 commit comments

Comments
 (0)