/**
 * @description 按照需要写入 必要可以注入全局
 */

interface enumObj {
  key: string
  value: number
}

/**
 * @description 将枚举转为数组
 * @param objEnum 枚举对象
 * @returns 转化后的数组
 */
export function formatEnum(objEnum: any): Array<enumObj> {
  const array: Array<enumObj> = new Array<enumObj>()
  for (const key in objEnum) {
    if (!isNaNModified(key)) continue
    const tempEnumObj: enumObj = {
      key,
      value: objEnum[key] as number
    }
    array.push(tempEnumObj)
  }
  return array
}

export function isNaNModified(inputStr: string) {
  const numericRepr = parseFloat(inputStr)
  return isNaN(numericRepr) || numericRepr.toString().length != inputStr.length
}

export function formatDateTime(date,fmt) {
  const o = {
      "M+": date.getMonth() + 1,
      "d+": date.getDate(),
      "H+": date.getHours(),
      "m+": date.getMinutes(),
      "s+": date.getSeconds(),
      "q+": Math.floor((date.getMonth() + 3) / 3),
      "S": date.getMilliseconds()
  };
  let year = date.getFullYear().toString();
  year = year.length >= 4 ? year : '0000'.substr(0, 4 - year.length) + year;

  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (year + "").substr(4 - RegExp.$1.length));
  for (const k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}