Repository: iamkun/dayjs
File: src/index.js (JavaScript)

Code before:
      case C.W:
        result = diff / C.MILLISECONDS_A_WEEK
        break
      case C.D:
        result = diff / C.MILLISECONDS_A_DAY
        break
      case C.S:
        result = diff / C.MILLISECONDS_A_SECOND
        break
      default: // milliseconds
        result = diff
    }
    return float ? result : Utils.absFloor(result)
  }

  fromNow(input) {
    const P = [{ l: C.S, v: C.MILLISECONDS_A_SECOND }, { l: C.M, v: C.MILLISECONDS_A_SECOND * 60 },
      { l: C.H, v: C.MILLISECONDS_A_HOUR }, { l: C.D, v: C.MILLISECONDS_A_DAY },
      { l: C.W, v: C.MILLISECONDS_A_WEEK }, { l: C.M, v: C.MILLISECONDS_A_WEEK * 4 },
      { l: C.Q, v: C.MILLISECONDS_A_WEEK * 12 }, { l: C.Y, v: C.MILLISECONDS_A_WEEK * 4 * 12 }]

    const result = input.diff(this, `${C.MS}s`)
    const resabs = Math.abs(result)
    let out = ''
    for (let i = 0; i < P.length; i += 1) {
      if (resabs >= P[i].v) out = `${Utils.absFloor(resabs / P[i].v)} ${P[i].l}${(resabs / P[i].v !== 1) ? 's' : ''}`
    }
    if (!result) return 'just now'
    return result > 0 ? `in ${out}` : `${out} ago`
  }

  daysInMonth() {
    return this.endOf(C.M).$D
  }

  clone() {
    return new Dayjs(this)
  }

  toDate() {
    return new Date(this.$d)
  }

  toArray() {
    return [
      this.$y,
      this.$M,
      this.$D,
      this.$H,
      this.$m,
      this.$s,

Review comment (suggestion):
Comment to this part:
`(resabs / P[i].v !== 1) ? 's' : ''`
Probably this is equal to:
`resabs !== P[i].v ? 's': ''`
without division it must be a bit faster :) 

Code after:
  }

  subtract(number, string) {
    return this.add(number * -1, string)
  }


  format(formatStr, localeObject) {
    const str = formatStr || C.FORMAT_DEFAULT
    const zoneStr = Utils.padZoneStr(this.$d.getTimezoneOffset())
    const locale = localeObject || this.$locale()
    const { weekdays, months } = locale
    return str.replace(C.REGEX_FORMAT, (match) => {
      if (match.indexOf('[') > -1) return match.replace(/\[|\]/g, '')
      switch (match) {
        case 'YY':
          return String(this.$y).slice(-2)
        case 'YYYY':
          return String(this.$y)
        case 'M':
          return String(this.$M + 1)
        case 'MM':
          return Utils.padStart(this.$M + 1, 2, '0')
        case 'MMM':
          return months[this.$M].slice(0, 3)
        case 'MMMM':
          return months[this.$M]
        case 'D':
          return String(this.$D)
        case 'DD':
          return Utils.padStart(this.$D, 2, '0')
        case 'd':
          return String(this.$W)
        case 'dddd':
          return weekdays[this.$W]
        case 'H':
          return String(this.$H)
        case 'HH':
          return Utils.padStart(this.$H, 2, '0')
        case 'h':
        case 'hh':
          if (this.$H === 0) return 12
          return Utils.padStart(this.$H < 13 ? this.$H : this.$H - 12, match === 'hh' ? 2 : 1, '0')
        case 'a':
          return this.$H < 12 ? 'am' : 'pm'
        case 'A':
          return this.$H < 12 ? 'AM' : 'PM'
        case 'm':
          return String(this.$m)
        case 'mm':
          return Utils.padStart(this.$m, 2, '0')