All files shleemy.ts

100% Statements 57/57
98.41% Branches 62/63
100% Functions 26/26
100% Lines 56/56

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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 2301x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x     1x       37x 37x 37x 37x           37x           37x             34x       33x       36x       35x       36x                 35x       36x                   35x       36x                     35x       36x                     35x       40x                     39x         30x 210x     138x     30x     1x       30x   37x       15x                   37x       15x                     36x 6x         30x 30x           30x           1x       1x 74x             1x                       37x              
export enum TimeIntervalLabel {
  SECONDS = "seconds",
  MINUTES = "minutes",
  HOURS = "hours",
  DAYS = "days",
  WEEKS = "weeks",
  MONTHS = "months",
  YEARS = "years",
}
 
enum IntervalValues {
  MILLISECONDS = 1000,
  SECONDS = 60,
  DAYS = 24,
  WEEKS = 7,
  MONTHS = 30,
  YEARS = 365,
}
 
export class ShleemyInterval {
  readonly diff: number;
  readonly tense: "future" | "past" | "present";
  constructor(
    readonly first: Date,
    readonly toDate: Date,
    readonly rounding: "ceil" | "floor" = "floor",
    private readonly humanReadable?: {
      future?: (value: number, interval: TimeIntervalLabel) => string;
      past?: (value: number, interval: TimeIntervalLabel) => string;
      present?: () => string;
    },
  ) {
    this.tense =
      first.getTime() < toDate.getTime()
        ? "past"
        : first.getTime() === toDate.getTime()
        ? "present"
        : "future";
    this.diff =
      this.tense === "future"
        ? Math.abs(first.getTime() - toDate.getTime())
        : Math.abs(first.getTime() - toDate.getTime());
  }
 
  get seconds(): number {
    return this.diff / IntervalValues.MILLISECONDS;
  }
 
  get roundedSeconds(): number {
    return Math[this.rounding](this.seconds);
  }
 
  get minutes(): number {
    return this.diff / (IntervalValues.MILLISECONDS * IntervalValues.SECONDS);
  }
 
  get roundedMinutes(): number {
    return Math[this.rounding](this.minutes);
  }
 
  get hours(): number {
    return (
      this.diff /
      (IntervalValues.MILLISECONDS *
        IntervalValues.SECONDS *
        IntervalValues.SECONDS)
    );
  }
 
  get roundedHours(): number {
    return Math[this.rounding](this.hours);
  }
 
  get days(): number {
    return (
      this.diff /
      (IntervalValues.MILLISECONDS *
        IntervalValues.SECONDS *
        IntervalValues.SECONDS) /
      IntervalValues.DAYS
    );
  }
 
  get roundedDays(): number {
    return Math[this.rounding](this.days);
  }
 
  get weeks(): number {
    return (
      this.diff /
      (IntervalValues.MILLISECONDS *
        IntervalValues.SECONDS *
        IntervalValues.SECONDS) /
      IntervalValues.DAYS /
      IntervalValues.WEEKS
    );
  }
 
  get roundedWeeks(): number {
    return Math[this.rounding](this.weeks);
  }
 
  get months(): number {
    return (
      this.diff /
      (IntervalValues.MILLISECONDS *
        IntervalValues.SECONDS *
        IntervalValues.SECONDS) /
      IntervalValues.DAYS /
      IntervalValues.MONTHS
    );
  }
 
  get roundedMonths(): number {
    return Math[this.rounding](this.months);
  }
 
  get years(): number {
    return (
      this.diff /
      (IntervalValues.MILLISECONDS *
        IntervalValues.SECONDS *
        IntervalValues.SECONDS) /
      IntervalValues.DAYS /
      IntervalValues.YEARS
    );
  }
 
  get roundedYears(): number {
    return Math[this.rounding](this.years);
  }
 
  get nearestInterval(): TimeIntervalLabel {
    let value;
    Object.values(TimeIntervalLabel).forEach(interval => {
      if (
        this[`rounded${interval[0].toUpperCase() + interval.substring(1)}`] > 0
      ) {
        value = interval;
      }
    });
    return value;
  }
 
  static pluralInterval = (
    value: number,
    interval: TimeIntervalLabel,
  ): string =>
    value === 1 ? interval.substring(0, interval.length - 1) : interval;
 
  private toFutureHumanReadable = (
    value: number,
    interval: TimeIntervalLabel,
  ): string =>
    this.humanReadable && this.humanReadable.future
      ? this.humanReadable.future(value, interval)
      : `in ${
          value === 1
            ? interval === TimeIntervalLabel.HOURS
              ? "an"
              : "a"
            : value
        } ${ShleemyInterval.pluralInterval(value, interval)}`;
 
  private toPastHumanReadable = (
    value: number,
    interval: TimeIntervalLabel,
  ): string =>
    this.humanReadable && this.humanReadable.past
      ? this.humanReadable.past(value, interval)
      : `${
          value === 1
            ? interval === TimeIntervalLabel.HOURS
              ? "an"
              : "a"
            : value
        } ${ShleemyInterval.pluralInterval(value, interval)} ago`;
 
  get forHumans(): string {
    if (this.diff <= IntervalValues.MILLISECONDS || this.tense === "present") {
      return this?.humanReadable?.present
        ? this.humanReadable.present()
        : "just now";
    }
 
    const fullIntervalName = this.nearestInterval;
    const value = this[
      `rounded${
        fullIntervalName[0].toUpperCase() + fullIntervalName.substring(1)
      }`
    ];
 
    return this.tense === "future"
      ? this.toFutureHumanReadable(value, fullIntervalName)
      : this.toPastHumanReadable(value, fullIntervalName);
  }
 
  toString(): string {
    return this.forHumans;
  }
}
 
const resolveDate = (date: string | Date) =>
  typeof date === "string" ? new Date(date) : date;
 
/**
 * Factory to build ShleemyInterval
 * @param date
 * @param options
 */
export const shleemy = (
  date: string | Date,
  options?: {
    toDate?: Date | string;
    rounding?: "ceil" | "floor";
    humanReadable?: {
      future?: (value: number, interval: TimeIntervalLabel) => string;
      past?: (value: number, interval: TimeIntervalLabel) => string;
      present?: () => string;
    };
  },
): ShleemyInterval => {
  return new ShleemyInterval(
    resolveDate(date),
    resolveDate(options?.toDate || new Date()),
    options?.rounding,
    options?.humanReadable,
  );
};