Download code/web/src/lib/format.ts from MiniMaxAI/oss-ecosystem-data: direct link, hf CLI and curl.
- Browser
- Download file 2.22 kB
-
https://huggingface.co/datasets/MiniMaxAI/oss-ecosystem-data/resolve/c5ddbc97bef3ea5778daf766b28066b6deb7ed84/code/web/src/lib/format.ts
- Command line
-
hf download hf://datasets/MiniMaxAI/oss-ecosystem-data@c5ddbc97bef3ea5778daf766b28066b6deb7ed84/code/web/src/lib/format.ts
-
curl -L -o format.ts https://huggingface.co/datasets/MiniMaxAI/oss-ecosystem-data/resolve/c5ddbc97bef3ea5778daf766b28066b6deb7ed84/code/web/src/lib/format.ts
2.22 kB
| 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) | |