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.

Readme.md 3.8KB

5 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * `pb` for petabytes
  61. The units are in powers of two, not ten. This means 1kb = 1024b according to this parser.
  62. **Arguments**
  63. | Name | Type | Description |
  64. |---------------|--------|--------------------|
  65. | value | `string`|`number` | String to parse, or number in bytes. |
  66. **Returns**
  67. | Name | Type | Description |
  68. |---------|-------------|-------------------------|
  69. | results | `number`|`null` | Return null upon error. Value in bytes otherwise. |
  70. **Example**
  71. ```js
  72. bytes('1KB');
  73. // output: 1024
  74. bytes('1024');
  75. // output: 1024
  76. bytes(1024);
  77. // output: 1KB
  78. ```
  79. ## License
  80. [MIT](LICENSE)
  81. [coveralls-image]: https://badgen.net/coveralls/c/github/visionmedia/bytes.js/master
  82. [coveralls-url]: https://coveralls.io/r/visionmedia/bytes.js?branch=master
  83. [downloads-image]: https://badgen.net/npm/dm/bytes
  84. [downloads-url]: https://npmjs.org/package/bytes
  85. [npm-image]: https://badgen.net/npm/node/bytes
  86. [npm-url]: https://npmjs.org/package/bytes
  87. [travis-image]: https://badgen.net/travis/visionmedia/bytes.js/master
  88. [travis-url]: https://travis-ci.org/visionmedia/bytes.js