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:
parent
9995ff511e
commit
da349a7113
|
@ -1,19 +1,17 @@
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
function humanFileSize(bytes) {
|
function humanFileSize(size) {
|
||||||
const thresh = 1024
|
if (size === 0) {
|
||||||
if(Math.abs(bytes) < thresh) {
|
return "0 B";
|
||||||
return bytes + ' B'
|
}
|
||||||
}
|
if (size < 0) {
|
||||||
const units = ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB']
|
return null;
|
||||||
var u = -1
|
}
|
||||||
do {
|
const base = Math.floor(Math.log2(size) / 10)
|
||||||
bytes /= thresh
|
const unit = ["B", "KiB", "MiB", "GiB", "TiB"][base]
|
||||||
++u
|
const relative = size / 2**(10*base)
|
||||||
} while(Math.abs(bytes) >= thresh && u < units.length - 1)
|
return relative.toFixed(3) + " " + unit
|
||||||
return bytes.toFixed(1)+' '+units[u]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function textToA(line) {
|
function textToA(line) {
|
||||||
let outerElement = document.createElement('div')
|
let outerElement = document.createElement('div')
|
||||||
outerElement.innerHTML = line
|
outerElement.innerHTML = line
|
||||||
|
|
Loading…
Reference in a new issue