From 275fcc8fee628775fd8bf0f9080c53758222d8fc Mon Sep 17 00:00:00 2001 From: jos Date: Sat, 28 Dec 2019 17:12:56 +0100 Subject: [PATCH] Changed the file size reported in `preview` mode to `KB` and `MB` --- HISTORY.md | 6 ++++++ src/js/util.js | 24 ++++++++++++------------ test/util.test.js | 15 +++++++-------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 78ff8dd..7963c23 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,6 +3,12 @@ https://github.com/josdejong/jsoneditor +## not yet published, version 8.1.1 + +- Changed the file size reported in `preview` mode to `KB` and `MB` instead of + `KiB` and `MiB` in order to match the size reported by filesystems. + + ## 2019-12-18, version 8.1.0 - Implemented `popupAnchor` allowing to select a custom anchor element. diff --git a/src/js/util.js b/src/js/util.js index 075b4b8..573657e 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -1371,7 +1371,7 @@ export function isTimestamp (field, value) { /** * Return a human readable document size - * For example formatSize(7570718) outputs '7.2 MiB' + * For example formatSize(7570718) outputs '7.6 MB' * @param {number} size * @return {string} Returns a human readable size */ @@ -1380,23 +1380,23 @@ export function formatSize (size) { return size.toFixed() + ' B' } - const KiB = size / 1024 - if (KiB < 900) { - return KiB.toFixed(1) + ' KiB' + const KB = size / 1000 + if (KB < 900) { + return KB.toFixed(1) + ' KB' } - const MiB = KiB / 1024 - if (MiB < 900) { - return MiB.toFixed(1) + ' MiB' + const MB = KB / 1000 + if (MB < 900) { + return MB.toFixed(1) + ' MB' } - const GiB = MiB / 1024 - if (GiB < 900) { - return GiB.toFixed(1) + ' GiB' + const GB = MB / 1000 + if (GB < 900) { + return GB.toFixed(1) + ' GB' } - const TiB = GiB / 1024 - return TiB.toFixed(1) + ' TiB' + const TB = GB / 1000 + return TB.toFixed(1) + ' TB' } /** diff --git a/test/util.test.js b/test/util.test.js index fcbf205..549550b 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -484,14 +484,13 @@ describe('util', () => { it('should format a document size in a human readable way', () => { assert.strictEqual(formatSize(500), '500 B') - assert.strictEqual(formatSize(900), '0.9 KiB') - assert.strictEqual(formatSize(77.89 * 1024), '77.9 KiB') - assert.strictEqual(formatSize(950 * 1024), '0.9 MiB') - assert.strictEqual(formatSize(7.22 * 1024 * 1024), '7.2 MiB') - assert.strictEqual(formatSize(955.4 * 1024 * 1024), '0.9 GiB') - assert.strictEqual(formatSize(22.37 * 1024 * 1024 * 1024), '22.4 GiB') - assert.strictEqual(formatSize(1024 * 1024 * 1024 * 1024), '1.0 TiB') - }) + assert.strictEqual(formatSize(900), '0.9 KB') + assert.strictEqual(formatSize(77.89 * 1000), '77.9 KB') + assert.strictEqual(formatSize(950 * 1000), '0.9 MB') + assert.strictEqual(formatSize(7.22 * 1000 * 1000), '7.2 MB') + assert.strictEqual(formatSize(945.4 * 1000 * 1000), '0.9 GB') + assert.strictEqual(formatSize(22.37 * 1000 * 1000 * 1000), '22.4 GB') + assert.strictEqual(formatSize(1000 * 1000 * 1000 * 1000), '1.0 TB') }) it('should limit characters', () => { assert.strictEqual(limitCharacters('hello world', 11), 'hello world')