Skip to content

Commit

Permalink
Changed the file size reported in preview mode to KB and MB
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Dec 28, 2019
1 parent 7729e8b commit 275fcc8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 12 additions & 12 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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'
}

/**
Expand Down
15 changes: 7 additions & 8 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 275fcc8

Please sign in to comment.