mirror of
https://github.com/4gray/iptvnator.git
synced 2026-10-08 17:06:15 -08:00
- Replaced moment.js usage with Intl.DateTimeFormat for date formatting in various components. - Introduced utility functions `normalizeDateLocale` and `formatWithIntl` for locale normalization and date formatting. - Updated PlaylistSwitcher, RecentPlaylists, ContentCard, EPG components, and MultiEpgContainer to utilize the new date formatting approach. - Removed moment-date pipe and related tests, simplifying date handling logic. - Ensured all date displays respect the current locale settings. Entire-Checkpoint: 271bd0162027
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
const DEFAULT_DATE_LOCALE = 'en';
|
|
|
|
const DATE_LOCALE_ALIASES: Record<string, string> = {
|
|
ary: 'ar-MA',
|
|
by: 'be',
|
|
zhtw: 'zh-Hant',
|
|
};
|
|
|
|
const formatterCache = new Map<string, Intl.DateTimeFormat>();
|
|
|
|
export function normalizeDateLocale(locale?: string | null): string {
|
|
const normalizedLocale = String(locale ?? '').trim();
|
|
|
|
if (!normalizedLocale) {
|
|
return DEFAULT_DATE_LOCALE;
|
|
}
|
|
|
|
return (
|
|
DATE_LOCALE_ALIASES[normalizedLocale.toLowerCase()] ?? normalizedLocale
|
|
);
|
|
}
|
|
|
|
export function formatWithIntl(
|
|
value: Date | number | string,
|
|
options: Intl.DateTimeFormatOptions & {
|
|
locale?: string | null;
|
|
timeZone?: string;
|
|
} = {}
|
|
): string {
|
|
const date =
|
|
value instanceof Date
|
|
? value
|
|
: typeof value === 'number'
|
|
? new Date(value)
|
|
: new Date(value);
|
|
|
|
if (Number.isNaN(date.getTime())) {
|
|
return '';
|
|
}
|
|
|
|
const { locale, ...formatOptions } = options;
|
|
const resolvedLocale = normalizeDateLocale(locale);
|
|
const cacheKey = JSON.stringify([resolvedLocale, formatOptions]);
|
|
|
|
let formatter = formatterCache.get(cacheKey);
|
|
if (!formatter) {
|
|
formatter = new Intl.DateTimeFormat(resolvedLocale, formatOptions);
|
|
formatterCache.set(cacheKey, formatter);
|
|
}
|
|
|
|
return formatter.format(date);
|
|
}
|