SOURCE

// 工具类
export default {
  // 根据str字符返回实际显示字符
  getTextPlaceholder(str, defaultStr = '--') {
    if (!str && str !== 0) {
      return defaultStr;
    }
    return str;
  },
  // 数字千分位
  thousandsFormatter(num) {
    return `${this.getTextPlaceholder(num)}`.replace(/(?<!\.\d*)\B(?=(\d{3})+(?!\d))/g, ',');
  },
  // 对象、数组深拷贝
  deepCopy(target) {
    let result;
    if (typeof target === 'object') {
      if (Array.isArray(target)) {
        result = [];
        for (const i of target) {
          result.push(this.deepCopy(i));
        }
      } else if (target === null) {
        result = null;
      } else if (target.constructor === RegExp) {
        result = target;
      } else {
        result = {};
        for (const i of Object.keys(target)) {
          result[i] = this.deepCopy(target[i]);
        }
      }
    } else {
      result = target;
    }
    return result;
  },
  getYearWeek(year, month, day) {
    const firstDate = new Date(year, 0, 1);
    const currentDate = new Date(year, month - 1, day);
    const d = Math.round((currentDate.valueOf() - firstDate.valueOf()) / 86400000);
    const firstDay = firstDate.getDay();
    return Math.floor((firstDay + d) / 7) + 1;
  },
  // 日期格式化
  dateFormatter(date = new Date(), format = 'yyyy/MM/dd hh:mm:ss') {
    const newDate = date instanceof Date ? date : new Date(date);
    let fmt = format || 'yyyy/MM/dd hh:mm:ss';
    const formatMatchYear = fmt.match(/(y+)/);
    if (formatMatchYear) {
      fmt = fmt.replace(formatMatchYear[0], `${newDate.getFullYear()}`.substring(4 - formatMatchYear[0].length));
    }
    const quarter = Math.floor(newDate.getMonth() / 3) + 1;
    const week = this.getYearWeek(newDate.getFullYear(), newDate.getMonth() + 1, newDate.getDate());
    const o = {
      'M+': newDate.getMonth() + 1,
      'd+': newDate.getDate(),
      'h+': newDate.getHours(),
      'm+': newDate.getMinutes(),
      's+': newDate.getSeconds(),
      'q+': `${quarter}`,
      'Q+': `Q${quarter}`,
      'w+': `${week}`,
      'W+': `W${week}`,
      'ww+': `${week}`.padStart(2, '0'), // 个位周补0
      'Www+': `W${week}`.padStart(2, '0') // 个位周补0
    };

    Object.keys(o).map(k => {
      const formatMatch = fmt.match(new RegExp(`(${k})`));
      if (formatMatch) {
        let str = `${o[k]}`;
        fmt = fmt.replace(formatMatch[0], formatMatch[0].length === 1 ? str : `00${str}`.substring(str.length));
      }
    });

    return fmt;
  },
  setCookie(name, value, options = {}) {
    const {expiresDays = null, path = '/', domain = '', secure = false} = options;
    let cookieString = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
    if (expiresDays) {
      const date = new Date();
      date.setTime(date.getTime() + expiresDays * 24 * 60 * 60 * 1000);
      cookieString += `; expires=${date.toUTCString()}`;
    }
    cookieString += `; path=${path}`;
    if (domain) {
      cookieString += `; domain=${domain}`;
    }
    if (secure) {
      cookieString += '; Secure';
    }
    document.cookie = cookieString;
  },
  getCookie(name) {
    const cookieArray = document.cookie.split('; ');
    for (let i = 0; i < cookieArray.length; i++) {
      const [cookieName, cookieValue] = cookieArray[i].split('=');
      if (decodeURIComponent(cookieName) === name) {
        return decodeURIComponent(cookieValue);
      }
    }
    return null;
  },
  clearCookie(name) {
    if (!name) {
      const cookieArray = document.cookie.split('; ');
      for (let i = 0; i < cookieArray.length; i++) {
        const [cookieName] = cookieArray[i].split('=');
        this.setCookie(cookieName, '', {expires: -1});
      }
    }
    this.setCookie(name || 'JSESSIONID', '', {expires: -1});
  }
};
console 命令行工具 X clear

                    
>
console