1 | # Bytes utility
|
---|
2 |
|
---|
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]
|
---|
7 |
|
---|
8 | Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa.
|
---|
9 |
|
---|
10 | ## Installation
|
---|
11 |
|
---|
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):
|
---|
15 |
|
---|
16 | ```bash
|
---|
17 | $ npm install bytes
|
---|
18 | ```
|
---|
19 |
|
---|
20 | ## Usage
|
---|
21 |
|
---|
22 | ```js
|
---|
23 | var bytes = require('bytes');
|
---|
24 | ```
|
---|
25 |
|
---|
26 | #### bytes.format(number value, [options]): string|null
|
---|
27 |
|
---|
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
|
---|
29 | rounded.
|
---|
30 |
|
---|
31 | **Arguments**
|
---|
32 |
|
---|
33 | | Name | Type | Description |
|
---|
34 | |---------|----------|--------------------|
|
---|
35 | | value | `number` | Value in bytes |
|
---|
36 | | options | `Object` | Conversion options |
|
---|
37 |
|
---|
38 | **Options**
|
---|
39 |
|
---|
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 `''`. |
|
---|
47 |
|
---|
48 | **Returns**
|
---|
49 |
|
---|
50 | | Name | Type | Description |
|
---|
51 | |---------|------------------|-------------------------------------------------|
|
---|
52 | | results | `string`|`null` | Return null upon error. String value otherwise. |
|
---|
53 |
|
---|
54 | **Example**
|
---|
55 |
|
---|
56 | ```js
|
---|
57 | bytes(1024);
|
---|
58 | // output: '1KB'
|
---|
59 |
|
---|
60 | bytes(1000);
|
---|
61 | // output: '1000B'
|
---|
62 |
|
---|
63 | bytes(1000, {thousandsSeparator: ' '});
|
---|
64 | // output: '1 000B'
|
---|
65 |
|
---|
66 | bytes(1024 * 1.7, {decimalPlaces: 0});
|
---|
67 | // output: '2KB'
|
---|
68 |
|
---|
69 | bytes(1024, {unitSeparator: ' '});
|
---|
70 | // output: '1 KB'
|
---|
71 |
|
---|
72 | ```
|
---|
73 |
|
---|
74 | #### bytes.parse(string|number value): number|null
|
---|
75 |
|
---|
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.
|
---|
78 |
|
---|
79 | Supported units and abbreviations are as follows and are case-insensitive:
|
---|
80 |
|
---|
81 | * `b` for bytes
|
---|
82 | * `kb` for kilobytes
|
---|
83 | * `mb` for megabytes
|
---|
84 | * `gb` for gigabytes
|
---|
85 | * `tb` for terabytes
|
---|
86 |
|
---|
87 | The units are in powers of two, not ten. This means 1kb = 1024b according to this parser.
|
---|
88 |
|
---|
89 | **Arguments**
|
---|
90 |
|
---|
91 | | Name | Type | Description |
|
---|
92 | |---------------|--------|--------------------|
|
---|
93 | | value | `string`|`number` | String to parse, or number in bytes. |
|
---|
94 |
|
---|
95 | **Returns**
|
---|
96 |
|
---|
97 | | Name | Type | Description |
|
---|
98 | |---------|-------------|-------------------------|
|
---|
99 | | results | `number`|`null` | Return null upon error. Value in bytes otherwise. |
|
---|
100 |
|
---|
101 | **Example**
|
---|
102 |
|
---|
103 | ```js
|
---|
104 | bytes('1KB');
|
---|
105 | // output: 1024
|
---|
106 |
|
---|
107 | bytes('1024');
|
---|
108 | // output: 1024
|
---|
109 |
|
---|
110 | bytes(1024);
|
---|
111 | // output: 1024
|
---|
112 | ```
|
---|
113 |
|
---|
114 | ## License
|
---|
115 |
|
---|
116 | [MIT](LICENSE)
|
---|
117 |
|
---|
118 | [downloads-image]: https://img.shields.io/npm/dm/bytes.svg
|
---|
119 | [downloads-url]: https://npmjs.org/package/bytes
|
---|
120 | [npm-image]: https://img.shields.io/npm/v/bytes.svg
|
---|
121 | [npm-url]: https://npmjs.org/package/bytes
|
---|
122 | [travis-image]: https://img.shields.io/travis/visionmedia/bytes.js/master.svg
|
---|
123 | [travis-url]: https://travis-ci.org/visionmedia/bytes.js
|
---|
124 | [coveralls-image]: https://img.shields.io/coveralls/visionmedia/bytes.js/master.svg
|
---|
125 | [coveralls-url]: https://coveralls.io/r/visionmedia/bytes.js?branch=master
|
---|