index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*!
  2. * bytes
  3. * Copyright(c) 2012-2014 TJ Holowaychuk
  4. * Copyright(c) 2015 Jed Watson
  5. * MIT Licensed
  6. */
  7. 'use strict';
  8. /**
  9. * Module exports.
  10. * @public
  11. */
  12. module.exports = bytes;
  13. module.exports.format = format;
  14. module.exports.parse = parse;
  15. /**
  16. * Module variables.
  17. * @private
  18. */
  19. var map = {
  20. b: 1,
  21. kb: 1 << 10,
  22. mb: 1 << 20,
  23. gb: 1 << 30,
  24. tb: ((1 << 30) * 1024)
  25. };
  26. /**
  27. *Convert the given value in bytes into a string or parse to string to an integer in bytes.
  28. *
  29. * @param {string|number} value
  30. * @param {{
  31. * case: [string],
  32. * thousandsSeparator: [string]
  33. * }} [options] bytes options.
  34. *
  35. * @returns {string|number|null}
  36. */
  37. function bytes(value, options) {
  38. if (typeof value === 'string') {
  39. return parse(value);
  40. }
  41. if (typeof value === 'number') {
  42. return format(value, options);
  43. }
  44. return null;
  45. }
  46. /**
  47. * Format the given value in bytes into a string.
  48. *
  49. * If the value is negative, it is kept as such. If it is a float,
  50. * it is rounded.
  51. *
  52. * @param {number} value
  53. * @param {object} [options]
  54. * @param {string} [options.thousandsSeparator=]
  55. * @public
  56. */
  57. function format(val, options) {
  58. if (typeof val !== 'number') {
  59. return null;
  60. }
  61. var mag = Math.abs(val);
  62. var thousandsSeparator = (options && options.thousandsSeparator) || '';
  63. var unit = 'B';
  64. var value = val;
  65. if (mag >= map.tb) {
  66. value = Math.round(value / map.tb * 100) / 100;
  67. unit = 'TB';
  68. } else if (mag >= map.gb) {
  69. value = Math.round(value / map.gb * 100) / 100;
  70. unit = 'GB';
  71. } else if (mag >= map.mb) {
  72. value = Math.round(value / map.mb * 100) / 100;
  73. unit = 'MB';
  74. } else if (mag >= map.kb) {
  75. value = Math.round(value / map.kb * 100) / 100;
  76. unit = 'kB';
  77. }
  78. if (thousandsSeparator) {
  79. value = value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparator);
  80. }
  81. return value + unit;
  82. }
  83. /**
  84. * Parse the string value into an integer in bytes.
  85. *
  86. * If no unit is given, it is assumed the value is in bytes.
  87. *
  88. * @param {number|string} val
  89. * @public
  90. */
  91. function parse(val) {
  92. if (typeof val === 'number' && !isNaN(val)) {
  93. return val;
  94. }
  95. if (typeof val !== 'string') {
  96. return null;
  97. }
  98. // Test if the string passed is valid
  99. var results = val.match(/^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb)$/i);
  100. var floatValue;
  101. var unit = 'b';
  102. if (!results) {
  103. // Nothing could be extracted from the given string
  104. floatValue = parseInt(val);
  105. unit = 'b'
  106. } else {
  107. // Retrieve the value and the unit
  108. floatValue = parseFloat(results[1]);
  109. unit = results[4].toLowerCase();
  110. }
  111. return map[unit] * floatValue;
  112. }