1 export function formatFileSize(
2 bytes: number,
3 decimalPlaces: number = 2
4 ): string {
5 if (bytes === Infinity) {
6 return "unlimited"
7 }
8 if (bytes === 0) {
9 return "0 Bytes"
10 }
11
12 const k = 1024
13 const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
14 const i = Math.floor(Math.log(bytes) / Math.log(k))
15
16 return (
17 parseFloat((bytes / Math.pow(k, i)).toFixed(decimalPlaces)) + " " + sizes[i]
18 )
19 }