forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

format.ts78 lines, 3.2 KiBHistoryRawDownload
1import type { Locale } from "@/i18n/messages";
2
3export function formatDateTime(ms: number | null | undefined, locale: Locale = "en"): string {
4 if (ms == null) return "—";
5 return new Intl.DateTimeFormat(locale === "tr" ? "tr-TR" : "en-US", {
6 dateStyle: "medium",
7 timeStyle: "short",
8 }).format(new Date(ms));
9}
10
11/** Time only (e.g. "07:33") — for a chat turn's stamp. */
12export function formatTime(ms: number | null | undefined, locale: Locale = "en"): string {
13 if (ms == null) return "—";
14 return new Intl.DateTimeFormat(locale === "tr" ? "tr-TR" : "en-US", { timeStyle: "short" }).format(new Date(ms));
15}
16
17/** Date only, no time — for stacked date/time cells. */
18export function formatDate(ms: number | null | undefined, locale: Locale = "en"): string {
19 if (ms == null) return "—";
20 return new Intl.DateTimeFormat(locale === "tr" ? "tr-TR" : "en-US", {
21 dateStyle: "medium",
22 }).format(new Date(ms));
23}
24
25/** Convert datetime-local input value to unix ms, or null if empty. */
26export function localInputToMs(value: string): number | null {
27 if (!value) return null;
28 const t = new Date(value).getTime();
29 return Number.isNaN(t) ? null : t;
30}
31
32/** Convert unix ms to datetime-local input value. */
33export function msToLocalInput(ms: number | null | undefined): string {
34 if (ms == null) return "";
35 const d = new Date(ms);
36 const pad = (n: number) => String(n).padStart(2, "0");
37 return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
38}
39
40export function formatDurationMinutes(ms: number | null | undefined, locale: Locale = "en"): string {
41 if (ms == null) return "—";
42 return locale === "tr" ? `${Math.round(ms / 60_000)} dk` : `${Math.round(ms / 60_000)} min`;
43}
44
45export function formatDurationClock(ms: number | null | undefined): string {
46 if (ms == null) return "--:--";
47 const totalSeconds = Math.max(0, Math.floor(ms / 1000));
48 const minutes = Math.floor(totalSeconds / 60);
49 const seconds = totalSeconds % 60;
50 return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
51}
52
53export function examDurationMs(durationMs: number | null | undefined, startsAt: number | null, endsAt: number | null): number | null {
54 if (durationMs != null) return durationMs;
55 if (startsAt == null || endsAt == null || endsAt <= startsAt) return null;
56 return endsAt - startsAt;
57}
58
59/**
60 * A mark or average with a fixed number of decimals in the reader's locale:
61 * "68,2" in Turkish, "68.2" in English.
62 */
63export function formatDecimal(value: number | null | undefined, locale: Locale = "en", digits = 1): string {
64 if (value == null || !Number.isFinite(value)) return "—";
65 return new Intl.NumberFormat(locale === "tr" ? "tr-TR" : "en-US", {
66 minimumFractionDigits: digits,
67 maximumFractionDigits: digits,
68 }).format(value);
69}
70
71/**
72 * A chart or count value in the reader's locale with up to `maxDigits`
73 * decimals: "1.234,5" in Turkish, "1,234.5" in English. Whole numbers stay whole.
74 */
75export function formatNumber(value: number | null | undefined, locale: Locale = "en", maxDigits = 1): string {
76 if (value == null || !Number.isFinite(value)) return "—";
77 return new Intl.NumberFormat(locale === "tr" ? "tr-TR" : "en-US", { maximumFractionDigits: maxDigits }).format(value);
78}