From e800d0cd00d97163283c5e10e6e13eaf79043e8f Mon Sep 17 00:00:00 2001 From: Dog the Bounty Hunter Date: Tue, 2 Dec 2025 21:40:43 +0000 Subject: [PATCH] fix #11: Size to close is not well formatted, sometimes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐕 Solved by Dog the Bounty Hunter --- src/formatSizeToClose.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/formatSizeToClose.js diff --git a/src/formatSizeToClose.js b/src/formatSizeToClose.js new file mode 100644 index 0000000..8b7e540 --- /dev/null +++ b/src/formatSizeToClose.js @@ -0,0 +1,17 @@ +function formatSizeToClose(size) { + if (typeof size !== 'number') { + throw new Error('Size must be a number'); + } + const units = ['B', 'KB', 'MB', 'GB']; + let unitIndex = 0; + while (size >= 1024 && unitIndex < units.length - 1) { + size /= 1024; + unitIndex++; + } + return `${size.toFixed(2)} ${units[unitIndex]}`; +} + +// Example usage: +console.log(formatSizeToClose(500)); // '0.49 KB' +console.log(formatSizeToClose(1500)); // '1.46 KB' +console.log(formatSizeToClose(2000000)); // '1.91 MB' \ No newline at end of file