mirror of
https://github.com/itdoginfo/podkop.git
synced 2025-12-06 11:36:50 +03:00
13 lines
434 B
TypeScript
13 lines
434 B
TypeScript
// steal from https://github.com/sindresorhus/pretty-bytes/blob/master/index.js
|
|
export function prettyBytes(n: number) {
|
|
const UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
|
|
if (n < 1000) {
|
|
return n + ' B';
|
|
}
|
|
const exponent = Math.min(Math.floor(Math.log10(n) / 3), UNITS.length - 1);
|
|
n = Number((n / Math.pow(1000, exponent)).toPrecision(3));
|
|
const unit = UNITS[exponent];
|
|
return n + ' ' + unit;
|
|
}
|