API Docs for: 0.25.0
Show:

File: addon/utils/human-file-size.js

  1. /**
  2. * @module ember-osf
  3. * @submodule utils
  4. */
  5.  
  6. /**
  7. * @class human-file-size
  8. */
  9.  
  10. /**
  11. * Borrowed from osf code, transforms received number of bytes size into human-
  12. * readable sizing.
  13. *
  14. * @method humanFileSize
  15. * @param {Number|String} bytes Number of bytes
  16. * @param {Boolean} bool Whether to use 1000 as the base for conversion or 1024
  17. * @return {String}
  18. */
  19. export default function humanFileSize(bytes, si) {
  20. //Borrowed from osfHelpers:
  21. //https://github.com/CenterForOpenScience/osf.io/blob/develop/website/static/js/osfHelpers.js#L645
  22. var thresh = si ? 1000 : 1024;
  23. if(Math.abs(bytes) < thresh) {
  24. return bytes + ' B';
  25. }
  26. var units = si ?
  27. ['kB','MB','GB','TB','PB','EB','ZB','YB'] :
  28. ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
  29. var u = -1;
  30. do {
  31. bytes /= thresh;
  32. ++u;
  33. } while(Math.abs(bytes) >= thresh && u < units.length - 1);
  34. return bytes.toFixed(1) + ' ' + units[u];
  35. }
  36.