function copyData (target) {
function checkType (val) {
return Object.prototype.toString.call(val).slice(8, -1)
}
let res; const type = checkType(target)
if (type === 'Object') {
res = {}
} else if (type === 'Array') {
res = []
} else {
return target
}
for (const i in target) {
const value = target[i]
if (checkType(value) === 'Object' || checkType(value) === 'Array') {
res[i] = copyData(value)
} else {
res[i] = value
}
}
return res
}
function deepClone (obj) {
const _toString = Object.prototype.toString
if (!obj || typeof obj !== 'object') {
return obj
}
if (obj.nodeType && 'cloneNode' in obj) {
return obj.cloneNode(true)
}
if (_toString.call(obj) === '[object Date]') {
return new Date(obj.getTime())
}
if (_toString.call(obj) === '[object RegExp]') {
const flags = []
if (obj.global) { flags.push('g') }
if (obj.multiline) { flags.push('m') }
if (obj.ignoreCase) { flags.push('i') }
return new RegExp(obj.source, flags.join(''))
}
const result = Array.isArray(obj) ? [] : obj.constructor ? new obj.constructor() : {}
for (const key in obj) {
result[key] = deepClone(obj[key])
}
return result
}