contiai / apps /web /src /lib /format.ts
aliroya's picture
ContiAI platform: API and queue worker in one container (part 2)
2bb5af3 verified
Raw History Blame Contribute Delete
3.72 kB
/** Formatting.
*
* Numerals are Latin 0-9 everywhere, not Arabic-Indic. Two reasons: they scan
* faster in tables and metrics, and they match what the API returns, so a
* figure on screen and the same figure in a log are the same characters.
* `ar-EG` alone gives Arabic-Indic digits, hence the explicit `-u-nu-latn`.
*
* Anything holding digits or an identifier has to be direction-isolated before
* it sits inside an Arabic sentence, or the browser reorders it. That is the
* most common bug in a mixed-script RTL interface, so `isolate` exists rather
* than leaving it to each caller to remember.
*/
const LOCALE = "ar-EG-u-nu-latn";
const plain = new Intl.NumberFormat(LOCALE);
const compactFmt = new Intl.NumberFormat(LOCALE, {
notation: "compact",
maximumFractionDigits: 1,
});
const dateFmt = new Intl.DateTimeFormat(LOCALE, { dateStyle: "long", timeStyle: "short" });
const dayFmt = new Intl.DateTimeFormat(LOCALE, { month: "short", day: "numeric" });
export const num = (n: number) => plain.format(n);
export const compact = (n: number) => compactFmt.format(n);
export const percent = (fraction: number) =>
new Intl.NumberFormat(LOCALE, { style: "percent", maximumFractionDigits: 0 }).format(fraction);
export const usd = (n: number) =>
new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2,
maximumFractionDigits: n > 0 && n < 1 ? 4 : 2,
}).format(n);
/** Wrap a run of Latin text or digits so RTL context cannot reorder it. */
export const isolate = (value: string | number) => `⁨${value}⁩`;
/** Full timestamp. Use as the tooltip on a relative one. */
export const fullDate = (iso: string | null) => (iso ? dateFmt.format(new Date(iso)) : "—");
export const dayLabel = (iso: string) => dayFmt.format(new Date(iso));
/** Relative for recent events, absolute once relative stops being useful. */
export function when(iso: string | null): string {
if (!iso) return "—";
const seconds = Math.round((Date.now() - new Date(iso).getTime()) / 1000);
if (seconds < 45) return "الآن";
if (seconds < 90) return "منذ دقيقة";
const minutes = Math.round(seconds / 60);
if (minutes < 45) return `منذ ${num(minutes)} دقيقة`;
const hours = Math.round(minutes / 60);
if (hours < 24) return `منذ ${num(hours)} ساعة`;
const days = Math.round(hours / 24);
if (days === 1) return "أمس";
if (days < 7) return `منذ ${num(days)} أيام`;
return dateFmt.format(new Date(iso));
}
export function duration(from: string | null, to: string | null): string {
if (!from) return "—";
const end = to ? new Date(to).getTime() : Date.now();
const seconds = Math.max(0, Math.round((end - new Date(from).getTime()) / 1000));
if (seconds < 60) return `${num(seconds)} ثانية`;
const minutes = Math.floor(seconds / 60);
if (minutes < 60) return `${num(minutes)} دقيقة`;
return `${num(Math.floor(minutes / 60))} س ${num(minutes % 60)} د`;
}
/** "About how long will this take" from an estimate in seconds. */
export function approxDuration(seconds: number | null | undefined): string {
if (!seconds) return "—";
if (seconds < 120) return `${num(seconds)} ثانية`;
const minutes = Math.round(seconds / 60);
if (minutes < 60) return `حوالي ${num(minutes)} دقيقة`;
return `حوالي ${num(Math.round((minutes / 60) * 10) / 10)} ساعة`;
}
export function bytes(n: number | null | undefined): string {
if (n === null || n === undefined) return "—";
if (n < 1024) return `${num(n)} بايت`;
if (n < 1024 ** 2) return `${num(Math.round(n / 102.4) / 10)} ك.ب`;
return `${num(Math.round(n / (1024 ** 2 / 10)) / 10)} م.ب`;
}