// SPDX-License-Identifier: AGPL-3.0-or-later // Number formatting shared by the chrome's pages, so a quantity two pages both // show reads the same on each (the vault app and Settings show vault sizes). /** A byte count for a label: "512 B", "1.5 KB", "42 MB". */ export function formatBytes(bytes) { if (bytes < 1024) { return `${bytes} B`; } const units = ["KB", "MB", "GB", "TB"]; let value = bytes / 1024; let unit = 0; while (value >= 1024 && unit < units.length - 1) { value /= 1024; unit += 1; } return `${value < 10 ? value.toFixed(1) : Math.round(value)} ${units[unit]}`; } /** A media time for a player: "4:05", "1:02:03"; "0:00" for unknown. */ export function formatTime(seconds) { if (!Number.isFinite(seconds) || seconds < 0) { return "0:00"; } const whole = Math.floor(seconds); const h = Math.floor(whole / 3600); const m = Math.floor((whole % 3600) / 60); const s = String(whole % 60).padStart(2, "0"); return h ? `${h}:${String(m).padStart(2, "0")}:${s}` : `${m}:${s}`; } /** A count with its noun: "1 collection", "3 collections". */ export function plural(n, one) { return `${n} ${n === 1 ? one : `${one}s`}`; }