Spaces:
Running

File size: 2,810 Bytes
03608f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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) : "—";
}