All files index.ts

100% Statements 61/61
81.57% Branches 31/38
100% Functions 4/4
100% Lines 53/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107                                                        1x 1x                 1x 1x   1x 7x 5x 5x     1x   1x 9x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x   8x 8x 8x 8x 8x 8x   8x 6x 5x 4x 3x 2x 1x 8x   1x 2x 2x 2x   1x   1x 1x 1x 1x 1x 1x         1x 3x 3x 1x   2x 2x       1x  
interface Date {
  /**
   * [format 日期格式化]
   * @param  {[type]} format ["YYYY年MM月dd日hh小时mm分ss秒"]
   * @return {[type]}        [string]
   */
  format(format: string): string;
  /**
   * [ago 多少小时前、多少分钟前、多少秒前]
   * @return {[type]} [string]
   */
  ago(format?: string | number): string;
  /**
   * [TZC 解决因时区变更,导致显示服务器时间不准确 time Zone Converter]
   * @param {[type]} timeZone [时区]
   */
  TZC(timeZone: number): Date;
}
 
interface String {
  /**
   * [toHHMMSS 超过分钟以分钟为单位,超过小时以小时为单位]
   * @param  {[type]} format ["123112".toHHMMSS('hh时mm分ss秒')]
   * @return {[type]} [number]
   */
  toHHMMSS(format: string): string;
}
 
Date.prototype.format = function(format){
  const timeFormat = {
    "M+" : this.getMonth()+1, //month
    "d+" : this.getDate(), //day
    "h+" : this.getHours(), //hour
    "m+" : this.getMinutes(), //minute
    "s+" : this.getSeconds(), //second
    "q+" : Math.floor((this.getMonth()+3)/3), //quarter
    "S" : this.getMilliseconds() //millisecond
  }
  Eif(/(y+)/.test(format))
    format = format.replace(RegExp.$1, (this.getFullYear()+"").substring(4 - RegExp.$1.length));
 
  for(const k in timeFormat) {
    if(new RegExp("("+ k +")").test(format)) {
      const str = timeFormat[k as keyof typeof timeFormat] as unknown as string;
      format = format.replace(RegExp.$1, RegExp.$1.length==1 ? str : ("00"+ str).substring((""+ str).length));
    }
  }
  return format;
}
Date.prototype.ago = function(format) {
  if(!format) return '';
  let now=this.getTime(),
    past = typeof format === 'number' && !isNaN(format) ? format : new Date(format).getTime(),
    diffValue = now - past,
    result='',
    minute = 1000 * 60,
    hour = minute * 60,
    day = hour * 24,
    halfamonth = day * 15,
    month = day * 30,
    year = month * 12,
 
    _year = diffValue/year,
    _month =diffValue/month,
    _week =diffValue/(7*day),
    _day =diffValue/day,
    _hour =diffValue/hour,
    _min =diffValue/minute;
 
  if(_year >= 1) result=parseInt(_year.toString()) + "年前";
  else if(_month >= 1) result=parseInt(_month.toString()) + "个月前";
  else if(_week >= 1) result=parseInt(_week.toString()) + "周前";
  else if(_day >= 1) result=parseInt(_day.toString()) + "天前";
  else if(_hour >= 1) result=parseInt(_hour.toString()) + "个小时前";
  else if(_min >= 1) result=parseInt(_min.toString()) + "分钟前";
  else result="刚刚";
  return result;
}
Date.prototype.TZC = function(timeZone) {
  const new_date = new Date(),
    old_date = this.getTime();
    return (isNaN(timeZone)&&!timeZone)? this : new Date(old_date + new_date.getTimezoneOffset() * 60 * 1000 + timeZone * 60 * 60 * 1000);
}
String.prototype.toHHMMSS = function(format) {
  let hour, minute, second, timeFormat;
  const str = parseInt(this.trim())
  hour = parseInt((str/3600).toString())
  minute = parseInt((str/60).toString())
  Eif(minute>=60) minute=minute%60
    second = str % 60;
  timeFormat = {
    "h+":hour,
    "m+":minute,
    "s+":second
  }
  for(let k in timeFormat) {
    Eif(new RegExp("("+ k +")").test(format)) {
      if(RegExp.$1 == "hh" && hour > 99){
        format = format.replace('hh', hour.toString())
      } else {
        const str = timeFormat[k as keyof typeof timeFormat] as unknown as string;
        format = format.replace(RegExp.$1, RegExp.$1.length==1 ? str : ("00"+ str).substring((""+ str).length))
      }
    }
  };
  return format
}