Open source personal finance application
fix(date): rename calculateDate to formatDateToStringWithLast and update usages
Ilya Komichevd9fa0ca39dbd
diff --git a/app/components/date/useDateFormats.ts b/app/components/date/useDateFormats.tsindex 9b3bb2b3..fbafa751 100644--- a/app/components/date/useDateFormats.ts+++ b/app/components/date/useDateFormats.ts@@ -7,7 +7,7 @@ import { useGetDateRange } from '~/components/stat/date/useGetDateRange'export function useDateFormats() {const { locale, t } = useI18n()- const { calculateDate } = useGetDateRange(t, locale.value)+ const { formatDateToStringWithLast } = useGetDateRange(t, locale.value)function getDates(periodName: PeriodNameWithAll, date: number) {if (periodName === 'all')@@ -34,14 +34,14 @@ export function useDateFormats() {full: formatByLocale(date, 'dd.MM.yyyy HH:mm', locale.value),month: formatByLocale(date, 'MMM', locale.value),week: formatByLocale(date, 'dd.MM', locale.value),- weekday: `${diff < 2 ? `${calculateDate({ by: 'day', duration: 1, end: date, start: date, type: 'start' })}, ` : ''} ${formatByLocale(date, 'EEEE', locale.value)}`,+ weekday: `${diff < 2 ? `${formatDateToStringWithLast({ by: 'day', duration: 1, end: date, start: date, type: 'start' })}, ` : ''} ${formatByLocale(date, 'EEEE', locale.value)}`,}case 'trnItem':- return calculateDate({ by: 'day', duration: 1, end: date, start: date, type: 'start' })+ return formatDateToStringWithLast({ by: 'day', duration: 1, end: date, start: date, type: 'start' })default:- return calculateDate({ by: 'day', duration: 1, end: date, start: date, type: 'start' })+ return formatDateToStringWithLast({ by: 'day', duration: 1, end: date, start: date, type: 'start' })}}diff --git a/app/components/stat/chart/View.vue b/app/components/stat/chart/View.vueindex 4ea39454..1efcbe78 100644--- a/app/components/stat/chart/View.vue+++ b/app/components/stat/chart/View.vue@@ -13,7 +13,6 @@ import type { PeriodNameWithAll } from '~/components/stat/filter/types'import { formatByLocale } from '~/components/date/utils'import { config, lineConfig } from '~/components/stat/chart/config'import { getLocalAmount } from '~/components/stat/chart/utils'-import { useGetDateRange } from '~/components/stat/date/useGetDateRange'const props = withDefaults(defineProps<{@@ -51,8 +50,7 @@ use([TooltipComponent,])-const { locale, t } = useI18n()-const { getStringDateRange } = useGetDateRange(t, locale.value)+const { locale } = useI18n()const chartRef = ref()function getFormatForChart(periodName: PeriodNameWithAll) {@@ -80,15 +78,9 @@ const option = computed(() => {})data.xAxis.axisLabel.formatter = (date: string) => {- if (props.period === 'day') {- return getStringDateRange({- end: new Date(+date).getTime(),- start: new Date(+date).getTime(),- }, 'day', 1)- }-return formatByLocale(new Date(+date), getFormatForChart(props.period, new Date(+date)), locale.value)}+data.xAxis.axisPointer.label.formatter = ({ value }: { value: string }) => {return formatByLocale(new Date(+value), getFormatForChart(props.period, new Date(+value)), locale.value)}diff --git a/app/components/stat/date/Range.vue b/app/components/stat/date/Range.vueindex 75e6f24c..649dbc3e 100644--- a/app/components/stat/date/Range.vue+++ b/app/components/stat/date/Range.vue@@ -21,7 +21,7 @@ const date = computed(() => {const intervalsBy = isIntervalSelected ? statDate.params.value.intervalsBy : statDate.params.value.rangeByconst rangeDuration = isIntervalSelected ? statDate.params.value.intervalsDuration : statDate.params.value.rangeDuration- return `${getStringDateRange(range.value, intervalsBy, rangeDuration, statDate.params.value.isShowMaxRange)}`+ return `${getStringDateRange(range.value, intervalsBy, rangeDuration)}`})</script>diff --git a/app/components/stat/date/useGetDateRange.ts b/app/components/stat/date/useGetDateRange.tsindex b293d9de..6797d34c 100644--- a/app/components/stat/date/useGetDateRange.ts+++ b/app/components/stat/date/useGetDateRange.ts@@ -85,7 +85,7 @@ export function useGetDateRange(t: (key: string, choice?: number) => string, locreturn type === 'start' ? formatByLocale(start, 'd MMM yyyy', locale) : ` - ${formatByLocale(end, 'd MMM yyyy', locale)}`}- function calculateDate(params: DateFormatParams, isShowMaxRange?: boolean): string {+ function formatDateToStringWithLast(params: DateFormatParams, isShowMaxRange?: boolean): string {const { by, duration, end, start, type } = params// Single duration cases@@ -119,13 +119,11 @@ export function useGetDateRange(t: (key: string, choice?: number) => string, loc// Special case for single day in current year/monthif (by === 'day' && isSameYear(start, today)) {- if (isSameMonth(start, today))- return type === 'start' ? formatByLocale(start, 'd MMM') : ''- return type === 'start' ? formatByLocale(start, 'd MMM') : ''+ return type === 'start' ? formatByLocale(start, 'd MMMM') : ''}}- // Last N periods case+ // Last N periodsconst isCurrentPeriod = (() => {switch (by) {case 'year': return isSameYear(end, today)@@ -147,25 +145,57 @@ export function useGetDateRange(t: (key: string, choice?: number) => string, loc}}- function getStringDateRange(range: Range, by: StatDateParams['rangeBy'], duration: StatDateParams['rangeDuration'], isShowMaxRange: boolean = false) {- const yearStart = calculateDate({+ /**+ * Calculates and formats a date range string based on the provided parameters.+ *+ * - If the duration is 1 day and the start date is in the current year, returns a localized date string for the start date (if `type` is `'start'`).+ * - Otherwise, formats the date range according to the specified granularity (`by`), which can be `'year'`, `'month'`, `'week'`, or `'day'`.+ *+ * @param params+ * - `by`: The granularity of the date range ('year', 'month', 'week', or 'day').+ * - `duration`: The length of the date range.+ * - `start`: The start date of the range.+ * - `type`: The type of date to format ('start' or other).+ * @returns A formatted date range string based on the input parameters.+ */+ function formatDateToString(params: DateFormatParams): string {+ const { by, duration, start, type } = params++ if (duration === 1 && by === 'day' && isSameYear(start, today)) {+ return type === 'start' ? formatByLocale(start, 'd MMMM') : ''+ }++ switch (by) {+ case 'year': return formatYearRange(params)+ case 'month': return formatMonthRange(params)+ case 'week': return formatWeekRange(params)+ case 'day': return formatDayRange(params)+ }+ }++ function getStringDateRange(range: Range, by: StatDateParams['rangeBy'], duration: StatDateParams['rangeDuration']) {+ const yearStart = formatDateToString({by,duration,end: new Date(range.end),start: new Date(range.start),type: 'start',- }, isShowMaxRange)+ })- const yearEnd = calculateDate({+ const yearEnd = formatDateToString({by,duration,end: new Date(range.end),start: new Date(range.start),type: 'end',- }, isShowMaxRange)+ })return `${yearStart}${yearEnd}`}- return { calculateDate, getStringDateRange }+ return {+ formatDateToString,+ formatDateToStringWithLast,+ getStringDateRange,+ }}