oss-ecosystem-data / code /web /src /lib /format.ts
saladday1's picture
code: sync from git
2471af3 verified
Raw History Blame
1.48 kB
/** Compact English magnitudes: 987 / 12.3K / 456K / 1.42M / 142M / 1.2B. */
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 (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"]
/** "Nov 3" */
export const md = (s: string) => `${MONTHS[Number(s.slice(5, 7)) - 1]} ${Number(s.slice(8, 10))}`
/** "Nov 3, 2026" */
export const mdy = (s: string) => `${md(s)}, ${s.slice(0, 4)}`
/** "Nov 2026" */
export const my = (s: string) => `${MONTHS[Number(s.slice(5, 7)) - 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)