mirror of
https://github.com/thelounge/thelounge.git
synced 2024-06-03 14:22:19 +02:00
16 lines
391 B
TypeScript
16 lines
391 B
TypeScript
const sizes = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"];
|
|
|
|
export default function formatSize(size: number): string {
|
|
if (size <= 0) {
|
|
throw new Error("Size must be a positive number");
|
|
}
|
|
|
|
const i = Math.floor(Math.log(size) / Math.log(1024));
|
|
|
|
if (i >= sizes.length) {
|
|
throw new Error("Size is out of range");
|
|
}
|
|
|
|
return `${(size / Math.pow(1024, i)).toFixed(1)} ${sizes[i]}`;
|
|
}
|