Newer
Older
xc-business-system / src / utils / deepCopy.ts
const isType = function (type: any) {
  return function (arg: any) {
    return Object.prototype.toString.call(arg) === `[object ${type}]`
  }
}

export const isFunction = isType('Function')

export const isObject = isType('Object')

export const isString = isType('String')

export const isArray = isType('Array')

export const isDate = isType('Date')

// 深拷贝
export function deepCopy(param: any): any {
  if (!Array.isArray(param) && !isObject(param)) {
    return param
  }
  else if (Array.isArray(param)) {
    return param.map(() => deepCopy(param))
  }
  else if (isObject(param)) {
    const result = {} as any
    Object.keys(param).forEach((key) => {
      result[key] = deepCopy(param[key])
    })
    return result
  }
}