import { FiltersPeriodId } from '../../store/filters/filters.types'; export const MONTHLY: FiltersPeriodId = 1; export const QUARTERLY: FiltersPeriodId = 3; export const YEARLY: FiltersPeriodId = 12; const locale = 'en-US'; function getYear(date) { return `${date.getFullYear()}`; } function getQuarter(date) { const quarter = Math.ceil((date.getMonth() + 1) / QUARTERLY); return `Q${quarter}`; } function getShortMonth(date) { return date.toLocaleString(locale, { month: 'short' }); } function getMonthlyLabel(date) { return `${date.toLocaleString(locale, { month: 'long' })} ${getYear(date)}`; } export function getQuarterlyLabel(date) { return getQuarter(date) + ' ' + date.getFullYear(); } function getYearly(date) { return `${date.getFullYear()}`; } export function getAggregationPeriods() { return [ { id: MONTHLY, value: 'monthly', name: 'Monthly', getLabel: getMonthlyLabel, getChartLabel: getShortMonth }, { id: QUARTERLY, value: 'quarterly', name: 'Quarterly', getLabel: getQuarterlyLabel, getChartLabel: getQuarter }, { id: YEARLY, value: 'yearly', name: 'Yearly', getLabel: getYearly, getChartLabel: getYear }, ]; } function getAggregationPeriod(aggregationPeriodId: FiltersPeriodId) { const period = getAggregationPeriods() .find((p) => p.id === aggregationPeriodId); if (!period) { throw new Error(`${aggregationPeriodId} is not a valid aggregation period id`); } return period; } export function getAggregationPeriodValue(aggregationPeriodId: FiltersPeriodId) { const period = getAggregationPeriod(aggregationPeriodId); return period.value; } export function getAggregationPeriodName(aggregationPeriodId) { const period = getAggregationPeriod(aggregationPeriodId); return period.name; } export function getAggregationLabel(date, aggregationPeriodId) { const period = getAggregationPeriod(aggregationPeriodId); return period.getLabel(date); } export function getAggregationChartLabel(date, aggregationPeriodId) { const period = getAggregationPeriod(aggregationPeriodId); return period.getChartLabel(date); } export default { getAggregationPeriods, getAggregationPeriodName, };