UNPKG

347 BJavaScriptView Raw
1const UNITS = ['B', 'K', 'M', 'G', 'T', 'P', 'E'];
2
3const { floor, log, min, round } = Math;
4
5const BASE = 1024;
6
7export default size => {
8 const i = size ? min(UNITS.length - 1, floor(log(size) / log(BASE))) : 0;
9 const s = (round((size / BASE ** i) * BASE) / BASE).toPrecision(4);
10 return new Array(6 - s.length).join(' ') + s + UNITS[i];
11};