| |
| |
| |
| import { |
| differenceInDays, |
| endOfYear, |
| format, |
| getYear, |
| isSameMonth, |
| isSameYear, |
| isValid, |
| parseISO, |
| startOfYear, |
| } from "date-fns"; |
|
|
| |
| |
| |
| |
| function parseDate(dateStr: string): Date | null { |
| |
| const isoDate = parseISO(dateStr); |
| if (isValid(isoDate)) { |
| return isoDate; |
| } |
|
|
| |
| const fallbackDate = new Date(dateStr); |
| return isValid(fallbackDate) ? fallbackDate : null; |
| } |
|
|
| |
| |
| |
| function isApproxFullYear(fromDate: Date, toDate: Date): boolean { |
| const fromYearStart = startOfYear(fromDate); |
| const toYearEnd = endOfYear(toDate); |
|
|
| |
| const daysFromStart = differenceInDays(fromDate, fromYearStart); |
| const daysToEnd = differenceInDays(toYearEnd, toDate); |
|
|
| return daysFromStart <= 10 && daysToEnd <= 10; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function generateArtifactDescription(from: string, to: string): string { |
| const fromDate = parseDate(from); |
| const toDate = parseDate(to); |
|
|
| |
| if (!fromDate || !toDate) { |
| return `${from} to ${to}`; |
| } |
|
|
| const fromYear = getYear(fromDate); |
| const toYear = getYear(toDate); |
|
|
| |
| if (isSameYear(fromDate, toDate) && isSameMonth(fromDate, toDate)) { |
| return format(fromDate, "MMM yyyy"); |
| } |
|
|
| |
| if (isSameYear(fromDate, toDate)) { |
| |
| if (isApproxFullYear(fromDate, toDate)) { |
| return `${fromYear}`; |
| } |
| return `${format(fromDate, "MMM")}-${format(toDate, "MMM")} ${fromYear}`; |
| } |
|
|
| |
| |
| if (toYear === fromYear + 1 && isApproxFullYear(fromDate, toDate)) { |
| return `${toYear}`; |
| } |
|
|
| |
| return `${fromYear}-${toYear}`; |
| } |
|
|