From c4eccea8bb9ff9f6d6dab37c4e10673052a65ff7 Mon Sep 17 00:00:00 2001 From: hensm Date: Wed, 9 Sep 2020 15:56:51 +0100 Subject: [PATCH] Use correct units for site file sizes --- docs/index.js | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/docs/index.js b/docs/index.js index 507173e..3f902c5 100644 --- a/docs/index.js +++ b/docs/index.js @@ -174,30 +174,37 @@ function onError (err) { } -function formatSize (bytes, precision = 1) { - // Sizes in bytes - const kilobyte = 1024; - const megabyte = kilobyte * 1024; - const gigabyte = megabyte * 1024; - const terabyte = gigabyte * 1024; - const petabyte = terabyte * 1024; +function formatSize (bytes, precision = 1, useMetric = false) { + const factor = useMetric ? 1000 : 1024; - if (bytes >= 0 && bytes < kilobyte) { + // Sizes in bytes + const kxbyte = factor; + const mxbyte = kxbyte * factor; + const gxbyte = mxbyte * factor; + const txbyte = gxbyte * factor; + const pxbyte = txbyte * factor; + + if (bytes >= 0 && bytes < kxbyte) { return `${bytes} B`; - } else if (bytes >= kilobyte && bytes < megabyte) { - return `${(bytes / kilobyte).toFixed(precision)} KB`; + } else if (bytes >= kxbyte && bytes < mxbyte) { + return `${(bytes / kxbyte).toFixed(precision)} ${ + useMetric ? "KB" : "KiB"}`; - } else if (bytes >= megabyte && bytes < gigabyte) { - return `${(bytes / megabyte).toFixed(precision)} MB`; + } else if (bytes >= mxbyte && bytes < gxbyte) { + return `${(bytes / mxbyte).toFixed(precision)} ${ + useMetric ? "MB" : "MiB"}`; - } else if (bytes >= gigabyte && bytes < terabyte) { - return `${(bytes / gigabyte).toFixed(precision)} GB`; + } else if (bytes >= gxbyte && bytes < txbyte) { + return `${(bytes / gxbyte).toFixed(precision)} ${ + useMetric ? "GB" : "GiB"}`; - } else if (bytes >= terabyte && bytes < petabyte) { - return `${(bytes / terabyte).toFixed(precision)} TB`; + } else if (bytes >= txbyte && bytes < pxbyte) { + return `${(bytes / txbyte).toFixed(precision)} ${ + useMetric ? "TB" : "TiB"}`; - } else if (bytes >= petabyte) { - return `${(bytes / petabyte).toFixed(precision)} PB`; + } else if (bytes >= pxbyte) { + return `${(bytes / pxbyte).toFixed(precision)} ${ + useMetric ? "PB" : "PiB"}`; } }