SPB Git

Repository Public

format.ts 2.2 KB 34 lines typescript

1export function fmtTime(ms: number): string {2  if (!Number.isFinite(ms) || ms < 0) ms = 0;3  const s = Math.floor(ms / 1000), h = Math.floor(s / 3600), m = Math.floor((s % 3600) / 60), r = s % 60;4  return h ? `${h}:${String(m).padStart(2, "0")}:${String(r).padStart(2, "0")}` : `${m}:${String(r).padStart(2, "0")}`;5}6export function fmtDuration(ms: number, locale: string): string {7  const min = Math.round(ms / 60000);8  if (min < 60) return `${min} min`;9  const h = Math.floor(min / 60), m = min % 60;10  return locale === "en" ? `${h} h ${m} min` : `${h} h ${String(m).padStart(2, "0")}`;11}12export function fmtBytes(n: number | null | undefined, locale: string): string {13  if (n == null) return "—";14  const units = locale === "en" ? ["B", "KB", "MB", "GB", "TB"] : ["o", "Ko", "Mo", "Go", "To"];15  let i = 0, v = n;16  while (v >= 1024 && i < units.length - 1) { v /= 1024; i++; }17  return `${v.toLocaleString(locale === "en" ? "en-CA" : "fr-CA", { maximumFractionDigits: v >= 100 || i === 0 ? 0 : 1 })} ${units[i]}`;18}19export function fmtDate(d: string | null | undefined, locale: string, withTime = true): string {20  if (!d) return "—";21  return new Intl.DateTimeFormat(locale === "en" ? "en-CA" : "fr-CA", withTime ? { dateStyle: "medium", timeStyle: "short" } : { dateStyle: "medium" }).format(new Date(d));22}23/** "FLAC · 44,1 kHz · 16 bits" from what is known; unknown parts are simply left out. */24export function audioInfo(t: { codec: string; sampleRate: number | null; bitDepth: number | null; bitrate?: number | null }, locale: string): string {25  const names: Record<string, string> = { flac: "FLAC", mp3: "MP3", aac: "AAC", alac: "ALAC", vorbis: "Vorbis", opus: "Opus" };26  const codec = names[t.codec] ?? (t.codec.startsWith("pcm") ? "PCM" : t.codec.startsWith("dsd") ? "DSD" : t.codec.toUpperCase());27  const lossy = ["mp3", "aac", "vorbis", "opus"].includes(t.codec);28  const parts = [codec];29  if (t.sampleRate) parts.push(`${(t.sampleRate / 1000).toLocaleString(locale === "en" ? "en-CA" : "fr-CA", { maximumFractionDigits: 1 })} kHz`);30  if (t.bitDepth && !lossy) parts.push(locale === "en" ? `${t.bitDepth}-bit` : `${t.bitDepth} bits`);31  if (lossy && t.bitrate) parts.push(`${Math.round(t.bitrate / 1000)} kb/s`);32  return parts.join(" · ");33}34
Files main
Menu

Theme