index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.byteSize = factory());
  5. }(this, (function () { 'use strict';
  6. /**
  7. * @module byte-size
  8. */
  9. let defaultOptions = {};
  10. const _options = new WeakMap();
  11. class ByteSize {
  12. constructor (bytes, options) {
  13. options = Object.assign({
  14. units: 'metric',
  15. precision: 1
  16. }, defaultOptions, options);
  17. _options.set(this, options);
  18. const tables = {
  19. metric: [
  20. { from: 0 , to: 1e3 , unit: 'B' , long: 'bytes' },
  21. { from: 1e3 , to: 1e6 , unit: 'kB', long: 'kilobytes' },
  22. { from: 1e6 , to: 1e9 , unit: 'MB', long: 'megabytes' },
  23. { from: 1e9 , to: 1e12, unit: 'GB', long: 'gigabytes' },
  24. { from: 1e12, to: 1e15, unit: 'TB', long: 'terabytes' },
  25. { from: 1e15, to: 1e18, unit: 'PB', long: 'petabytes' },
  26. { from: 1e18, to: 1e21, unit: 'EB', long: 'exabytes' },
  27. { from: 1e21, to: 1e24, unit: 'ZB', long: 'zettabytes' },
  28. { from: 1e24, to: 1e27, unit: 'YB', long: 'yottabytes' },
  29. ],
  30. metric_octet: [
  31. { from: 0 , to: 1e3 , unit: 'o' , long: 'octets' },
  32. { from: 1e3 , to: 1e6 , unit: 'ko', long: 'kilooctets' },
  33. { from: 1e6 , to: 1e9 , unit: 'Mo', long: 'megaoctets' },
  34. { from: 1e9 , to: 1e12, unit: 'Go', long: 'gigaoctets' },
  35. { from: 1e12, to: 1e15, unit: 'To', long: 'teraoctets' },
  36. { from: 1e15, to: 1e18, unit: 'Po', long: 'petaoctets' },
  37. { from: 1e18, to: 1e21, unit: 'Eo', long: 'exaoctets' },
  38. { from: 1e21, to: 1e24, unit: 'Zo', long: 'zettaoctets' },
  39. { from: 1e24, to: 1e27, unit: 'Yo', long: 'yottaoctets' },
  40. ],
  41. iec: [
  42. { from: 0 , to: Math.pow(1024, 1), unit: 'B' , long: 'bytes' },
  43. { from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'KiB', long: 'kibibytes' },
  44. { from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'MiB', long: 'mebibytes' },
  45. { from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'GiB', long: 'gibibytes' },
  46. { from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'TiB', long: 'tebibytes' },
  47. { from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'PiB', long: 'pebibytes' },
  48. { from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'EiB', long: 'exbibytes' },
  49. { from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'ZiB', long: 'zebibytes' },
  50. { from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'YiB', long: 'yobibytes' },
  51. ],
  52. iec_octet: [
  53. { from: 0 , to: Math.pow(1024, 1), unit: 'o' , long: 'octets' },
  54. { from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'Kio', long: 'kibioctets' },
  55. { from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'Mio', long: 'mebioctets' },
  56. { from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'Gio', long: 'gibioctets' },
  57. { from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'Tio', long: 'tebioctets' },
  58. { from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'Pio', long: 'pebioctets' },
  59. { from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'Eio', long: 'exbioctets' },
  60. { from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'Zio', long: 'zebioctets' },
  61. { from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'Yio', long: 'yobioctets' },
  62. ],
  63. };
  64. Object.assign(tables, options.customUnits);
  65. const prefix = bytes < 0 ? '-' : '';
  66. bytes = Math.abs(bytes);
  67. const table = tables[options.units];
  68. if (table) {
  69. const units = table.find(u => bytes >= u.from && bytes < u.to);
  70. if (units) {
  71. const value = units.from === 0
  72. ? prefix + bytes
  73. : prefix + (bytes / units.from).toFixed(options.precision);
  74. this.value = value;
  75. this.unit = units.unit;
  76. this.long = units.long;
  77. } else {
  78. this.value = prefix + bytes;
  79. this.unit = '';
  80. this.long = '';
  81. }
  82. } else {
  83. throw new Error(`Invalid units specified: ${options.units}`)
  84. }
  85. }
  86. toString () {
  87. const options = _options.get(this);
  88. return options.toStringFn ? options.toStringFn.bind(this)() : `${this.value} ${this.unit}`
  89. }
  90. }
  91. /**
  92. * Returns an object with the spec `{ value: string, unit: string, long: string }`. The returned object defines a `toString` method meaning it can be used in any string context.
  93. * @param {number} - The bytes value to convert.
  94. * @param [options] {object} - Optional config.
  95. * @param [options.precision] {number} - Number of decimal places. Defaults to `1`.
  96. * @param [options.units] {string} - Specify `'metric'`, `'iec'`, `'metric_octet'`, `'iec_octet'` or the name of a property from the custom units table in `options.customUnits`. Defaults to `metric`.
  97. * @param [options.customUnits] {object} - An object containing one or more custom unit lookup tables.
  98. * @param [options.toStringFn] {function} - A `toString` function to override the default.
  99. * @returns {object}
  100. * @alias module:byte-size
  101. */
  102. function byteSize (bytes, options) {
  103. return new ByteSize(bytes, options)
  104. }
  105. /**
  106. * Set the default `byteSize` options for the duration of the process.
  107. * @param options {object} - A `byteSize` options object.
  108. */
  109. byteSize.defaultOptions = function (options) {
  110. defaultOptions = options;
  111. };
  112. return byteSize;
  113. })));