diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts
index 5cc459a..5037545 100644
--- a/src/lib/helpers.ts
+++ b/src/lib/helpers.ts
@@ -8,22 +8,23 @@ export function getFileContent(file: Buffer | string): string {
return buffer.toString();
}
-const BYTE_SIZES = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
+const BYTE_SIZES = ['B', 'K', 'M', 'G', 'TB', 'PB', 'EB', 'ZB', 'YB'];
// Bytes
const SIZE_BASE = 1024;
/**
* Format number of bytes as string
- * Source @see https://stackoverflow.com/a/18650828/388951
*/
-export function formatBytes(bytes: number, decimals = 2): string {
- if (bytes === 0) return `0 ${BYTE_SIZES[0]}`;
+export function formatBytes(bytes: number, maximumSignificantDigits = 3): string {
+ if (bytes === 0) return `0${BYTE_SIZES[0]}`;
const exponent = Math.floor(Math.log(bytes) / Math.log(SIZE_BASE));
const value = bytes / Math.pow(SIZE_BASE, exponent);
// `parseFloat` removes trailing zero
- return `${parseFloat(value.toFixed(decimals))} ${BYTE_SIZES[exponent]}`;
+ return `${new Intl.NumberFormat('default', { maximumSignificantDigits }).format(value)}${
+ BYTE_SIZES[exponent]
+ }`;
}
export function formatPercent(value: number, total: number, fractionDigits?: number): string {
diff --git a/tests/e2e/__snapshots__/cli.test.ts.snap b/tests/e2e/__snapshots__/cli.test.ts.snap
index 15ee840..4ee7cf2 100644
--- a/tests/e2e/__snapshots__/cli.test.ts.snap
+++ b/tests/e2e/__snapshots__/cli.test.ts.snap
@@ -54,9 +54,9 @@ exports['CLI should output multiple bundles result as html 1'] = `
@@ -68,7 +68,7 @@ exports['CLI should output multiple bundles result as html 1'] = `