File size: 2,224 Bytes
0953253
 
 
 
2471af3
 
 
 
 
 
 
 
 
0953253
 
 
 
 
2471af3
 
 
 
 
 
 
 
 
 
 
 
 
0953253
 
 
 
 
 
 
 
 
 
 
2471af3
 
 
 
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
import { getLang } from "@/lib/i18n"

/** Compact magnitudes, about three figures: 987 / 12.3K / 456K / 1.42M / 142M / 1.2B, and in
 *  Chinese 9,876 / 1.23万 / 45.6万 / 456万 / 4347万 / 1.75亿. */
export function num(n: number | null | undefined): string {
  if (n == null) return "–"
  const [v, u] = numParts(n)
  return v + u
}

/** Split form for big display numbers: ["1.42", "M"]. */
export function numParts(n: number): [string, string] {
  const a = Math.abs(n)
  if (getLang() === "zh") {
    if (a >= 1e8) return [trim((n / 1e8).toFixed(a >= 1e10 ? 0 : a >= 1e9 ? 1 : 2)), "亿"]
    if (a >= 1e4) return [trim((n / 1e4).toFixed(a >= 1e6 ? 0 : a >= 1e5 ? 1 : 2)), "万"]
    return [Math.round(n).toLocaleString("en-US"), ""]
  }
  if (a >= 1e9) return [trim((n / 1e9).toFixed(a >= 1e10 ? 1 : 2)), "B"]
  if (a >= 1e6) return [trim((n / 1e6).toFixed(a >= 1e8 ? 0 : a >= 1e7 ? 1 : 2)), "M"]
  if (a >= 1e4) return [trim((n / 1e3).toFixed(a >= 1e5 ? 0 : 1)), "K"]
  return [Math.round(n).toLocaleString("en-US"), ""]
}

export const full = (n: number | null | undefined) => (n == null ? "–" : n.toLocaleString("en-US"))

const trim = (s: string) => s.replace(/\.0+$/, "").replace(/(\.\d*?)0+$/, "$1")

export const hfUrl = (id: string) => `https://huggingface.co/${id}`

const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
const zh = () => getLang() === "zh"
const M = (s: string) => Number(s.slice(5, 7))
const D = (s: string) => Number(s.slice(8, 10))
/** "Nov" / "11月" */
export const mon = (s: string) => (zh() ? `${M(s)}月` : MONTHS[M(s) - 1])
/** "Nov 3" / "11月3日" */
export const md = (s: string) => (zh() ? `${M(s)}月${D(s)}日` : `${MONTHS[M(s) - 1]} ${D(s)}`)
/** "Nov 3, 2026" / "2026年11月3日" */
export const mdy = (s: string) => (zh() ? `${s.slice(0, 4)}年${md(s)}` : `${md(s)}, ${s.slice(0, 4)}`)
/** "Nov 2026" / "2026年11月" */
export const my = (s: string) => (zh() ? `${s.slice(0, 4)}年${M(s)}月` : `${MONTHS[M(s) - 1]} ${s.slice(0, 4)}`)

export const DAY = 86400000
export const toTime = (s: string) => Date.parse(s.slice(0, 10) + "T00:00:00Z")
export const isoDay = (t: number) => new Date(t).toISOString().slice(0, 10)