diff --git a/frontend/src/lib/utils/formatBytes.ts b/frontend/src/lib/utils/formatBytes.ts new file mode 100644 index 0000000000..62fbf45289 --- /dev/null +++ b/frontend/src/lib/utils/formatBytes.ts @@ -0,0 +1,14 @@ +function formatBytes(bytes: number, decimals: number = 0) { + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + + if (bytes === 0) return [0, sizes[0]]; + + const k = 1024; + const dm = decimals < 0 ? 0 : decimals; + + const i = Math.floor(Math.log(bytes) / Math.log(k)); + + return [parseFloat((bytes / Math.pow(k, i)).toFixed(dm)), sizes[i]]; +} + +export default formatBytes;