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.

125 lines
3.8 KiB

  1. # Bytes utility
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Build Status][travis-image]][travis-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.format(number value, [options]): string|null
  19. 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
  20. rounded.
  21. **Arguments**
  22. | Name | Type | Description |
  23. |---------|----------|--------------------|
  24. | value | `number` | Value in bytes |
  25. | options | `Object` | Conversion options |
  26. **Options**
  27. | Property | Type | Description |
  28. |-------------------|--------|-----------------------------------------------------------------------------------------|
  29. | decimalPlaces | `number`|`null` | Maximum number of decimal places to include in output. Default value to `2`. |
  30. | fixedDecimals | `boolean`|`null` | Whether to always display the maximum number of decimal places. Default value to `false` |
  31. | thousandsSeparator | `string`|`null` | Example of values: `' '`, `','` and `.`... Default value to `''`. |
  32. | unit | `string`|`null` | The unit in which the result will be returned (B/KB/MB/GB/TB). Default value to `''` (which means auto detect). |
  33. | unitSeparator | `string`|`null` | Separator to use between number and unit. Default value to `''`. |
  34. **Returns**
  35. | Name | Type | Description |
  36. |---------|------------------|-------------------------------------------------|
  37. | results | `string`|`null` | Return null upon error. String value otherwise. |
  38. **Example**
  39. ```js
  40. bytes(1024);
  41. // output: '1KB'
  42. bytes(1000);
  43. // output: '1000B'
  44. bytes(1000, {thousandsSeparator: ' '});
  45. // output: '1 000B'
  46. bytes(1024 * 1.7, {decimalPlaces: 0});
  47. // output: '2KB'
  48. bytes(1024, {unitSeparator: ' '});
  49. // output: '1 KB'
  50. ```
  51. #### bytes.parse(string|number value): number|null
  52. Parse the string value into an integer in bytes. If no unit is given, or `value`
  53. is a number, it is assumed the value is in bytes.
  54. Supported units and abbreviations are as follows and are case-insensitive:
  55. * `b` for bytes
  56. * `kb` for kilobytes
  57. * `mb` for megabytes
  58. * `gb` for gigabytes
  59. * `tb` for terabytes
  60. The units are in powers of two, not ten. This means 1kb = 1024b according to this parser.
  61. **Arguments**
  62. | Name | Type | Description |
  63. |---------------|--------|--------------------|
  64. | value | `string`|`number` | String to parse, or number in bytes. |
  65. **Returns**
  66. | Name | Type | Description |
  67. |---------|-------------|-------------------------|
  68. | results | `number`|`null` | Return null upon error. Value in bytes otherwise. |
  69. **Example**
  70. ```js
  71. bytes('1KB');
  72. // output: 1024
  73. bytes('1024');
  74. // output: 1024
  75. bytes(1024);
  76. // output: 1024
  77. ```
  78. ## License
  79. [MIT](LICENSE)
  80. [downloads-image]: https://img.shields.io/npm/dm/bytes.svg
  81. [downloads-url]: https://npmjs.org/package/bytes
  82. [npm-image]: https://img.shields.io/npm/v/bytes.svg
  83. [npm-url]: https://npmjs.org/package/bytes
  84. [travis-image]: https://img.shields.io/travis/visionmedia/bytes.js/master.svg
  85. [travis-url]: https://travis-ci.org/visionmedia/bytes.js
  86. [coveralls-image]: https://img.shields.io/coveralls/visionmedia/bytes.js/master.svg
  87. [coveralls-url]: https://coveralls.io/r/visionmedia/bytes.js?branch=master