// 时间格式化
export function timeList(timestamp) {
  var date = new Date(timestamp)
  var Y = date.getFullYear() + '-'
  var M = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) + '-' : (date.getMonth() + 1) + '-'
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  var h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':'
  var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
  var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
  return Y + M + D + " " + h + m + s
}

export function timeDate(timestamp) {
  var date = new Date(timestamp)
  var Y = date.getFullYear() + '-'
  var M = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) + '-' : (date.getMonth() + 1) + '-'
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  return Y + M + D
}

export function timeMd(timestamp) {
  var date = new Date(timestamp)
  var M = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) + '-' : (date.getMonth() + 1) + '-'
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  return  M + D
}

export function timeHms(timestamp) {
  var date = new Date(timestamp)
  var h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':'
  var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
  var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
  return h + m + s
}

export function debounce (fn, interval) {
  var timer;
  var gapTime = interval || 1000;//间隔时间，如果interval不传，则默认1000ms
  return function() {
    clearTimeout(timer);
    var context = this;
    var args = arguments;//保存此处的arguments，因为setTimeout是全局的，arguments不是防抖函数需要的。
    timer = setTimeout(function() {
      fn.call(context,args);
    }, gapTime);
  };
}

//使用递归的方式实现数组、对象的深拷贝

export function deepClone (obj) {

  let objClone = Array.isArray(obj) ? [] : {};

  if (obj && typeof obj === "object") {

    for (var key in obj) {

      if (obj.hasOwnProperty(key)) {

        //判断ojb子元素是否为对象，如果是，递归复制

        if (obj[key] && typeof obj[key] === "object") {

          objClone[key] = deepClone(obj[key]);

        } else {

          //如果不是，简单复制

          objClone[key] = obj[key];

        }

      }

    }

  }

  return objClone;

};

