Skip to content

Commit

Permalink
fix cloneObject() function to respect RegExp (MagicMirrorOrg#3237)
Browse files Browse the repository at this point in the history
  • Loading branch information
khassel committed Oct 19, 2023
1 parent a0b444d commit 6c849a9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ _This release is scheduled to be released on 2024-01-01._

- Fix the option eventClass on customEvents.
- Fix yr API version in locationforecast call (#3227)
- Fix cloneObject() function to respect RegExp (#3237)

## [2.25.0] - 2023-10-01

Expand Down
4 changes: 4 additions & 0 deletions js/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ function cloneObject(obj) {
return obj;
}

if (obj.constructor.name === "RegExp") {
return new RegExp(obj);
}

const temp = obj.constructor(); // give temp the original obj's constructor
for (const key in obj) {
temp[key] = cloneObject(obj[key]);
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/classes/class_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ describe("File js/class", () => {
expect(obj).toBe(expected);
});

it("should clone regex", () => {
const expected = /.*Magic/;
const obj = clone(expected);
expect(obj).toEqual(expected);
expect(expected === obj).toBe(false);
});

it("should clone undefined", () => {
const expected = undefined;
const obj = clone(expected);
Expand Down

0 comments on commit 6c849a9

Please sign in to comment.