baublet/w8mngr

View on GitHub
api/helpers/getDateRangeWithDefault.ts

Summary

Maintainability
C
7 hrs
Test Coverage
import addYears from "date-fns/addYears";
import subYears from "date-fns/subYears";

import { dayStringFromDate } from "../../shared";
import { Maybe } from "../../shared/types";

export function getDateRangeWithDefault(args?: {
  from?: Maybe<Date>;
  to?: Maybe<Date>;
}) {
  let from = args?.from;
  let to = args?.to;

  if (!from && !to) {
    from = subYears(new Date(), 1);
    to = new Date();
  } else if (!from && to) {
    from = subYears(to, 1);
  } else if (from && !to) {
    to = addYears(from, 1);
  }

  return {
    from: dayStringFromDate(from as Date),
    to: dayStringFromDate(to as Date),
  };
}