You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.7 KiB

3 months ago
  1. # Bytes utility
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Build Status][ci-image]][ci-url]
  5. [![Test Coverage][coveralls-image]][coveralls-url]
  6. Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa.
  7. ## Installation
  8. This is a [Node.js](https://nodejs.org/en/) module available through the
  9. [npm registry](https://www.npmjs.com/). Installation is done using the
  10. [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
  11. ```bash
  12. $ npm install bytes
  13. ```
  14. ## Usage
  15. ```js
  16. var bytes = require('bytes');
  17. ```
  18. #### bytes(number|string value, [options]): number|string|null
  19. Default export function. Delegates to either `bytes.format` or `bytes.parse` based on the type of `value`.
  20. **Arguments**
  21. | Name | Type | Description |
  22. |---------|----------|--------------------|
  23. | value | `number`|`string` | Number value to format or string value to parse |
  24. | options | `Object` | Conversion options for `format` |
  25. **Returns**
  26. | Name | Type | Description |
  27. |---------|------------------|-------------------------------------------------|
  28. | results | `string`|`number`|`null` | Return null upon error. Numeric value in bytes, or string value otherwise. |
  29. **Example**
  30. ```js
  31. bytes(1024);
  32. // output: '1KB'
  33. bytes('1KB');
  34. // output: 1024
  35. ```
  36. #### bytes.format(number value, [options]): string|null
  37. Format the given value in bytes into a string. If the value is negative, it is kept as such. If it is a float, it is
  38. rounded.
  39. **Arguments**
  40. | Name | Type | Description |
  41. |---------|----------|--------------------|
  42. | value | `number` | Value in bytes |
  43. | options | `Object` | Conversion options |
  44. **Options**
  45. | Property | Type | Description |
  46. |-------------------|--------|-----------------------------------------------------------------------------------------|
  47. | decimalPlaces | `number`|`null` | Maximum number of decimal places to include in output. Default value to `2`. |
  48. | fixedDecimals | `boolean`|`null` | Whether to always display the maximum number of decimal places. Default value to `false` |
  49. | thousandsSeparator | `string`|`null` | Example of values: `' '`, `','` and `'.'`... Default value to `''`. |
  50. | unit | `string`|`null` | The unit in which the result will be returned (B/KB/MB/GB/TB). Default value to `''` (which means auto detect). |
  51. | unitSeparator | `string`|`null` | Separator to use between number and unit. Default value to `''`. |
  52. **Returns**
  53. | Name | Type | Description |
  54. |---------|------------------|-------------------------------------------------|
  55. | results | `string`|`null` | Return null upon error. String value otherwise. |
  56. **Example**
  57. ```js
  58. bytes.format(1024);
  59. // output: '1KB'
  60. bytes.format(1000);
  61. // output: '1000B'
  62. bytes.format(1000, {thousandsSeparator: ' '});
  63. // output: '1 000B'
  64. bytes.format(1024 * 1.7, {decimalPlaces: 0});
  65. // output: '2KB'
  66. bytes.format(1024, {unitSeparator: ' '});
  67. // output: '1 KB'
  68. ```
  69. #### bytes.parse(string|number value): number|null
  70. Parse the string value into an integer in bytes. If no unit is given, or `value`
  71. is a number, it is assumed the value is in bytes.
  72. Supported units and abbreviations are as follows and are case-insensitive:
  73. * `b` for bytes
  74. * `kb` for kilobytes
  75. * `mb` for megabytes
  76. * `gb` for gigabytes
  77. * `tb` for terabytes
  78. * `pb` for petabytes
  79. The units are in powers of two, not ten. This means 1kb = 1024b according to this parser.
  80. **Arguments**
  81. | Name | Type | Description |
  82. |---------------|--------|--------------------|
  83. | value | `string`|`number` | String to parse, or number in bytes. |
  84. **Returns**
  85. | Name | Type | Description |
  86. |---------|-------------|-------------------------|
  87. | results | `number`|`null` | Return null upon error. Value in bytes otherwise. |
  88. **Example**
  89. ```js
  90. bytes.parse('1KB');
  91. // output: 1024
  92. bytes.parse('1024');
  93. // output: 1024
  94. bytes.parse(1024);
  95. // output: 1024
  96. ```
  97. ## License
  98. [MIT](LICENSE)
  99. [ci-image]: https://badgen.net/github/checks/visionmedia/bytes.js/master?label=ci
  100. [ci-url]: https://github.com/visionmedia/bytes.js/actions?query=workflow%3Aci
  101. [coveralls-image]: https://badgen.net/coveralls/c/github/visionmedia/bytes.js/master
  102. [coveralls-url]: https://coveralls.io/r/visionmedia/bytes.js?branch=master
  103. [downloads-image]: https://badgen.net/npm/dm/bytes
  104. [downloads-url]: https://npmjs.org/package/bytes
  105. [npm-image]: https://badgen.net/npm/v/bytes
  106. [npm-url]: https://npmjs.org/package/bytes