mirror of
https://github.com/fabro-sh/fabro.git
synced 2026-10-09 03:20:56 +00:00
feat(web): group memory sizes with thousands separators
Large GiB values rendered without separators (2173 GiB). Format the numeric part with toLocaleString so it reads 2,173 GiB. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
b845e76792
commit
cc7d6a007f
2 changed files with 9 additions and 6 deletions
|
|
@ -109,14 +109,13 @@ const BYTES_PER_MIB = 1024 * 1024;
|
|||
const BYTES_PER_KIB = 1024;
|
||||
|
||||
/**
|
||||
* Format a byte count as a memory/disk size (e.g. "8 GiB", "512 MiB", "4 KiB", "742 B").
|
||||
* Pass `fractionDigits: 0` to round to whole units.
|
||||
* Format a byte count as a memory/disk size (e.g. "8 GiB", "2,173 GiB", "742 B").
|
||||
* Large values get thousands separators. Pass `fractionDigits: 0` to round to
|
||||
* whole units.
|
||||
*/
|
||||
export function formatBytesAsMemory(bytes: number, fractionDigits = 1): string {
|
||||
const scaled = (unit: number) => {
|
||||
const value = bytes / unit;
|
||||
return Number.isInteger(value) ? `${value}` : value.toFixed(fractionDigits);
|
||||
};
|
||||
const scaled = (unit: number) =>
|
||||
(bytes / unit).toLocaleString("en-US", { maximumFractionDigits: fractionDigits });
|
||||
if (bytes >= BYTES_PER_GIB) return `${scaled(BYTES_PER_GIB)} GiB`;
|
||||
if (bytes >= BYTES_PER_MIB) return `${scaled(BYTES_PER_MIB)} MiB`;
|
||||
if (bytes >= BYTES_PER_KIB) return `${scaled(BYTES_PER_KIB)} KiB`;
|
||||
|
|
|
|||
|
|
@ -196,6 +196,10 @@ describe("formatBytesAsMemory", () => {
|
|||
expect(formatBytesAsMemory(51.8 * 1024 * 1024 * 1024, 0)).toBe("52 GiB");
|
||||
expect(formatBytesAsMemory(1536, 0)).toBe("2 KiB");
|
||||
});
|
||||
|
||||
test("groups large values with thousands separators", () => {
|
||||
expect(formatBytesAsMemory(2173 * 1024 * 1024 * 1024, 0)).toBe("2,173 GiB");
|
||||
});
|
||||
});
|
||||
|
||||
describe("RunSandbox route", () => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue