Skip to content

Commit

Permalink
fix/build-nobelium
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Aug 1, 2023
1 parent 72dbf54 commit 570ab5a
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,32 @@ export function isIterable(obj) {
return obj != null && typeof obj[Symbol.iterator] === 'function'
}

/**
* 深拷贝对象
* 根据源对象类型深度复制,支持object和array
* @param {*} obj
* @returns
*/
export function deepClone(obj) {
const newObj = Array.isArray(obj) ? [] : {}
if (obj && typeof obj === 'object') {
if (Array.isArray(obj)) {
// If obj is an array, create a new array and deep clone each element
return obj.map(item => deepClone(item))
} else if (obj && typeof obj === 'object') {
const newObj = {}
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (obj[key] instanceof Date) { // 判断属性值是否为 Date 对象
newObj[key] = new Date(obj[key].getTime()).toISOString() // 直接拷贝引用
} else if (obj[key] && typeof obj[key] === 'object') {
newObj[key] = deepClone(obj[key])
if (obj[key] instanceof Date) {
newObj[key] = new Date(obj[key].getTime()).toISOString()
} else {
newObj[key] = obj[key]
newObj[key] = deepClone(obj[key])
}
}
}
return newObj
} else {
return obj
}
return newObj
}

/**
* 延时
* @param {*} ms
Expand Down

0 comments on commit 570ab5a

Please sign in to comment.