bytes.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Node
  2. if (typeof module !== 'undefined' && module.exports) {
  3. var numeral = require('../../numeral');
  4. var expect = require('chai').expect;
  5. }
  6. describe('Bytes', function() {
  7. after(function() {
  8. numeral.reset();
  9. });
  10. it('should format to bytes', function() {
  11. var decimal = 1000;
  12. var binary = 1024;
  13. var tests = [
  14. [0,'0b','0B'],
  15. [null,'0 b','0 B'],
  16. [100,'0b','100B'],
  17. [binary * 2,'0 ib','2 KiB'],
  18. [Math.pow(binary, 2) * 5,'0ib','5MiB'],
  19. [Math.pow(binary, 3) * 7.343,'0.[0] ib','7.3 GiB'],
  20. [Math.pow(binary, 4) * 3.1536544,'0.000ib','3.154TiB'],
  21. [Math.pow(binary, 5) * 2.953454534534,'0ib','3PiB'],
  22. [decimal * 2,'0 b','2 KB'],
  23. [Math.pow(decimal, 2) * 5,'0b','5MB'],
  24. [Math.pow(decimal, 3) * 7.343,'0.[0] b','7.3 GB'],
  25. [Math.pow(decimal, 4) * 3.1536544,'0.000b','3.154TB'],
  26. [Math.pow(decimal, 5) * 2.953454534534,'0b','3PB']
  27. ],
  28. i;
  29. for (i = 0; i < tests.length; i++) {
  30. expect(numeral(tests[i][0]).format(tests[i][1])).to.equal(tests[i][2]);
  31. }
  32. });
  33. it('should unformat to number', function() {
  34. var decimal = 1000;
  35. var binary = 1024;
  36. var tests = [
  37. ['0B', 0],
  38. ['0 B', 0],
  39. ['100B', 100],
  40. ['2 KiB', binary * 2],
  41. ['5MiB', Math.pow(binary, 2) * 5],
  42. ['7.3 GiB', Math.pow(binary, 3) * 7.3],
  43. ['3.154TiB', Math.pow(binary, 4) * 3.154],
  44. ['3PiB', Math.pow(binary, 5) * 3],
  45. ['2 KB', decimal * 2],
  46. ['5MB', Math.pow(decimal, 2) * 5],
  47. ['7.3 GB', Math.pow(decimal, 3) * 7.3],
  48. ['3.154TB', Math.pow(decimal, 4) * 3.154],
  49. ['3PB', Math.pow(decimal, 5) * 3]
  50. ],
  51. i;
  52. for (i = 0; i < tests.length; i++) {
  53. expect(numeral(tests[i][0]).value()).to.equal(tests[i][1]);
  54. }
  55. });
  56. });