3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Build Status][travis-image]][travis-url]
6 [![Test Coverage][coveralls-image]][coveralls-url]
8 Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa.
12 This is a [Node.js](https://nodejs.org/en/) module available through the
13 [npm registry](https://www.npmjs.com/). Installation is done using the
14 [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
23 var bytes = require('bytes');
26 #### bytes.format(number value, [options]): string|null
28 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
33 | Name | Type | Description |
34 |---------|----------|--------------------|
35 | value | `number` | Value in bytes |
36 | options | `Object` | Conversion options |
40 | Property | Type | Description |
41 |-------------------|--------|-----------------------------------------------------------------------------------------|
42 | decimalPlaces | `number`|`null` | Maximum number of decimal places to include in output. Default value to `2`. |
43 | fixedDecimals | `boolean`|`null` | Whether to always display the maximum number of decimal places. Default value to `false` |
44 | thousandsSeparator | `string`|`null` | Example of values: `' '`, `','` and `.`... Default value to `''`. |
45 | unit | `string`|`null` | The unit in which the result will be returned (B/KB/MB/GB/TB). Default value to `''` (which means auto detect). |
46 | unitSeparator | `string`|`null` | Separator to use between number and unit. Default value to `''`. |
50 | Name | Type | Description |
51 |---------|------------------|-------------------------------------------------|
52 | results | `string`|`null` | Return null upon error. String value otherwise. |
63 bytes(1000, {thousandsSeparator: ' '});
66 bytes(1024 * 1.7, {decimalPlaces: 0});
69 bytes(1024, {unitSeparator: ' '});
74 #### bytes.parse(string|number value): number|null
76 Parse the string value into an integer in bytes. If no unit is given, or `value`
77 is a number, it is assumed the value is in bytes.
79 Supported units and abbreviations are as follows and are case-insensitive:
88 The units are in powers of two, not ten. This means 1kb = 1024b according to this parser.
92 | Name | Type | Description |
93 |---------------|--------|--------------------|
94 | value | `string`|`number` | String to parse, or number in bytes. |
98 | Name | Type | Description |
99 |---------|-------------|-------------------------|
100 | results | `number`|`null` | Return null upon error. Value in bytes otherwise. |
119 [coveralls-image]: https://badgen.net/coveralls/c/github/visionmedia/bytes.js/master
120 [coveralls-url]: https://coveralls.io/r/visionmedia/bytes.js?branch=master
121 [downloads-image]: https://badgen.net/npm/dm/bytes
122 [downloads-url]: https://npmjs.org/package/bytes
123 [npm-image]: https://badgen.net/npm/node/bytes
124 [npm-url]: https://npmjs.org/package/bytes
125 [travis-image]: https://badgen.net/travis/visionmedia/bytes.js/master
126 [travis-url]: https://travis-ci.org/visionmedia/bytes.js