/*
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-12-05 09:56:10
 * @LastEditors: 无尘
 * @LastEditTime: 2020-10-22 17:07:44
 */

/**
 * @description: 防抖
 * @param {Function} fn
 * @param {Number} delays
 * @returns {Function}
 * @author: 无尘
 */
export function _debounce(fn, delays) {
  let delay = delays || 200;
  let timer;
  // console.log(fn)
  return function() {
    let that = this;
    let args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(function() {
      timer = null;
      fn.apply(that, args);
    }, delay);
  };
}
/**
 * @description: 节流
 * @param {Function} fn
 * @param {Number} intervals
 * @returns {Function}
 * @author: 无尘
 */
export function _throttle(fn, intervals) {
  let last;
  let timer;
  let interval = intervals || 200;
  return function() {
    let that = this;
    let args = arguments;
    let now = +new Date();
    if (last && now - last < interval) {
      clearTimeout(timer);
      timer = setTimeout(function() {
        last = now;
        fn.apply(that, args);
      }, interval);
    } else {
      last = now;
      fn.apply(that, args);
    }
  };
}

/**
 * @description: 手机号格式化
 * @param {String} phone
 * @returns {String}
 * @author: 无尘
 */
export function formatPhone(phone) {
  phone = phone.toString();
  return phone.substr(0, 3) + '****' + phone.substr(7, 11);
}

/**
 * @description: 时间戳格式化
 * @param {Number} num
 * @author: 无尘
 */

function formatDig(num) {
  return num > 9 ? '' + num : '0' + num;
}
export function formatDate(time) {
  let now = new Date(time);
  let year = now.getFullYear();
  let month = now.getMonth() + 1;
  let date = now.getDate();
  let hour = now.getHours();
  let minute = now.getMinutes();
  let second = now.getSeconds();
  let data = year + '-' + formatDig(month) + '-' + formatDig(date) + ' ' + formatDig(hour) + ':' + formatDig(minute) + ':' + formatDig(second);
  return data;
}

/**
 * @description: 千位分割
 * @param {Number} num
 * @returns {String}
 * @author: 无尘
 */
export function formatNum(num) {
  let number = num.toString().split('.'); // 分隔小数点
  let dot = '0.' + (number[1] || 0);
  let reg = /\d{1,3}(?=(\d{3})+$)/g;
  return (
    (number[0] + '').replace(reg, '$&,') +
    '.' +
    Number(dot)
      .toFixed(2)
      .toString()
      .split('.')[1]
  );
}

/**
 * @description: 表情过滤
 * @param {String} str
 * @author: 无尘
 */
// eslint-disable-next-line
export function emojiToStr(str) {
  if (!String(str) || String(str) == 'null') {
    return '';
  }
  str = String(str).replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]|[\uD800-\uDBFF]|[\uDC00-\uDFFF]|[^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n]/g, '');
  return unescape(escape(str).replace(/\%uD.{3}/g, ''));
}
