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

Commit 767dd4b

Browse files
12wrigjacopybara-github
authored andcommitted
goog.object.unsafeClone now clones Date objects correctly.
Fixes #1175. RELNOTES: goog.object.unsafeClone now clones Date objects correctly. PiperOrigin-RevId: 474078524 Change-Id: I82a9a2af03baaf646cfdfad19da4fd0cf8163863
1 parent ff0bbf1 commit 767dd4b

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

closure/goog/object/object.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ function unsafeClone(obj) {
454454
return new Map(obj);
455455
} else if (typeof Set !== 'undefined' && obj instanceof Set) {
456456
return new Set(obj);
457+
} else if (obj instanceof Date) {
458+
return new Date(obj.getTime());
457459
}
458460
const clone = Array.isArray(obj) ? [] :
459461
typeof ArrayBuffer === 'function' &&

closure/goog/object/object_test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ testSuite({
268268
assertEquals(original.f, clone.f);
269269
},
270270

271+
testUnsafeCloneDates() {
272+
const original = {d: new Date(5000)};
273+
const clone = googObject.unsafeClone(original);
274+
275+
assertNotEquals(original, clone);
276+
assertNotEquals(original.d, clone.d);
277+
assertTrue(clone.d instanceof Date);
278+
assertEquals(5000, clone.d.getTime());
279+
},
280+
271281
testForEach() {
272282
const m = getObject();
273283
let s = '';

0 commit comments

Comments
 (0)