2021-06-24 21:13:06 +03:00
|
|
|
import { subDays, startOfDay, endOfDay } from 'date-fns';
|
2021-12-22 22:08:28 +03:00
|
|
|
import { cond, filter, isEmpty, T } from 'ramda';
|
2022-12-03 14:20:18 +03:00
|
|
|
import { dateOrNull, DateOrString, formatInternational, isBeforeOrEqual, parseISO } from './date';
|
2022-12-03 14:15:36 +03:00
|
|
|
|
2020-12-15 00:58:15 +03:00
|
|
|
export interface DateRange {
|
2021-06-24 21:13:06 +03:00
|
|
|
startDate?: Date | null;
|
|
|
|
endDate?: Date | null;
|
2020-12-15 00:58:15 +03:00
|
|
|
}
|
|
|
|
|
2022-12-03 14:45:25 +03:00
|
|
|
const ALL = 'all';
|
|
|
|
const INTERVAL_TO_STRING_MAP = {
|
2020-12-15 00:58:15 +03:00
|
|
|
today: 'Today',
|
|
|
|
yesterday: 'Yesterday',
|
|
|
|
last7Days: 'Last 7 days',
|
|
|
|
last30Days: 'Last 30 days',
|
|
|
|
last90Days: 'Last 90 days',
|
2021-12-22 22:08:28 +03:00
|
|
|
last180Days: 'Last 180 days',
|
2020-12-15 00:58:15 +03:00
|
|
|
last365Days: 'Last 365 days',
|
2022-12-03 14:45:25 +03:00
|
|
|
[ALL]: undefined,
|
2020-12-15 00:58:15 +03:00
|
|
|
};
|
|
|
|
|
2022-12-03 14:45:25 +03:00
|
|
|
export type DateInterval = keyof typeof INTERVAL_TO_STRING_MAP;
|
|
|
|
|
|
|
|
const INTERVALS = Object.keys(INTERVAL_TO_STRING_MAP) as DateInterval[];
|
|
|
|
|
|
|
|
export const dateRangeIsEmpty = (dateRange?: DateRange): boolean => dateRange === undefined
|
|
|
|
|| isEmpty(filter(Boolean, dateRange as any));
|
|
|
|
|
|
|
|
export const rangeIsInterval = (range?: DateRange | DateInterval): range is DateInterval =>
|
|
|
|
typeof range === 'string' && INTERVALS.includes(range);
|
|
|
|
|
|
|
|
export const DATE_INTERVALS = INTERVALS.filter((value) => value !== ALL) as DateInterval[];
|
2021-03-06 18:54:43 +03:00
|
|
|
|
2022-11-26 11:11:46 +03:00
|
|
|
export const datesToDateRange = (startDate?: string, endDate?: string): DateRange => ({
|
|
|
|
startDate: dateOrNull(startDate),
|
|
|
|
endDate: dateOrNull(endDate),
|
|
|
|
});
|
|
|
|
|
2020-12-15 00:58:15 +03:00
|
|
|
const dateRangeToString = (range?: DateRange): string | undefined => {
|
|
|
|
if (!range || dateRangeIsEmpty(range)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range.startDate && !range.endDate) {
|
|
|
|
return `Since ${formatInternational(range.startDate)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!range.startDate && range.endDate) {
|
|
|
|
return `Until ${formatInternational(range.endDate)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${formatInternational(range.startDate)} - ${formatInternational(range.endDate)}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const rangeOrIntervalToString = (range?: DateRange | DateInterval): string | undefined => {
|
2022-12-03 14:45:25 +03:00
|
|
|
if (!range || range === ALL) {
|
2020-12-15 00:58:15 +03:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rangeIsInterval(range)) {
|
|
|
|
return dateRangeToString(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
return INTERVAL_TO_STRING_MAP[range];
|
|
|
|
};
|
|
|
|
|
2021-06-24 21:13:06 +03:00
|
|
|
const startOfDaysAgo = (daysAgo: number) => startOfDay(subDays(new Date(), daysAgo));
|
2021-09-12 11:16:05 +03:00
|
|
|
const endingToday = (startDate: Date): DateRange => ({ startDate, endDate: endOfDay(new Date()) });
|
2021-06-24 21:13:06 +03:00
|
|
|
|
2020-12-15 00:58:15 +03:00
|
|
|
export const intervalToDateRange = (dateInterval?: DateInterval): DateRange => {
|
2022-12-03 14:45:25 +03:00
|
|
|
if (!dateInterval || dateInterval === ALL) {
|
2020-12-15 00:58:15 +03:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (dateInterval) {
|
|
|
|
case 'today':
|
2021-09-12 11:16:05 +03:00
|
|
|
return endingToday(startOfDay(new Date()));
|
2020-12-15 00:58:15 +03:00
|
|
|
case 'yesterday':
|
2021-06-24 21:13:06 +03:00
|
|
|
return { startDate: startOfDaysAgo(1), endDate: endOfDay(subDays(new Date(), 1)) };
|
2020-12-15 00:58:15 +03:00
|
|
|
case 'last7Days':
|
2021-09-12 11:16:05 +03:00
|
|
|
return endingToday(startOfDaysAgo(7));
|
2020-12-15 00:58:15 +03:00
|
|
|
case 'last30Days':
|
2021-09-12 11:16:05 +03:00
|
|
|
return endingToday(startOfDaysAgo(30));
|
2020-12-15 00:58:15 +03:00
|
|
|
case 'last90Days':
|
2021-09-12 11:16:05 +03:00
|
|
|
return endingToday(startOfDaysAgo(90));
|
2021-12-22 22:08:28 +03:00
|
|
|
case 'last180Days':
|
2021-09-12 11:16:05 +03:00
|
|
|
return endingToday(startOfDaysAgo(180));
|
2020-12-15 00:58:15 +03:00
|
|
|
case 'last365Days':
|
2021-09-12 11:16:05 +03:00
|
|
|
return endingToday(startOfDaysAgo(365));
|
2020-12-15 00:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
};
|
2021-12-22 22:08:28 +03:00
|
|
|
|
|
|
|
export const dateToMatchingInterval = (date: DateOrString): DateInterval => {
|
|
|
|
const theDate: Date = parseISO(date);
|
|
|
|
|
|
|
|
return cond<never, DateInterval>([
|
2022-03-26 14:17:42 +03:00
|
|
|
[() => isBeforeOrEqual(startOfDay(new Date()), theDate), () => 'today'],
|
|
|
|
[() => isBeforeOrEqual(startOfDaysAgo(1), theDate), () => 'yesterday'],
|
|
|
|
[() => isBeforeOrEqual(startOfDaysAgo(7), theDate), () => 'last7Days'],
|
|
|
|
[() => isBeforeOrEqual(startOfDaysAgo(30), theDate), () => 'last30Days'],
|
|
|
|
[() => isBeforeOrEqual(startOfDaysAgo(90), theDate), () => 'last90Days'],
|
|
|
|
[() => isBeforeOrEqual(startOfDaysAgo(180), theDate), () => 'last180Days'],
|
|
|
|
[() => isBeforeOrEqual(startOfDaysAgo(365), theDate), () => 'last365Days'],
|
2022-12-03 14:45:25 +03:00
|
|
|
[T, () => ALL],
|
2021-12-22 22:08:28 +03:00
|
|
|
])();
|
|
|
|
};
|
2022-12-03 14:15:36 +03:00
|
|
|
|
|
|
|
export const toDateRange = (rangeOrInterval: DateRange | DateInterval): DateRange => {
|
|
|
|
if (rangeIsInterval(rangeOrInterval)) {
|
|
|
|
return intervalToDateRange(rangeOrInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rangeOrInterval;
|
|
|
|
};
|