File size: 3,718 Bytes
2bb5af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/** 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)} م.ب`;
}