Skip to content

Commit

Permalink
Add special case for Dates in clone
Browse files Browse the repository at this point in the history
Fixes #78
  • Loading branch information
chbrown committed Mar 11, 2021
1 parent 47e718d commit 16ff06f
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function isNonPrimitive(value: any): value is object {
/**
Recursively copy a value.
@param source - should be a JavaScript primitive, Array, or (plain old) Object.
@param source - should be a JavaScript primitive, Array, Date, or (plain old) Object.
@returns copy of source where every Array and Object have been recursively
reconstructed from their constituent elements
*/
Expand All @@ -42,6 +42,11 @@ export function clone<T extends any>(source: T): T {
}
return arrayTarget
}
// Date
if (source.constructor == Date) {
const dateTarget: any = new Date(+source)
return dateTarget
}
// Object
const objectTarget: any = {}
// declaring the variable (with const) inside the loop is faster
Expand Down

0 comments on commit 16ff06f

Please sign in to comment.