index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 formatThousandsRegExp = /\B(?=(\d{3})+(?!\d))/g;
  20. var formatDecimalsRegExp = /(?:\.0*|(\.[^0]+)0+)$/;
  21. var map = {
  22. b: 1,
  23. kb: 1 << 10,
  24. mb: 1 << 20,
  25. gb: 1 << 30,
  26. tb: ((1 << 30) * 1024)
  27. };
  28. // TODO: use is-finite module?
  29. var numberIsFinite = Number.isFinite || function (v) { return typeof v === 'number' && isFinite(v); };
  30. var parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb)$/i;
  31. /**
  32. * Convert the given value in bytes into a string or parse to string to an integer in bytes.
  33. *
  34. * @param {string|number} value
  35. * @param {{
  36. * case: [string],
  37. * decimalPlaces: [number]
  38. * fixedDecimals: [boolean]
  39. * thousandsSeparator: [string]
  40. * unitSeparator: [string]
  41. * }} [options] bytes options.
  42. *
  43. * @returns {string|number|null}
  44. */
  45. function bytes(value, options) {
  46. if (typeof value === 'string') {
  47. return parse(value);
  48. }
  49. if (typeof value === 'number') {
  50. return format(value, options);
  51. }
  52. return null;
  53. }
  54. /**
  55. * Format the given value in bytes into a string.
  56. *
  57. * If the value is negative, it is kept as such. If it is a float,
  58. * it is rounded.
  59. *
  60. * @param {number} value
  61. * @param {object} [options]
  62. * @param {number} [options.decimalPlaces=2]
  63. * @param {number} [options.fixedDecimals=false]
  64. * @param {string} [options.thousandsSeparator=]
  65. * @param {string} [options.unitSeparator=]
  66. *
  67. * @returns {string|null}
  68. * @public
  69. */
  70. function format(value, options) {
  71. if (!numberIsFinite(value)) {
  72. return null;
  73. }
  74. var mag = Math.abs(value);
  75. var thousandsSeparator = (options && options.thousandsSeparator) || '';
  76. var unitSeparator = (options && options.unitSeparator) || '';
  77. var decimalPlaces = (options && options.decimalPlaces !== undefined) ? options.decimalPlaces : 2;
  78. var fixedDecimals = Boolean(options && options.fixedDecimals);
  79. var unit = 'B';
  80. if (mag >= map.tb) {
  81. unit = 'TB';
  82. } else if (mag >= map.gb) {
  83. unit = 'GB';
  84. } else if (mag >= map.mb) {
  85. unit = 'MB';
  86. } else if (mag >= map.kb) {
  87. unit = 'kB';
  88. }
  89. var val = value / map[unit.toLowerCase()];
  90. var str = val.toFixed(decimalPlaces);
  91. if (!fixedDecimals) {
  92. str = str.replace(formatDecimalsRegExp, '$1');
  93. }
  94. if (thousandsSeparator) {
  95. str = str.replace(formatThousandsRegExp, thousandsSeparator);
  96. }
  97. return str + unitSeparator + unit;
  98. }
  99. /**
  100. * Parse the string value into an integer in bytes.
  101. *
  102. * If no unit is given, it is assumed the value is in bytes.
  103. *
  104. * @param {number|string} val
  105. *
  106. * @returns {number|null}
  107. * @public
  108. */
  109. function parse(val) {
  110. if (typeof val === 'number' && !isNaN(val)) {
  111. return val;
  112. }
  113. if (typeof val !== 'string') {
  114. return null;
  115. }
  116. // Test if the string passed is valid
  117. var results = parseRegExp.exec(val);
  118. var floatValue;
  119. var unit = 'b';
  120. if (!results) {
  121. // Nothing could be extracted from the given string
  122. floatValue = parseInt(val, 10);
  123. unit = 'b'
  124. } else {
  125. // Retrieve the value and the unit
  126. floatValue = parseFloat(results[1]);
  127. unit = results[4].toLowerCase();
  128. }
  129. return Math.floor(map[unit] * floatValue);
  130. }