Spaces:
Running
Running
Download web/src/lib/format.ts from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 2.81 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/lib/format.ts
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/web/src/lib/format.ts
-
curl -L -o format.ts https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/lib/format.ts
2.81 kB
| // Display helpers. Amounts are whole rupees (Cedar has no floating point). | |
| const MONEY = /(amount|total|refund|price|cost|cod|outstanding|limit|balance|paid)/i; | |
| export function isMoneyField(name: string): boolean { | |
| return MONEY.test(name) && !/(count|rate|pct|percent|score)/i.test(name); | |
| } | |
| export function formatMoney(n: number, locale = "en"): string { | |
| const num = new Intl.NumberFormat(locale === "ur" ? "en-PK" : "en-PK", { | |
| maximumFractionDigits: 0, | |
| }).format(n); | |
| return `Rs ${num}`; | |
| } | |
| export function formatNumber(n: number, digits = 0): string { | |
| return new Intl.NumberFormat("en-PK", { maximumFractionDigits: digits }).format(n); | |
| } | |
| export function formatPct(n: number | null | undefined, digits = 0): string { | |
| if (n === null || n === undefined || Number.isNaN(n)) return "—"; | |
| return `${(n * 100).toFixed(digits)}%`; | |
| } | |
| /** Compact duration: 45s, 3m 12s, 2h 5m, 1d 3h. */ | |
| export function formatDuration(totalSeconds: number): string { | |
| const s = Math.max(0, Math.floor(totalSeconds)); | |
| const d = Math.floor(s / 86400); | |
| const h = Math.floor((s % 86400) / 3600); | |
| const m = Math.floor((s % 3600) / 60); | |
| const sec = s % 60; | |
| if (d) return h ? `${d}d ${h}h` : `${d}d`; | |
| if (h) return m ? `${h}h ${m}m` : `${h}h`; | |
| if (m) return sec ? `${m}m ${sec}s` : `${m}m`; | |
| return `${sec}s`; | |
| } | |
| export function formatValue(field: string, v: unknown, locale = "en"): string { | |
| if (v === undefined) return "—"; | |
| if (v === null) return "null"; | |
| if (typeof v === "number") return isMoneyField(field) ? formatMoney(v, locale) : formatNumber(v, 2); | |
| if (typeof v === "boolean") return v ? "true" : "false"; | |
| if (typeof v === "string") return v; | |
| return JSON.stringify(v); | |
| } | |
| export function formatDateTime(iso: string | null | undefined, locale = "en"): string { | |
| if (!iso) return "—"; | |
| const d = new Date(iso); | |
| if (Number.isNaN(d.getTime())) return iso; | |
| return new Intl.DateTimeFormat(locale === "ur" ? "ur-PK" : "en-GB", { | |
| dateStyle: "medium", | |
| timeStyle: "short", | |
| }).format(d); | |
| } | |
| export function formatTime(iso: string, locale = "en"): string { | |
| const d = new Date(iso); | |
| return new Intl.DateTimeFormat(locale === "ur" ? "ur-PK" : "en-GB", { | |
| hour: "2-digit", | |
| minute: "2-digit", | |
| second: "2-digit", | |
| }).format(d); | |
| } | |
| export function humanize(key: string): string { | |
| const s = key.replace(/[._]/g, " ").trim(); | |
| return s.charAt(0).toUpperCase() + s.slice(1); | |
| } | |
| /** "demo.refund_order" -> "Refund order" */ | |
| export function toolLabel(tool: string): string { | |
| const name = tool.includes(".") ? tool.slice(tool.indexOf(".") + 1) : tool; | |
| return humanize(name); | |
| } | |
| export function shortId(id: string | null | undefined): string { | |
| return id ? id.slice(0, 8) : "—"; | |
| } | |