codekingpro/portable-devtools
115k
1// Convert bytes to printable output, for file reporting in tarballs
2// Only supports up to GB because that's way larger than anything the registry supports anyways.
3
4const formatBytes = (bytes, space = true) => {
5 let spacer = ''
6 if (space) {
7 spacer = ' '
8 }
9
10 if (bytes < 1000) {
11 // B
12 return `${bytes}${spacer}B`
13 }
14
15 if (bytes < 999950) {
16 // kB
17 return `${(bytes / 1000).toFixed(1)}${spacer}kB`
18 }
19
20 if (bytes < 999950000) {
21 // MB
22 return `${(bytes / 1000000).toFixed(1)}${spacer}MB`
23 }
24
25 // GB
26 return `${(bytes / 1000000000).toFixed(1)}${spacer}GB`
27}
28
29module.exports = formatBytes
30 