mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 17:24:10 +00:00
Now that the legacy v1 frontend (commit 064bdf53) is gone, the v2 directory is the only frontend and the v2 suffix is just noise. Renames resources/scripts-v2 to resources/scripts via git mv (so git records the move as renames, preserving blame and log --follow), then bulk-rewrites the 152 files that imported via @v2/... to use @/scripts/... instead. The existing @ alias (resources/) covers the new path with no extra config needed.
Drops the now-unused @v2 alias from vite.config.js and points the laravel-vite-plugin entry at resources/scripts/main.ts. Updates the only blade reference (resources/views/app.blade.php) to match. The package.json test script (eslint ./resources/scripts) automatically targets the right place after the rename without any edit.
Verified: npm run build exits clean and the Vite warning lines now reference resources/scripts/plugins/i18n.ts, confirming every import resolved through the new path. git log --follow on any moved file walks back through its scripts-v2 history.
106 lines
2.6 KiB
TypeScript
106 lines
2.6 KiB
TypeScript
import { format, formatDistanceToNow, parseISO, isValid } from 'date-fns'
|
|
import type { Locale } from 'date-fns'
|
|
|
|
/**
|
|
* Default date format used across the application.
|
|
*/
|
|
export const DEFAULT_DATE_FORMAT = 'yyyy-MM-dd'
|
|
|
|
/**
|
|
* Default datetime format used across the application.
|
|
*/
|
|
export const DEFAULT_DATETIME_FORMAT = 'yyyy-MM-dd HH:mm:ss'
|
|
|
|
/**
|
|
* Format a date value into a string using the given format pattern.
|
|
*
|
|
* @param date - A Date object, ISO string, or timestamp
|
|
* @param formatStr - A date-fns format pattern (default: 'yyyy-MM-dd')
|
|
* @param options - Optional locale for localized formatting
|
|
* @returns Formatted date string, or empty string if invalid
|
|
*/
|
|
export function formatDate(
|
|
date: Date | string | number,
|
|
formatStr: string = DEFAULT_DATE_FORMAT,
|
|
options?: { locale?: Locale }
|
|
): string {
|
|
const parsed = normalizeDate(date)
|
|
|
|
if (!parsed || !isValid(parsed)) {
|
|
return ''
|
|
}
|
|
|
|
return format(parsed, formatStr, options)
|
|
}
|
|
|
|
/**
|
|
* Get a human-readable relative time string (e.g. "3 days ago").
|
|
*
|
|
* @param date - A Date object, ISO string, or timestamp
|
|
* @param options - Optional settings for suffix and locale
|
|
* @returns Relative time string, or empty string if invalid
|
|
*/
|
|
export function relativeTime(
|
|
date: Date | string | number,
|
|
options?: { addSuffix?: boolean; locale?: Locale }
|
|
): string {
|
|
const parsed = normalizeDate(date)
|
|
|
|
if (!parsed || !isValid(parsed)) {
|
|
return ''
|
|
}
|
|
|
|
return formatDistanceToNow(parsed, {
|
|
addSuffix: options?.addSuffix ?? true,
|
|
locale: options?.locale,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Parse a date string or value into a Date object.
|
|
*
|
|
* @param date - A Date object, ISO string, or timestamp
|
|
* @returns A valid Date object, or null if parsing fails
|
|
*/
|
|
export function parseDate(date: Date | string | number): Date | null {
|
|
const parsed = normalizeDate(date)
|
|
|
|
if (!parsed || !isValid(parsed)) {
|
|
return null
|
|
}
|
|
|
|
return parsed
|
|
}
|
|
|
|
/**
|
|
* Check whether a given date value is valid.
|
|
*
|
|
* @param date - A Date object, ISO string, or timestamp
|
|
* @returns True if the date is valid
|
|
*/
|
|
export function isValidDate(date: Date | string | number): boolean {
|
|
const parsed = normalizeDate(date)
|
|
return parsed !== null && isValid(parsed)
|
|
}
|
|
|
|
/**
|
|
* Normalize various date input types into a Date object.
|
|
*/
|
|
function normalizeDate(date: Date | string | number): Date | null {
|
|
if (date instanceof Date) {
|
|
return date
|
|
}
|
|
|
|
if (typeof date === 'string') {
|
|
const parsed = parseISO(date)
|
|
return isValid(parsed) ? parsed : null
|
|
}
|
|
|
|
if (typeof date === 'number') {
|
|
const parsed = new Date(date)
|
|
return isValid(parsed) ? parsed : null
|
|
}
|
|
|
|
return null
|
|
}
|