// 判断是否是数字
export const isNumber = (val) => {
  const regPos = /^\d+(\.\d+)?$/; // 非负浮点数
  const regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; // 负浮点数
  return regPos.test(val) || regNeg.test(val);
};

// 创造guid
export const createGuid = () => {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
    .replace(/[xy]/g, (c) => {
      // eslint-disable-next-line no-bitwise
      const r = (Math.random() * 16) | 0;
      // eslint-disable-next-line no-bitwise
      const v = c === 'x' ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    })
    .toUpperCase();
};

// 深拷贝
export const deepCopy = (p, c) => {
  // eslint-disable-next-line no-param-reassign
  c = c || {};
  // eslint-disable-next-line no-restricted-syntax
  for (const i in p) {
    if (typeof p[i] === 'object') {
      // eslint-disable-next-line no-param-reassign
      c[i] = p[i].constructor === Array ? [] : {};
      deepCopy(p[i], c[i]);
    } else {
      // eslint-disable-next-line no-param-reassign
      c[i] = p[i];
    }
  }
  return c;
};

// rgba颜色转换
export const hexToRgba = (hex, opacity) => {
  // eslint-disable-next-line no-param-reassign
  hex = hex || '#808080';
  // eslint-disable-next-line no-param-reassign
  opacity = opacity !== undefined ? opacity : 1;
  // eslint-disable-next-line radix
  return `rgba(${parseInt(`0x${hex.slice(1, 3)}`)}, ${parseInt(`0x${hex.slice(3, 5)}`)}, ${parseInt(
    `0x${hex.slice(5, 7)}`,
  )}, ${opacity})`;
};

// 加法运算
export const addNumMethod = (num1, num2) => {
  let sq1;
  let sq2;
  let m;
  try {
    sq1 = num1.toString().split('.')[1].length;
  } catch (e) {
    sq1 = 0;
  }
  try {
    sq2 = num2.toString().split('.')[1].length;
  } catch (e) {
    sq2 = 0;
  }
  // eslint-disable-next-line prefer-const
  m = Math.max(sq1, sq2);
  return (num1 + num2).toFixed(m);
};

// 进制转换
export const hexSwitch = (num, m, n) => {
  const s = `${num}`;
  const result = parseInt(s, m).toString(n);
  return result;
};

// 文本样式
export const textStyle = () => {
  return {
    font: 'bold 11pt Helvetica, Arial, sans-serif',
    stroke: 'whitesmoke',
  };
};
