- W6: formatRelative(date, t, "compact" | "verbose") ersetzt formatRelativeCompact/formatRelativeVerbose (nur die Übersetzungsschlüssel unterschieden sich). Test hält beide Formen und die Grenzen fest. - W7: getStripe() statt eines Proxy-Objekts um den faulen Singleton; der Client entsteht weiterhin erst beim ersten Aufruf (fehlender Key bricht den Import nicht). Sponsor-Router, Webhook und Mock angepasst. W5 (Bild-/Musik-URLs zusammenführen) bewusst nicht umgesetzt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
25 lines
1.1 KiB
TypeScript
25 lines
1.1 KiB
TypeScript
type TFunc = (key: string, values?: Record<string, string | number>) => string;
|
|
|
|
/**
|
|
* „kurz“: "5m", "3h", "2d" — wo wenig Platz ist (Kommentare, Mitteilungen,
|
|
* Werbekarten, Nachrichten); Übersetzer mit Namespace "RelativeTime".
|
|
* „ausführlich“: "vor 5 Minuten" — Beitragskarten im Feed; Übersetzer mit
|
|
* justNow/minutesAgo/hoursAgo/daysAgo. Ab 7 Tagen das Datum.
|
|
*/
|
|
const KEYS = {
|
|
compact: ["minutes", "hours", "days"],
|
|
verbose: ["minutesAgo", "hoursAgo", "daysAgo"],
|
|
} as const;
|
|
|
|
export function formatRelative(date: Date | string, t: TFunc, style: keyof typeof KEYS = "compact"): string {
|
|
const [minutesKey, hoursKey, daysKey] = KEYS[style];
|
|
const diffMins = Math.floor((Date.now() - new Date(date).getTime()) / 60000);
|
|
const diffHours = Math.floor(diffMins / 60);
|
|
const diffDays = Math.floor(diffHours / 24);
|
|
if (diffMins < 1) return t("justNow");
|
|
if (diffMins < 60) return t(minutesKey, { count: diffMins });
|
|
if (diffHours < 24) return t(hoursKey, { count: diffHours });
|
|
if (diffDays < 7) return t(daysKey, { count: diffDays });
|
|
return new Date(date).toLocaleDateString();
|
|
}
|