From a54cc071dbcc5868ce69b011ab55013fa242c482 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 15 Aug 2024 13:36:45 +0530 Subject: [PATCH] Fix JS human_readable not working for sizes >= 1GB --- src/pyj/utils.pyj | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/pyj/utils.pyj b/src/pyj/utils.pyj index 1a2a6256f6..724b80c025 100644 --- a/src/pyj/utils.pyj +++ b/src/pyj/utils.pyj @@ -142,20 +142,18 @@ def rating_to_stars(value, allow_half_stars=False, star='★', half='⯨'): ans = star.repeat(int(r/2.0)) return ans + def human_readable(size, sep=' '): - divisor, suffix = 1, "B" - for i, candidate in enumerate(('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB')): - if size < (1 << ((i + 1) * 10)): - divisor, suffix = (1 << (i * 10)), candidate - break - size = (float(size)/divisor) + '' - pos = size.find(".") - if pos > -1: - size = size[:pos + 2] + if size == 0: + return f'0{sep}B' + i = Math.floor(Math.log(size) / Math.log(1024)) + size = (size / Math.pow(1024, i)).toFixed(1) if size.endswith('.0'): size = size[:-2] + suffix = v"['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB']"[i] return size + sep + suffix + def document_height(): html = document.documentElement return max(document.body.scrollHeight, document.body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight)