Created
April 12, 2019 06:53
-
-
Save jpenney1/65b9464cc47dedc164a43a02cbb3754d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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