README.hbs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. [![view on npm](https://badgen.net/npm/v/byte-size)](https://www.npmjs.org/package/byte-size)
  2. [![npm module downloads](https://badgen.net/npm/dt/byte-size)](https://www.npmjs.org/package/byte-size)
  3. [![Gihub repo dependents](https://badgen.net/github/dependents-repo/75lb/byte-size)](https://github.com/75lb/byte-size/network/dependents?dependent_type=REPOSITORY)
  4. [![Gihub package dependents](https://badgen.net/github/dependents-pkg/75lb/byte-size)](https://github.com/75lb/byte-size/network/dependents?dependent_type=PACKAGE)
  5. [![Build Status](https://travis-ci.org/75lb/byte-size.svg?branch=master)](https://travis-ci.org/75lb/byte-size)
  6. [![Coverage Status](https://coveralls.io/repos/github/75lb/byte-size/badge.svg)](https://coveralls.io/github/75lb/byte-size)
  7. [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)
  8. ***Upgraders, please check the [release notes](https://github.com/75lb/byte-size/releases).***
  9. # byte-size
  10. An isomorphic, load-anywhere function to convert a bytes value (e.g. `3456`) to a human-readable string (`'3.5 kB'`). Choose between [metric or IEC units](https://en.wikipedia.org/wiki/Gigabyte) (summarised below) or specify your own custom units.
  11. Value | Metric | Metric (octet) |
  12. ----- | ------------- | -------------- |
  13. 1000 | kB kilobyte | ko kilooctet |
  14. 1000^2 | MB megabyte | Mo megaoctet |
  15. 1000^3 | GB gigabyte | Go gigaoctet |
  16. 1000^4 | TB terabyte | To teraoctet |
  17. 1000^5 | PB petabyte | Po petaoctet |
  18. 1000^6 | EB exabyte | Eo exaoctet |
  19. 1000^7 | ZB zettabyte | Zo zettaoctet |
  20. 1000^8 | YB yottabyte | Yo yottaoctet |
  21. Value | IEC | IEC (octet) |
  22. ------ | ------------ | ------------- |
  23. 1024 | KiB kibibyte | Kio kibioctet |
  24. 1024^2 | MiB mebibyte | Mio mebioctet |
  25. 1024^3 | GiB gibibyte | Gio gibioctet |
  26. 1024^4 | TiB tebibyte | Tio tebioctet |
  27. 1024^5 | PiB pebibyte | Pio pebioctet |
  28. 1024^6 | EiB exbibyte | Eio exbioctet |
  29. 1024^7 | ZiB zebibyte | Zio zebioctet |
  30. 1024^8 | YiB yobibyte | Yio yobioctet |
  31. ## Synopsis
  32. By default, `byteSize` converts the input number to a human readable string with metric units and a precision of 1.
  33. ```js
  34. > const byteSize = require('byte-size')
  35. > byteSize(1580)
  36. { value: '1.6', unit: 'kB', long: 'kilobytes' }
  37. ```
  38. The object returned by `byteSize` defines a `toString` method therefore can be used directly in string context.
  39. ```js
  40. > `Filesize: ${byteSize(12400)}`
  41. 'Filesize: 12.4 kB'
  42. ```
  43. Override the default `toString` behaviour by setting [`options.toStringFn`](#bytesizebytes-options--object-).
  44. ```js
  45. > function toStringFn () {
  46. return `**${this.value}${this.unit}**`
  47. }
  48. > `Filesize: ${byteSize(12400, { toStringFn })}`
  49. 'Filesize: **12.4kB**'
  50. ```
  51. Beside the default of `metric`, there are three other built-in units available: `metric_octet`, `iec` and `iec_octet`.
  52. ```js
  53. > byteSize(1580, { units: 'iec' })
  54. { value: '1.5', unit: 'KiB', long: 'kibibytes' }
  55. > byteSize(1580, { units: 'iec_octet' })
  56. { value: '1.5', unit: 'Kio', long: 'kibioctets' }
  57. > byteSize(1580, { units: 'metric_octet' })
  58. { value: '1.6', unit: 'ko', long: 'kilooctets' }
  59. ```
  60. You can adjust the `precision`.
  61. ```js
  62. > byteSize(1580, { units: 'iec', precision: 3 })
  63. { value: '1.543', unit: 'KiB', long: 'kibibytes' }
  64. > byteSize(1580, { units: 'iec', precision: 0 })
  65. { value: '2', unit: 'KiB', long: 'kibibytes' }
  66. ```
  67. Define custom units by passing an object containing one or more additional conversion tables to `options.customUnits`. In `options.units`, specify the name of a property from the `customUnits` object.
  68. ```js
  69. > const customUnits = {
  70. simple: [
  71. { from: 0 , to: 1e3 , unit: '' },
  72. { from: 1e3 , to: 1e6 , unit: 'K', long: 'thousand' },
  73. { from: 1e6 , to: 1e9 , unit: 'Mn', long: 'million' },
  74. { from: 1e9 , to: 1e12, unit: 'Bn', long: 'billion' }
  75. ]
  76. }
  77. > const { value, unit } = byteSize(10000, { customUnits, units: 'simple' })
  78. > `${value}${unit}`
  79. '10.0K'
  80. ```
  81. Override the built-in defaults for the duration of the process by passing an options object to `byteSize.defaultOptions()`. This results in cleaner code in cases where `byteSize` is used often with the same options.
  82. ```js
  83. > byteSize.defaultOptions({
  84. units: 'simple',
  85. precision: 2,
  86. customUnits: {
  87. simple: [
  88. { from: 0, to: 1e3, unit: '' },
  89. { from: 1e3, to: 1e6, unit: 'k' },
  90. { from: 1e6, to: 1e9, unit: 'm' },
  91. { from: 1e9, to: 1e12, unit: 'bn' },
  92. ]
  93. },
  94. toStringFn: function () {
  95. return this.value + this.unit
  96. }
  97. })
  98. > [2400, 16400, 3991200].map(byteSize).join(', ')
  99. '2.40k, 16.40k, 3.99m'
  100. ```
  101. {{>main}}
  102. ## Load anywhere
  103. This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.
  104. Node.js:
  105. ```js
  106. const byteSize = require('byte-size')
  107. ```
  108. Within Node.js with ECMAScript Module support enabled:
  109. ```js
  110. import byteSize from 'byte-size'
  111. ```
  112. Within a modern browser ECMAScript Module:
  113. ```js
  114. import byteSize from './node_modules/byte-size/index.mjs'
  115. ```
  116. Old browser (adds `window.byteSize`):
  117. ```html
  118. <script nomodule src="./node_modules/byte-size/dist/index.js"></script>
  119. ```
  120. * * *
  121. &copy; 2014-21 Lloyd Brookes \<75pound@gmail.com\>.
  122. Isomorphic test suite by [test-runner](https://github.com/test-runner-js/test-runner) and [web-runner](https://github.com/test-runner-js/web-runner). Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).