From 4ba02a96f5adbf0de3bd3e4b142e8f6e41aadcd4 Mon Sep 17 00:00:00 2001 From: Tobias Lidskog Date: Sun, 12 Feb 2017 13:52:24 +0100 Subject: [PATCH] =?UTF-8?q?#146=20Don=E2=80=99t=20format=20small=20sizes?= =?UTF-8?q?=20as=20kB.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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. --- src/ts/helpers/parse.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/ts/helpers/parse.ts b/src/ts/helpers/parse.ts index f1bbadb4..389bf09a 100644 --- a/src/ts/helpers/parse.ts +++ b/src/ts/helpers/parse.ts @@ -87,6 +87,16 @@ export function formatDateLocalized(date: Date): string { return `${date.toUTCString()}
(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; }