format-bytes.js 585 B

12345678910111213141516171819202122
  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
  3. // supports anyways.
  4. const formatBytes = (bytes, space = true) => {
  5. let spacer = ''
  6. if (space)
  7. spacer = ' '
  8. if (bytes < 1000) // B
  9. return `${bytes}${spacer}B`
  10. if (bytes < 1000000) // kB
  11. return `${(bytes / 1000).toFixed(1)}${spacer}kB`
  12. if (bytes < 1000000000) // MB
  13. return `${(bytes / 1000000).toFixed(1)}${spacer}MB`
  14. return `${(bytes / 1000000000).toFixed(1)}${spacer}GB`
  15. }
  16. module.exports = formatBytes