//  深拷贝对象
export function copyObj(obj) {
  if (!isObject(obj)) {
    throw new Error('obj 不是一个对象！')
  }

  const isArray = Array.isArray(obj)
  const cloneObj = isArray ? [] : {}
  for (const key in obj) {
    cloneObj[key] = isObject(obj[key]) ? copyObj(obj[key]) : obj[key]
  }

  return cloneObj
}

// 赋值对象
export function assignObj(obj1, obj2) {
  for (const key in obj1, obj2) {
    if (isObject(obj2[key])) {
      assignObj(obj1[key], obj2[key])
    } else {
      obj1[key] = obj2[key]
    }
  }
}

export function isObject(o) {
  return (typeof o === 'object' || typeof o === 'function') && o !== null
}

//转义
export function HTMLDecode(str){
  var s = ''
        if (str.length == 0) return ''
        s = str.replace(/&lt;/g, '<')
        s = s.replace(/&amp;/g, '&')
        s = s.replace(/&gt;/g, '>')
        s = s.replace(/&#39;/g, "\'")
        s = s.replace(/&quot;/g, '"')
        return s
}

export function HTMLEncode(str){
    var s = ''
    if (str.length == 0) return ''
    s = str.replace(/&/g, '&amp;')
    s = s.replace(/</g, '&lt;')
    s = s.replace(/>/g, '&gt;')
    s = s.replace(/\'/g, '&#39;')
    s = s.replace(/\"/g, '&quot;')
    return s
}
//正则
export function dealInputVal(value) {
  value = value.replace(/^0*(0\.|[1-9])/, "$1");
  value = value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
  value = value.replace(/^\./g, ""); //验证第一个字符是数字而不是字符
  value = value.replace(/\.{1,}/g, "."); //只保留第一个.清除多余的
  value = value
    .replace(".", "$#$")
    .replace(/\./g, "")
    .replace("$#$", ".");
    value = value.replace(/^()*(\d*)\.(\d\d).*$/, "$1$2.$3"); //只能输入两个小数
    value =
      value.indexOf(".") > 0
        ? value.split(".")[0].substring(0, 10) + "." + value.split(".")[1]
        : value.substring(0, 10);
    return value;
  }