Fix JS human_readable not working for sizes >= 1GB

This commit is contained in:
Kovid Goyal 2024-08-15 13:36:45 +05:30
parent aee1560736
commit a54cc071db
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -142,20 +142,18 @@ def rating_to_stars(value, allow_half_stars=False, star='★', half='⯨'):
ans = star.repeat(int(r/2.0)) ans = star.repeat(int(r/2.0))
return ans return ans
def human_readable(size, sep=' '): def human_readable(size, sep=' '):
divisor, suffix = 1, "B" if size == 0:
for i, candidate in enumerate(('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB')): return f'0{sep}B'
if size < (1 << ((i + 1) * 10)): i = Math.floor(Math.log(size) / Math.log(1024))
divisor, suffix = (1 << (i * 10)), candidate size = (size / Math.pow(1024, i)).toFixed(1)
break
size = (float(size)/divisor) + ''
pos = size.find(".")
if pos > -1:
size = size[:pos + 2]
if size.endswith('.0'): if size.endswith('.0'):
size = size[:-2] size = size[:-2]
suffix = v"['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB']"[i]
return size + sep + suffix return size + sep + suffix
def document_height(): def document_height():
html = document.documentElement html = document.documentElement
return max(document.body.scrollHeight, document.body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight) return max(document.body.scrollHeight, document.body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight)