Changed the file size reported in `preview` mode to `KB` and `MB`

This commit is contained in:
jos 2019-12-28 17:12:56 +01:00
parent 7729e8b38e
commit 275fcc8fee
3 changed files with 25 additions and 20 deletions

View File

@ -3,6 +3,12 @@
https://github.com/josdejong/jsoneditor 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 ## 2019-12-18, version 8.1.0
- Implemented `popupAnchor` allowing to select a custom anchor element. - Implemented `popupAnchor` allowing to select a custom anchor element.

View File

@ -1371,7 +1371,7 @@ export function isTimestamp (field, value) {
/** /**
* Return a human readable document size * 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 * @param {number} size
* @return {string} Returns a human readable size * @return {string} Returns a human readable size
*/ */
@ -1380,23 +1380,23 @@ export function formatSize (size) {
return size.toFixed() + ' B' return size.toFixed() + ' B'
} }
const KiB = size / 1024 const KB = size / 1000
if (KiB < 900) { if (KB < 900) {
return KiB.toFixed(1) + ' KiB' return KB.toFixed(1) + ' KB'
} }
const MiB = KiB / 1024 const MB = KB / 1000
if (MiB < 900) { if (MB < 900) {
return MiB.toFixed(1) + ' MiB' return MB.toFixed(1) + ' MB'
} }
const GiB = MiB / 1024 const GB = MB / 1000
if (GiB < 900) { if (GB < 900) {
return GiB.toFixed(1) + ' GiB' return GB.toFixed(1) + ' GB'
} }
const TiB = GiB / 1024 const TB = GB / 1000
return TiB.toFixed(1) + ' TiB' return TB.toFixed(1) + ' TB'
} }
/** /**

View File

@ -484,14 +484,13 @@ describe('util', () => {
it('should format a document size in a human readable way', () => { it('should format a document size in a human readable way', () => {
assert.strictEqual(formatSize(500), '500 B') assert.strictEqual(formatSize(500), '500 B')
assert.strictEqual(formatSize(900), '0.9 KiB') assert.strictEqual(formatSize(900), '0.9 KB')
assert.strictEqual(formatSize(77.89 * 1024), '77.9 KiB') assert.strictEqual(formatSize(77.89 * 1000), '77.9 KB')
assert.strictEqual(formatSize(950 * 1024), '0.9 MiB') assert.strictEqual(formatSize(950 * 1000), '0.9 MB')
assert.strictEqual(formatSize(7.22 * 1024 * 1024), '7.2 MiB') assert.strictEqual(formatSize(7.22 * 1000 * 1000), '7.2 MB')
assert.strictEqual(formatSize(955.4 * 1024 * 1024), '0.9 GiB') assert.strictEqual(formatSize(945.4 * 1000 * 1000), '0.9 GB')
assert.strictEqual(formatSize(22.37 * 1024 * 1024 * 1024), '22.4 GiB') assert.strictEqual(formatSize(22.37 * 1000 * 1000 * 1000), '22.4 GB')
assert.strictEqual(formatSize(1024 * 1024 * 1024 * 1024), '1.0 TiB') assert.strictEqual(formatSize(1000 * 1000 * 1000 * 1000), '1.0 TB') })
})
it('should limit characters', () => { it('should limit characters', () => {
assert.strictEqual(limitCharacters('hello world', 11), 'hello world') assert.strictEqual(limitCharacters('hello world', 11), 'hello world')