Skip to content

Commit

Permalink
goog.object.unsafeClone now clones Date objects correctly.
Browse files Browse the repository at this point in the history
Fixes #1175.

RELNOTES: goog.object.unsafeClone now clones Date objects correctly.
PiperOrigin-RevId: 474078524
Change-Id: I82a9a2af03baaf646cfdfad19da4fd0cf8163863
  • Loading branch information
12wrigja authored and Copybara-Service committed Sep 13, 2022
1 parent ff0bbf1 commit 767dd4b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions closure/goog/object/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ function unsafeClone(obj) {
return new Map(obj);
} else if (typeof Set !== 'undefined' && obj instanceof Set) {
return new Set(obj);
} else if (obj instanceof Date) {
return new Date(obj.getTime());
}
const clone = Array.isArray(obj) ? [] :
typeof ArrayBuffer === 'function' &&
Expand Down
10 changes: 10 additions & 0 deletions closure/goog/object/object_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ testSuite({
assertEquals(original.f, clone.f);
},

testUnsafeCloneDates() {
const original = {d: new Date(5000)};
const clone = googObject.unsafeClone(original);

assertNotEquals(original, clone);
assertNotEquals(original.d, clone.d);
assertTrue(clone.d instanceof Date);
assertEquals(5000, clone.d.getTime());
},

testForEach() {
const m = getObject();
let s = '';
Expand Down

0 comments on commit 767dd4b

Please sign in to comment.