All files / src formatter.ts

82.35% Statements 14/17
62.5% Branches 5/8
50% Functions 2/4
86.67% Lines 13/15

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 411x   1x             1x                   1x                   1x 3x 3x 3x 3x 3x 3x   1x   1x  
import { format } from "date-fns";
 
class Formatter {
  /**
   * Formats a date to an easy-to-read string.
   *
   * @param date The date to format.
   * @returns A date (ex: "Mon, Mar 1")
   */
  date(date: Date): string {
    return format(date, "ddd, MMM D");
  }
 
  /**
   * Formats a date to an simple time.
   *
   * @param date The date to format.
   * @returns An time (ex: "12:00pm")
   */
  time(date: Date): string {
    return format(date, "hh:mma");
  }
 
  /**
   * Formats hours to the time format.
   *
   * @param hours The number of hours.
   * @returns Hours in the time format (ex: '8:00')
   */
  hours(hours: number): string {
    const isNegative = hours < 0 ? true : false;
    const h: number = Math.floor(hours);
    let m: string = String(((hours % 1) * 60).toFixed(0));
    Eif (m === "0") m = "00";
    Iif (Number(m) < 10 && Number(m) > 0) m = `0${m}`;
    return `${h}:${m}`;
  }
}
 
export default Formatter;