From 119b71aa0dfe20c0fdc45e7f23b1d7494c101142 Mon Sep 17 00:00:00 2001 From: Mathias Wulff Date: Fri, 23 Oct 2026 05:13:59 +1100 Subject: [PATCH] Give size as 0 Bytes for empty content in --replacement-js to close #687 --- src/engine.ts | 3 ++- test/js/replacement-js.test.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/engine.ts b/src/engine.ts index f802208f..c9b4d2e4 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -430,7 +430,8 @@ function readableSize(size) { if (1 === size) { return '1 Byte'; } - const i = Math.floor(Math.log(size) / Math.log(1024)); + // 0 has no logarithm, so it is counted in bytes + const i = size ? Math.floor(Math.log(size) / Math.log(1024)) : 0; return ( (size / Math.pow(1024, i)).toFixed(!!i ? 1 : 0) + ' ' + ['Bytes', 'KB', 'MB', 'GB', 'TB'][i] ); diff --git a/test/js/replacement-js.test.js b/test/js/replacement-js.test.js index c9fcf852..a3f045da 100644 --- a/test/js/replacement-js.test.js +++ b/test/js/replacement-js.test.js @@ -194,7 +194,7 @@ describe('--replacement-js', () => { expect(await js('foobar', 'bytes + "|" + size', 'foobar')).toBe('6|6 Bytes'); }); - test.todo('BUG #687: size of empty content is "0 Bytes"', async () => { + test('size of empty content is "0 Bytes" (#687)', async () => { expect(await inFile('bytes + "|" + size', '')).toBe('0|0 Bytes'); });