All files getSeasonFromDate.ts

100% Statements 26/26
100% Branches 27/27
100% Functions 3/3
100% Lines 25/25

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 551x                 1x 41x   1x 5x   1x         16x 16x 16x 16x 16x 16x   16x 3x 13x 2x 11x       2x 9x       2x 7x       2x 5x       3x   2x    
import {
  isAfter,
  isBefore,
  subDays,
  subWeeks,
  isSameDay,
  addDays,
} from "date-fns";
 
const isAfterOrSameDay = (date: Date, dateToCompare: Date) =>
  isAfter(date, dateToCompare) || isSameDay(date, dateToCompare);
 
const isBeforeOrSameDay = (date: Date, dateToCompare: Date) =>
  isBefore(date, dateToCompare) || isSameDay(date, dateToCompare);
 
export const getSeasonFromDate = (
  date: Date,
  adventStart: Date,
  easterDate: Date,
) => {
  const CHRISTMAS = new Date(adventStart.getFullYear(), 11, 25);
  const EPIPHANY = new Date(easterDate.getFullYear(), 0, 6);
  const ASH_WEDNESDAY = subDays(easterDate, 46);
  const FIRST_SUNDAY_OF_LENT = addDays(ASH_WEDNESDAY, 4);
  const PALM_SUNDAY = subWeeks(easterDate, 1);
  const PENTECOST = addDays(easterDate, 50);
 
  if (isBefore(date, CHRISTMAS) && isAfterOrSameDay(date, adventStart)) {
    return "advent";
  } else if (isAfterOrSameDay(date, CHRISTMAS) && isBefore(date, EPIPHANY)) {
    return "christmas";
  } else if (
    isAfterOrSameDay(date, ASH_WEDNESDAY) &&
    isBefore(date, FIRST_SUNDAY_OF_LENT)
  ) {
    return "ash-wednesday";
  } else if (
    isBefore(date, PALM_SUNDAY) &&
    isAfterOrSameDay(date, FIRST_SUNDAY_OF_LENT)
  ) {
    return "lent";
  } else if (
    isAfterOrSameDay(date, PALM_SUNDAY) &&
    isBefore(date, easterDate)
  ) {
    return "holy-week";
  } else if (
    isBeforeOrSameDay(date, PENTECOST) &&
    isAfterOrSameDay(date, easterDate)
  ) {
    return "easter";
  }
  return "ordinary";
};