summaryrefslogtreecommitdiff
path: root/src/misc/pretty-bytes.ts
blob: 68b67769affda60b59af186af5bc8bdd7b155301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
// steal from https://github.com/sindresorhus/pretty-bytes/blob/master/index.js

const UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

export default function prettyBytes(n: number) {
  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;
}