Skip to content

Instantly share code, notes, and snippets.

@jpenney1
Created April 12, 2019 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpenney1/65b9464cc47dedc164a43a02cbb3754d to your computer and use it in GitHub Desktop.
Save jpenney1/65b9464cc47dedc164a43a02cbb3754d to your computer and use it in GitHub Desktop.
const copy = item => {
return !item ? item
: Array.isArray(item) ? deepArrayCopy(item)
: Object.prototype.toString.call(item).includes('Date') ? new Date(item.getTime())
: typeof item === 'object' ? deepCopy(item)
: item
}
const deepCopy = obj => {
const objKeys = Object.keys(obj)
const newObj = {}
let i = objKeys.length
let key
while(i--) {
key = objKeys[i]
item = obj[key]
newObj[key] = copy(item)
}
return newObj
}
const deepArrayCopy = arr => {
let i = arr.length
let item
const newArr = []
while(i--) {
item = arr[i]
newArr[i] = copy(item)
}
return newArr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment