All files / shared/utils initializeDayjs.ts

100% Statements 16/16
100% Branches 2/2
100% Functions 2/2
100% Lines 16/16

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                      441x   441x 657x 657x 657x 657x 657x 657x 657x 657x   657x   657x                                   657x                                     441x           24x                         24x    
import dayjs from "dayjs";
import isBetween from "dayjs/plugin/isBetween";
import isSameOrAfter from "dayjs/plugin/isSameOrAfter";
import isSameOrBefore from "dayjs/plugin/isSameOrBefore";
import relativeTime from "dayjs/plugin/relativeTime";
import updateLocale from "dayjs/plugin/updateLocale";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
import advancedFormat from "dayjs/plugin/advancedFormat";
import "dayjs/locale/fi";
 
export const TIMEZONE = "Europe/Helsinki";
 
export const initializeDayjs = (): void => {
  dayjs.extend(isBetween);
  dayjs.extend(isSameOrAfter);
  dayjs.extend(isSameOrBefore);
  dayjs.extend(relativeTime);
  dayjs.extend(updateLocale);
  dayjs.extend(utc); // Required by timezone
  dayjs.extend(timezone);
  dayjs.extend(advancedFormat);
 
  dayjs.tz.setDefault(TIMEZONE);
 
  dayjs.updateLocale("en", {
    relativeTime: {
      future: "in %s",
      past: "%s ago",
      s: "a few seconds",
      m: "a minute",
      mm: "%d minutes",
      h: "an hour",
      hh: "%d hours",
      d: "a day",
      dd: "%d days",
      M: "a month",
      MM: "%d months",
      y: "a year",
      yy: "%d years",
    },
  });
 
  dayjs.updateLocale("fi", {
    relativeTime: {
      future: "%s päästä",
      past: "%s sitten",
      s: processRelativeTime,
      m: processRelativeTime,
      mm: processRelativeTime,
      h: processRelativeTime,
      hh: processRelativeTime,
      d: processRelativeTime,
      dd: processRelativeTime,
      M: processRelativeTime,
      MM: processRelativeTime,
      y: processRelativeTime,
      yy: processRelativeTime,
    },
  });
};
 
const processRelativeTime = (
  number: string,
  _withoutSuffix: boolean,
  key: string,
  isFuture: boolean,
): string => {
  const format: Record<string, [string, string]> = {
    s: ["muutama sekunti", "muutaman sekunnin"],
    m: ["minuutti", "minuutin"],
    mm: [number + " minuuttia", number + " minuutin"],
    h: ["tunti", "tunnin"],
    hh: [number + " tuntia", number + " tunnin"],
    d: ["päivä", "päivän"],
    dd: [number + " päivää", number + " päivän"],
    M: ["kuukausi", "kuukauden"],
    MM: [number + " kuukautta", number + " kuukauden"],
    y: ["vuosi", "vuoden"],
    yy: [number + " vuotta", number + " vuoden"],
  };
  return isFuture ? format[key][1] : format[key][0];
};