UNPKG

@ministryofjustice/hmpps-digital-prison-reporting-frontend

Version:

The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.

73 lines (69 loc) 1.96 kB
import dayjs from 'dayjs'; import isBetween from '../../_virtual/isBetween.js'; import RelativeDateRange from '../components/_inputs/date-range/types.js'; dayjs.extend(isBetween); const RANGE_CALCULATORS = { [RelativeDateRange.YESTERDAY]: now => ({ startDate: now.subtract(1, 'day'), endDate: now, }), [RelativeDateRange.TOMORROW]: now => ({ startDate: now, endDate: now.add(1, 'day'), }), [RelativeDateRange.LAST_WEEK]: now => ({ startDate: now.subtract(1, 'week'), endDate: now, }), [RelativeDateRange.NEXT_WEEK]: now => ({ startDate: now, endDate: now.add(1, 'week'), }), [RelativeDateRange.LAST_MONTH]: now => ({ startDate: now.subtract(1, 'month'), endDate: now, }), [RelativeDateRange.NEXT_MONTH]: now => ({ startDate: now, endDate: now.add(1, 'month'), }), }; const calcDatesInternal = (range) => { const now = dayjs().startOf('day'); return RANGE_CALCULATORS[range](now); }; const calcDates = (range) => { if (range === RelativeDateRange.NONE) { return null; } return calcDatesInternal(range); }; /* ----------------------------------------- /* ADAPTERS /* ----------------------------------------- /** * Adapter for date inputs (DD/MM/YYYY) */ const calcDatesForUI = (range) => { const result = calcDates(range); if (!result) return null; return { start: result.startDate.format('DD/MM/YYYY'), end: result.endDate.format('DD/MM/YYYY'), }; }; /** * Adapter for filter definitions (YYYY-MM-DD) */ const calcDatesForAPI = (range) => { const result = calcDates(range); if (!result) return null; return { start: result.startDate.format('YYYY-MM-DD'), end: result.endDate.format('YYYY-MM-DD'), }; }; export { calcDates, calcDatesForAPI, calcDatesForUI }; //# sourceMappingURL=durationCalculator.js.map