// 数字添加千分号
const formatNum = function(n) {
  let num1 = n.toString(),
    num2 = num1;
  let len = num1.length;
  // 判断是否有小数
  if (num1.indexOf("-") > -1 && num1.indexOf(".") === -1) {
    len = num1.split("-")[1].length;
    num2 = num1.split("-")[1];
    if (!num2) return num1;
    if (len <= 3) {
      return num1;
    } else {
      let remainder = len % 3;
      if (remainder > 0) {
        // 不是3的整数倍
        return (
          "-" +
          num2.slice(0, remainder) +
          "," +
          num2
            .slice(remainder, len)
            .match(/\d{3}/g)
            .join(",")
        );
      } else {
        // 是3的整数倍
        return (
          "-" +
          num2
            .slice(0, len)
            .match(/\d{3}/g)
            .join(",")
        );
      }
    }
  } else if (num1.indexOf("-") === -1 && num1.indexOf(".") > -1) {
    len = num1.split(".")[0].length;
    let decimals = "." + num1.split(".")[1];
    if (len <= 3) {
      return num1;
    } else {
      let remainder = len % 3;
      if (remainder > 0) {
        // 不是3的整数倍
        return (
          num1.slice(0, remainder) +
          "," +
          num1
            .slice(remainder, len)
            .match(/\d{3}/g)
            .join(",") +
          decimals
        );
      } else {
        // 是3的整数倍
        return (
          num1
            .slice(0, len)
            .match(/\d{3}/g)
            .join(",") + decimals
        );
      }
    }
  } else if (num1.indexOf("-") > -1 && num1.indexOf(".") > -1) {
    len = num1.split("-")[1].split(".")[0].length;
    let decimals = "." + num1.split(".")[1];
    num2 = num1.split("-")[1];
    if (len <= 3) {
      return num1;
    } else {
      let remainder = len % 3;
      if (remainder > 0) {
        // 不是3的整数倍
        return (
          "-" +
          num2.slice(0, remainder) +
          "," +
          num2
            .slice(remainder, len)
            .match(/\d{3}/g)
            .join(",") +
          decimals
        );
      } else {
        // 是3的整数倍
        return (
          "-" +
          num2
            .slice(0, len)
            .match(/\d{3}/g)
            .join(",") +
          decimals
        );
      }
    }
  } else {
    if (len <= 3) {
      return num1;
    } else {
      let remainder = len % 3;
      if (remainder > 0) {
        // 不是3的整数倍
        return (
          num1.slice(0, remainder) +
          "," +
          num1
            .slice(remainder, len)
            .match(/\d{3}/g)
            .join(",")
        );
      } else {
        // 是3的整数倍
        return num1
          .slice(0, len)
          .match(/\d{3}/g)
          .join(",");
      }
    }
  }
};
const formatTime = function(date, fmt) {
  if (!fmt) {
    fmt = "YYYY-mm-dd HH:MM";
  }
  let ret;
  const opt = {
    "Y+": date.getFullYear().toString(), // 年
    "m+": (date.getMonth() + 1).toString(), // 月
    "d+": date.getDate().toString(), // 日
    "H+": date.getHours().toString(), // 时
    "M+": date.getMinutes().toString(), // 分
    "S+": date.getSeconds().toString(), // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  };
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(
        ret[1],
        ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")
      );
    }
  }
  return fmt
};

export { formatNum, formatTime };
