From aca3363e3c4a43fdbc1e398a721b0d1b4d807b46 Mon Sep 17 00:00:00 2001 From: Evan Morikawa Date: Mon, 19 Sep 2016 20:13:23 -0400 Subject: [PATCH] fix(util): Utils.deepClone properly clones dates --- spec/utils-spec.coffee | 5 +++++ src/flux/models/utils.coffee | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/spec/utils-spec.coffee b/spec/utils-spec.coffee index 57b49b006..b4b9db216 100644 --- a/spec/utils-spec.coffee +++ b/spec/utils-spec.coffee @@ -76,6 +76,11 @@ describe 'Utils', -> @o2.circular = @o2 @o2Clone = Utils.deepClone(@o2) + it "deep clones dates correctly", -> + d1 = new Date(2016,1,1) + d2 = Utils.deepClone(d1) + expect(d2.valueOf()).toBe(d1.valueOf()) + it "makes a deep clone", -> @v1.push(4) @v2.push(7) diff --git a/src/flux/models/utils.coffee b/src/flux/models/utils.coffee index 33e51bb6e..fbb27136a 100644 --- a/src/flux/models/utils.coffee +++ b/src/flux/models/utils.coffee @@ -81,6 +81,10 @@ Utils = if _.isArray(object) # http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/ newObject = [] + else if object instanceof Date + # You can't clone dates by iterating through `getOwnPropertyNames` + # of the Date object. We need to special-case Dates. + newObject = new Date(object) else newObject = Object.create(Object.getPrototypeOf(object))