1 | const UNITS = ['B', 'K', 'M', 'G', 'T', 'P', 'E'];
|
2 |
|
3 | const { floor, log, min, round } = Math;
|
4 |
|
5 | const BASE = 1024;
|
6 |
|
7 | export 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 | };
|