Skip to content

Commit

Permalink
#146 Don’t format small sizes as kB.
Browse files Browse the repository at this point in the history
* Don’t format small sizes as kB.

Currently byte sizes are always shown with a kb approximation. This is not helpful for  small sizes, e.g. 5 bytes (~0 kb). Instead leave out the kb approximation for sizes < 1kB. Also:
- change from kb (kilobit) to kB (kilobyte)
- show as MB for sizes >= 1MB
- don’t use decimals for approximate sizes < 1MB. The difference between e.g. 67 kB and 68 kB is small, and integer sizes are more glanceable.

* Don’t format small sizes as kB.

Currently byte sizes are always shown with a kb approximation. This is not helpful for  small sizes, e.g. 5 bytes (~0 kb). Instead leave out the kb approximation for sizes < 1kB. Also:
- change from kb (kilobit) to kB (kilobyte)
- show as MB for sizes >= 1MB
- don’t use decimals for approximate sizes < 1MB. The difference between e.g. 67 kB and 68 kB is small, and integer sizes are more glanceable.
  • Loading branch information
tobli authored and micmro committed Feb 12, 2017
1 parent c1d9cef commit 4ba02a9
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/ts/helpers/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ export function formatDateLocalized(date: Date): string {
return `${date.toUTCString()}</br>(local time: ${date.toLocaleString()})`;
}

const BYTES_PER_KB = 1024;
const BYTES_PER_MB = 1024 * BYTES_PER_KB;

export function formatBytes(bytes: number): string {
return `${bytes} bytes (~${roundNumber(bytes / 1024, 1)} kb)`;
const raw = `${bytes} bytes`;
if (bytes >= BYTES_PER_MB) {
return `${raw} (~${roundNumber(bytes / BYTES_PER_KB, 1)} MB)`;
}
if (bytes >= BYTES_PER_KB) {
return `${raw} (~${roundNumber(bytes / BYTES_PER_KB, 0)} kB)`;
}
return raw;
}

0 comments on commit 4ba02a9

Please sign in to comment.