format-bytes.js 624 B

1234567891011121314151617181920212223242526272829
  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. const formatBytes = (bytes, space = true) => {
  4. let spacer = ''
  5. if (space) {
  6. spacer = ' '
  7. }
  8. if (bytes < 1000) {
  9. // B
  10. return `${bytes}${spacer}B`
  11. }
  12. if (bytes < 999950) {
  13. // kB
  14. return `${(bytes / 1000).toFixed(1)}${spacer}kB`
  15. }
  16. if (bytes < 999950000) {
  17. // MB
  18. return `${(bytes / 1000000).toFixed(1)}${spacer}MB`
  19. }
  20. // GB
  21. return `${(bytes / 1000000000).toFixed(1)}${spacer}GB`
  22. }
  23. module.exports = formatBytes