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.
This commit is contained in:
Simon Bruder 2024-01-06 02:36:45 +01:00
parent 9995ff511e
commit da349a7113
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC

View file

@ -1,18 +1,16 @@
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
function humanFileSize(bytes) { function humanFileSize(size) {
const thresh = 1024 if (size === 0) {
if(Math.abs(bytes) < thresh) { return "0B";
return bytes + ' B'
} }
const units = ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'] if (size < 0) {
var u = -1 return null;
do { }
bytes /= thresh const base = Math.floor(Math.log2(size) / 10)
++u const unit = ["B", "KiB", "MiB", "GiB", "TiB"][base]
} while(Math.abs(bytes) >= thresh && u < units.length - 1) const relative = size / 2**(10*base)
return bytes.toFixed(1)+' '+units[u] return relative.toFixed(3) + "" + unit
} }
function textToA(line) { function textToA(line) {
let outerElement = document.createElement('div') let outerElement = document.createElement('div')