util.js
900 Bytes
/*
* @Description:
* @Author: huyue
* @Date: 2023-07-24 17:44:17
* @LastEditors: huyue
* @LastEditTime: 2023-07-24 17:46:45
*/
const deepClone = obj => {
// Handle the 3 simple types, and null or undefined or function
if (obj == null || typeof obj !== 'object') return obj
// Handle Date
if (obj instanceof Date) {
const copy = new Date()
copy.setTime(obj.getTime())
return copy
}
// Handle Array or Object
if ((obj instanceof Array) | (obj instanceof Object)) {
const copy = obj instanceof Array ? [] : {}
for (var attr in obj) {
// eslint-disable-next-line no-prototype-builtins
if (obj.hasOwnProperty(attr)) {
copy[attr] = deepClone(obj[attr])
}
}
return copy
}
throw new Error("Unable to clone obj! Its type isn't supported.")
}
const utils = {
deepClone
}
export default utils