1 |
3a515b92
|
cagy
|
# 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 |
|
|
* `pb` for petabytes
|
87 |
|
|
|
88 |
|
|
The units are in powers of two, not ten. This means 1kb = 1024b according to this parser.
|
89 |
|
|
|
90 |
|
|
**Arguments**
|
91 |
|
|
|
92 |
|
|
| Name | Type | Description |
|
93 |
|
|
|---------------|--------|--------------------|
|
94 |
|
|
| value | `string`|`number` | String to parse, or number in bytes. |
|
95 |
|
|
|
96 |
|
|
**Returns**
|
97 |
|
|
|
98 |
|
|
| Name | Type | Description |
|
99 |
|
|
|---------|-------------|-------------------------|
|
100 |
|
|
| results | `number`|`null` | Return null upon error. Value in bytes otherwise. |
|
101 |
|
|
|
102 |
|
|
**Example**
|
103 |
|
|
|
104 |
|
|
```js
|
105 |
|
|
bytes('1KB');
|
106 |
|
|
// output: 1024
|
107 |
|
|
|
108 |
|
|
bytes('1024');
|
109 |
|
|
// output: 1024
|
110 |
|
|
|
111 |
|
|
bytes(1024);
|
112 |
|
|
// output: 1KB
|
113 |
|
|
```
|
114 |
|
|
|
115 |
|
|
## License
|
116 |
|
|
|
117 |
|
|
[MIT](LICENSE)
|
118 |
|
|
|
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
|