index.js 1.9 KB
export const getImgUrl = function(src, width = 300, height = 300, mode = 2) {
  return src ? src.replace(/(\{width}|\{height}|\{mode})/g, function($0) {
      const dict = {
        '{width}': width,
        '{height}': height,
        '{mode}': mode || 2
      };

      return dict[$0];
    }).replace(/https?:/, '') : '';
};


export const replaceHttp = function(src) {
  return src.replace(/https?:/, '');
};


export const debounce = (idle, action) => { // 函数去抖动,超过一定时间才会执行,如果周期内触发,充值计时器
  let last;

  return function() {
    let args = arguments;

    if (last) {
      clearTimeout(last);
    }
    last = setTimeout(() => {
      action.apply(this, args);
    }, idle);
  };
};


export const throttle = (delay, action) => { // 函数节流器,定义函数执行间隔,按频率触发函数
  let last = 0;

  return function() {
    let args = arguments;
    let curr = +new Date();

    if (curr - last > delay) {
      action.apply(this, args);
      last = curr;
    }
  };
};

/** 
 * 时间戳转化为年 月 日 时 分 秒 
 * time: 传入时间戳
 * format:返回格式,支持自定义,但参数必须与formateArr里保持一致 
 * formatTimeByDefined(1488481383,'Y/M/D h:m:s') => 2017/03/03 03:03:03
*/

export const formatTimeByDefined = function (time, format) {
  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(time * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}