From c28f279a108249cf5788e87cbc001c780af6d09c Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Sat, 6 Jan 2024 02:36:45 +0100 Subject: [PATCH] nginx-iteractive-index: Reimplement humanFileSize The previous implementation was copy-pasted from a source that did not allow redistribution or sublicensing. Therefore, I reimplemented the function myself. --- modules/nginx-interactive-index/listing.js | 24 ++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/modules/nginx-interactive-index/listing.js b/modules/nginx-interactive-index/listing.js index b9583bc..5c98810 100644 --- a/modules/nginx-interactive-index/listing.js +++ b/modules/nginx-interactive-index/listing.js @@ -1,19 +1,17 @@ document.addEventListener('DOMContentLoaded', () => { - function humanFileSize(bytes) { - const thresh = 1024 - if(Math.abs(bytes) < thresh) { - return bytes + ' B' - } - const units = ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'] - var u = -1 - do { - bytes /= thresh - ++u - } while(Math.abs(bytes) >= thresh && u < units.length - 1) - return bytes.toFixed(1)+' '+units[u] + function humanFileSize(size) { + if (size === 0) { + return "0 B"; + } + if (size < 0) { + return null; + } + const base = Math.floor(Math.log2(size) / 10) + const unit = ["B", "KiB", "MiB", "GiB", "TiB"][base] + const relative = size / 2**(10*base) + return relative.toFixed(3) + " " + unit } - function textToA(line) { let outerElement = document.createElement('div') outerElement.innerHTML = line