17 lines
369 B
TypeScript
Raw Normal View History

2024-11-27 09:43:03 +01:00
export const Bytes = {
pretty(bytes: number) {
const sizes = ["bytes", "KB", "MB", "GB", "TB"];
if (bytes == 0) {
return "0 b";
}
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)).toString());
2024-08-22 17:41:44 +02:00
2024-11-27 09:43:03 +01:00
if (i == 0) {
return bytes + " " + sizes[i];
}
2024-08-22 17:41:44 +02:00
2024-11-27 09:43:03 +01:00
return (bytes / Math.pow(1024, i)).toFixed(1) + " " + sizes[i];
2024-08-22 17:41:44 +02:00
}
2024-11-27 09:43:03 +01:00
}