diff options
Diffstat (limited to 'cli/js/util.ts')
-rw-r--r-- | cli/js/util.ts | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/cli/js/util.ts b/cli/js/util.ts index 54230af1f..b046a34f4 100644 --- a/cli/js/util.ts +++ b/cli/js/util.ts @@ -248,3 +248,19 @@ export function commonPath(paths: string[], sep = "/"): string { const prefix = parts.slice(0, endOfPrefix).join(sep); return prefix.endsWith(sep) ? prefix : `${prefix}${sep}`; } + +/** Utility function to turn the number of bytes into a human readable + * unit */ +export function humanFileSize(bytes: number): string { + const thresh = 1000; + if (Math.abs(bytes) < thresh) { + return bytes + " B"; + } + const units = ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; + let u = -1; + do { + bytes /= thresh; + ++u; + } while (Math.abs(bytes) >= thresh && u < units.length - 1); + return `${bytes.toFixed(1)} ${units[u]}`; +} |