3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Build Status][travis-image]][travis-url]
7 Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa.
12 var bytes = require('bytes');
15 #### bytes.format(number value, [options]): string|null
17 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
22 | Name | Type | Description |
23 |---------|--------|--------------------|
24 | value | `number` | Value in bytes |
25 | options | `Object` | Conversion options |
29 | Property | Type | Description |
30 |-------------------|--------|-----------------------------------------------------------------------------------------|
31 | decimalPlaces | `number`|`null` | Maximum number of decimal places to include in output. Default value to `2`. |
32 | fixedDecimals | `boolean`|`null` | Whether to always display the maximum number of decimal places. Default value to `false` |
33 | thousandsSeparator | `string`|`null` | Example of values: `' '`, `','` and `.`... Default value to `' '`. |
34 | unitSeparator | `string`|`null` | Separator to use between number and unit. Default value to `''`. |
38 | Name | Type | Description |
39 |---------|-------------|-------------------------|
40 | results | `string`|`null` | Return null upon error. String value otherwise. |
51 bytes(1000, {thousandsSeparator: ' '});
54 bytes(1024 * 1.7, {decimalPlaces: 0});
57 bytes(1024, {unitSeparator: ' '});
62 #### bytes.parse(string value): number|null
64 Parse the string value into an integer in bytes. If no unit is given, it is assumed the value is in bytes.
66 Supported units and abbreviations are as follows and are case-insensitive:
74 The units are in powers of two, not ten. This means 1kb = 1024b according to this parser.
78 | Name | Type | Description |
79 |---------------|--------|--------------------|
80 | value | `string` | String to parse. |
84 | Name | Type | Description |
85 |---------|-------------|-------------------------|
86 | results | `number`|`null` | Return null upon error. Value in bytes otherwise. |
101 npm install bytes --save
102 component install visionmedia/bytes.js
107 [![npm](https://img.shields.io/npm/l/express.svg)](https://github.com/visionmedia/bytes.js/blob/master/LICENSE)
109 [downloads-image]: https://img.shields.io/npm/dm/bytes.svg
110 [downloads-url]: https://npmjs.org/package/bytes
111 [npm-image]: https://img.shields.io/npm/v/bytes.svg
112 [npm-url]: https://npmjs.org/package/bytes
113 [travis-image]: https://img.shields.io/travis/visionmedia/bytes.js/master.svg
114 [travis-url]: https://travis-ci.org/visionmedia/bytes.js