All files / src/shared/GiveExportModal utils.ts

97.67% Statements 42/43
81.39% Branches 35/43
100% Functions 4/4
100% Lines 42/42

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          27x 8x   8x 16x   16x             16x     8x 8x   8x             94x   94x 15x 15x   7x 8x 8x             94x   27x   27x                     12x   12x 1x 1x     12x 4x 4x 4x           12x 2x 2x 2x 2x           12x 1x 1x 1x           12x 3x         9x 1x 1x             12x    
import { RangeValue } from "@shared/DateRangePicker/DateRangePicker.types";
import moment from "moment";
import { formatInTimeZone } from "date-fns-tz";
import { startOfMonth, endOfMonth, subDays, subMonths } from "date-fns";
 
export const formatDescription = (value: RangeValue): string => {
  Iif (!value.startDate || !value.endDate) return "";
 
  const formatWithSuffix = (date: moment.Moment) => {
    const day = date.date();
    const suffix =
      day % 10 === 1 && day !== 11
        ? "st"
        : day % 10 === 2 && day !== 12
        ? "nd"
        : day % 10 === 3 && day !== 13
        ? "rd"
        : "th";
    return `${day}${suffix} ${date.format("MMMM YYYY")}`;
  };
 
  const start = moment(value.startDate);
  const end = moment(value.endDate);
 
  return `${formatWithSuffix(start)} - ${formatWithSuffix(end)}`;
};
 
export function flattenErrors(
  errors: any,
  path: string[] = [],
): { path: string; message: string }[] {
  let result: { path: string; message: string }[] = [];
 
  for (const key in errors) {
    const val = errors[key];
    if (val && typeof val === "object" && !("message" in val)) {
      // go deeper
      result = result.concat(flattenErrors(val, [...path, key]));
    } else Eif (val?.message) {
      result.push({
        path: [...path, key].join("."),
        message: val.message,
      });
    }
  }
 
  return result;
}
export const dateFormat = "MM_dd_yyyy";
 
export const getDateRangeString = ({
  dateStringRep,
  timeZone = "",
  startDate,
  endDate,
}: {
  startDate?: Date;
  endDate?: Date;
  timeZone?: string;
  dateStringRep?: "All" | "7" | "month" | "custom" | string;
}) => {
  let dateRange = "";
 
  if (dateStringRep === "All") {
    const today = new Date();
    dateRange = `until-${formatInTimeZone(today, timeZone, dateFormat)}`;
  }
 
  if (dateStringRep === "7") {
    const today = new Date();
    const from = subDays(today, 6);
    dateRange = `${formatInTimeZone(
      from,
      timeZone,
      dateFormat,
    )}_to_${formatInTimeZone(today, timeZone, dateFormat)}`;
  }
  if (dateStringRep === "month") {
    const today = new Date();
    const firstDay = startOfMonth(subMonths(today, 1));
    const lastDay = endOfMonth(subMonths(today, 1));
    dateRange = `${formatInTimeZone(
      firstDay,
      timeZone,
      dateFormat,
    )}_to_${formatInTimeZone(lastDay, timeZone, dateFormat)}`;
  }
  if (dateStringRep === "current_month") {
    const today = new Date();
    const firstDay = startOfMonth(today);
    dateRange = `${formatInTimeZone(
      firstDay,
      timeZone,
      dateFormat,
    )}_to_${formatInTimeZone(today, timeZone, dateFormat)}`;
  }
  if (dateStringRep === "custom" && startDate && endDate) {
    dateRange = `${formatInTimeZone(
      startDate,
      timeZone,
      dateFormat,
    )}_to_${formatInTimeZone(endDate, timeZone, dateFormat)}`;
  } else if (dateStringRep === "custom" && (!startDate || !endDate)) {
    const today = new Date();
    dateRange = `${formatInTimeZone(
      today,
      timeZone,
      dateFormat,
    )}_to_${formatInTimeZone(today, timeZone, dateFormat)}`;
  }
 
  return dateRange;
};