mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2024-12-15 00:46:22 +00:00
b754470630
File size when JavaScript is disabled will now properly use B suffix. It's referring to the fact that their unit is bytes. Disclaimer about this. Back-end will only return the file size in bytes, front-end is supposed to convert them into prettier units (KB, MB, ...). Nothing much can be done if front-end have JavaScript disabled. I don't want to defer the task, prettying the units, to back-end.
30 lines
931 B
JavaScript
30 lines
931 B
JavaScript
/* global LazyLoad */
|
|
|
|
const page = {
|
|
lazyLoad: null
|
|
}
|
|
|
|
page.getPrettyBytes = function (num, si) {
|
|
// MIT License
|
|
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
|
if (!Number.isFinite(num)) return num
|
|
|
|
const neg = num < 0 ? '-' : ''
|
|
const scale = si ? 1000 : 1024
|
|
if (neg) num = -num
|
|
if (num < scale) return `${neg}${num} B`
|
|
|
|
const exponent = Math.min(Math.floor(Math.log10(num) / 3), 8) // 8 is count of KMGTPEZY
|
|
const numStr = Number((num / Math.pow(scale, exponent)).toPrecision(3))
|
|
const pre = (si ? 'kMGTPEZY' : 'KMGTPEZY').charAt(exponent - 1) + (si ? '' : 'i')
|
|
return `${neg}${numStr} ${pre}B`
|
|
}
|
|
|
|
window.onload = function () {
|
|
const elements = document.getElementsByClassName('file-size')
|
|
for (let i = 0; i < elements.length; i++)
|
|
elements[i].innerHTML = page.getPrettyBytes(parseInt(elements[i].innerHTML.replace(/\s*B$/i, '')))
|
|
|
|
page.lazyLoad = new LazyLoad()
|
|
}
|