Public source validation / validate (push) Failing after 3m8s
34 lines
1.5 KiB
TypeScript
34 lines
1.5 KiB
TypeScript
export const UI_LOCALE = 'nl-BE';
|
|
export const UI_TIME_ZONE = 'Europe/Brussels';
|
|
|
|
const numberFormatter = new Intl.NumberFormat(UI_LOCALE);
|
|
const decimalFormatter = new Intl.NumberFormat(UI_LOCALE, { maximumFractionDigits: 1 });
|
|
const dateTimeFormatter = new Intl.DateTimeFormat(UI_LOCALE, { dateStyle: 'medium', timeStyle: 'short', timeZone: UI_TIME_ZONE });
|
|
export const NEVER_RECEIVED = 'Nooit ontvangen';
|
|
|
|
export function formatNumber(value: number, maximumFractionDigits?: number): string {
|
|
if (!Number.isFinite(value)) return '—';
|
|
return (maximumFractionDigits === undefined ? numberFormatter : new Intl.NumberFormat(UI_LOCALE, { maximumFractionDigits })).format(value);
|
|
}
|
|
|
|
export function formatDecimal(value: number): string {
|
|
return Number.isFinite(value) ? decimalFormatter.format(value) : '—';
|
|
}
|
|
|
|
export function formatDateTime(value?: string): string {
|
|
if (!hasReceivedTimestamp(value)) return NEVER_RECEIVED;
|
|
const date = new Date(value);
|
|
return dateTimeFormatter.format(date);
|
|
}
|
|
|
|
/** Rejects transport zero-values and invalid input before they reach a visible <time>. */
|
|
export function hasReceivedTimestamp(value?: string): value is string {
|
|
if (!value?.trim()) return false;
|
|
const date = new Date(value);
|
|
return Number.isFinite(date.valueOf()) && date.valueOf() > 0 && date.getUTCFullYear() > 1;
|
|
}
|
|
|
|
export function formatPercent(value?: number, maximumFractionDigits = 1): string {
|
|
return value === undefined || !Number.isFinite(value) ? '—' : new Intl.NumberFormat(UI_LOCALE, { maximumFractionDigits, style: 'percent' }).format(value / 100);
|
|
}
|