Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading