fix(util): Utils.deepClone properly clones dates

This commit is contained in:
Evan Morikawa 2016-09-19 20:13:23 -04:00
parent a9d0f3f56a
commit aca3363e3c
2 changed files with 9 additions and 0 deletions

View file

@ -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)

View file

@ -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))