From 97832c8436f7061aa2230376458ac368bac0b595 Mon Sep 17 00:00:00 2001 From: harsha-mangena Date: Fri, 21 Jun 2024 15:03:15 -0400 Subject: [PATCH] Book sizes are shown in binary (MiB, etc) units Switched display of ZIM sizes from decimal (MB) to binary (MiB) units. Also made the size value to be formatted in a more human friendly way (fractional part is shown only to provide at least three significant digits). --- static/skin/index.js | 16 +++++++++++----- test/server.cpp | 4 ++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/static/skin/index.js b/static/skin/index.js index b46c5be90..28a452d2b 100644 --- a/static/skin/index.js +++ b/static/skin/index.js @@ -78,15 +78,21 @@ return result; } + function humanFriendlyNumStr(num, precision) { + const n = Math.abs(num).toFixed().length; + return num.toFixed(Math.max(0, precision - n)); + } + const humanFriendlySize = (fileSize) => { if (fileSize === 0) { return ''; } - const units = ['bytes', 'kB', 'MB', 'GB', 'TB']; - let quotient = Math.floor(Math.log10(fileSize) / 3); - quotient = quotient < units.length ? quotient : units.length - 1; - fileSize /= (1000 ** quotient); - return `${+fileSize.toFixed(2)} ${units[quotient]}`; + const units = ['bytes', 'KiB', 'MiB', 'GiB', 'TiB']; + let quotient = Math.floor(Math.log2(fileSize) / 10); + quotient = Math.min(quotient, units.length - 1); + fileSize /= (1024 ** quotient); + const fileSizeStr = humanFriendlyNumStr(fileSize, 3); + return `${fileSizeStr} ${units[quotient]}`; }; const humanFriendlyTitle = (title) => { diff --git a/test/server.cpp b/test/server.cpp index ce111c933..44fc99311 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -63,7 +63,7 @@ const ResourceCollection resources200Compressible{ { DYNAMIC_CONTENT, "/ROOT%23%3F/skin/index.css" }, { STATIC_CONTENT, "/ROOT%23%3F/skin/index.css?cacheid=1e78e7cf" }, { DYNAMIC_CONTENT, "/ROOT%23%3F/skin/index.js" }, - { STATIC_CONTENT, "/ROOT%23%3F/skin/index.js?cacheid=436e0a63" }, + { STATIC_CONTENT, "/ROOT%23%3F/skin/index.js?cacheid=f43eb0b9" }, { DYNAMIC_CONTENT, "/ROOT%23%3F/skin/iso6391To3.js" }, { STATIC_CONTENT, "/ROOT%23%3F/skin/iso6391To3.js?cacheid=ecde2bb3" }, { DYNAMIC_CONTENT, "/ROOT%23%3F/skin/isotope.pkgd.min.js" }, @@ -292,7 +292,7 @@ R"EXPECTEDRESULT( href="/ROOT%23%3F/skin/kiwix.css?cacheid=2158fad9" - +