Readme.md 4.66 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
3
4
# Bytes utility

[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
5
[![Build Status][ci-image]][ci-url]
Rosanny Sihombing's avatar
Rosanny Sihombing committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[![Test Coverage][coveralls-image]][coveralls-url]

Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa.

## Installation

This is a [Node.js](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/). Installation is done using the
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):

```bash
$ npm install bytes
```

## Usage

```js
var bytes = require('bytes');
```

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#### bytes(number|string value, [options]): number|string|null

Default export function. Delegates to either `bytes.format` or `bytes.parse` based on the type of `value`.

**Arguments**

| Name    | Type     | Description        |
|---------|----------|--------------------|
| value   | `number``string` | Number value to format or string value to parse |
| options | `Object` | Conversion options for `format` |

**Returns**

| Name    | Type             | Description                                     |
|---------|------------------|-------------------------------------------------|
| results | `string``number``null` | Return null upon error. Numeric value in bytes, or string value otherwise. |

**Example**

```js
bytes(1024);
// output: '1KB'

bytes('1KB');
// output: 1024
```

Rosanny Sihombing's avatar
Rosanny Sihombing committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#### bytes.format(number value, [options]): string|null

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
 rounded.

**Arguments**

| Name    | Type     | Description        |
|---------|----------|--------------------|
| value   | `number` | Value in bytes     |
| options | `Object` | Conversion options |

**Options**

| Property          | Type   | Description                                                                             |
|-------------------|--------|-----------------------------------------------------------------------------------------|
| decimalPlaces | `number``null` | Maximum number of decimal places to include in output. Default value to `2`. |
| fixedDecimals | `boolean``null` | Whether to always display the maximum number of decimal places. Default value to `false` |
71
| thousandsSeparator | `string``null` | Example of values: `' '`, `','` and `'.'`... Default value to `''`. |
Rosanny Sihombing's avatar
Rosanny Sihombing committed
72
73
74
75
76
77
78
79
80
81
82
83
| unit | `string``null` | The unit in which the result will be returned (B/KB/MB/GB/TB). Default value to `''` (which means auto detect). |
| unitSeparator | `string``null` | Separator to use between number and unit. Default value to `''`. |

**Returns**

| Name    | Type             | Description                                     |
|---------|------------------|-------------------------------------------------|
| results | `string``null` | Return null upon error. String value otherwise. |

**Example**

```js
84
bytes.format(1024);
Rosanny Sihombing's avatar
Rosanny Sihombing committed
85
86
// output: '1KB'

87
bytes.format(1000);
Rosanny Sihombing's avatar
Rosanny Sihombing committed
88
89
// output: '1000B'

90
bytes.format(1000, {thousandsSeparator: ' '});
Rosanny Sihombing's avatar
Rosanny Sihombing committed
91
92
// output: '1 000B'

93
bytes.format(1024 * 1.7, {decimalPlaces: 0});
Rosanny Sihombing's avatar
Rosanny Sihombing committed
94
95
// output: '2KB'

96
bytes.format(1024, {unitSeparator: ' '});
Rosanny Sihombing's avatar
Rosanny Sihombing committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// output: '1 KB'
```

#### bytes.parse(string|number value): number|null

Parse the string value into an integer in bytes. If no unit is given, or `value`
is a number, it is assumed the value is in bytes.

Supported units and abbreviations are as follows and are case-insensitive:

  * `b` for bytes
  * `kb` for kilobytes
  * `mb` for megabytes
  * `gb` for gigabytes
  * `tb` for terabytes
  * `pb` for petabytes

The units are in powers of two, not ten. This means 1kb = 1024b according to this parser.

**Arguments**

| Name          | Type   | Description        |
|---------------|--------|--------------------|
| value   | `string``number` | String to parse, or number in bytes.   |

**Returns**

| Name    | Type        | Description             |
|---------|-------------|-------------------------|
| results | `number``null` | Return null upon error. Value in bytes otherwise. |

**Example**

```js
131
bytes.parse('1KB');
Rosanny Sihombing's avatar
Rosanny Sihombing committed
132
133
// output: 1024

134
bytes.parse('1024');
Rosanny Sihombing's avatar
Rosanny Sihombing committed
135
136
// output: 1024

137
138
bytes.parse(1024);
// output: 1024
Rosanny Sihombing's avatar
Rosanny Sihombing committed
139
140
```

141
## License
Rosanny Sihombing's avatar
Rosanny Sihombing committed
142
143
144

[MIT](LICENSE)

145
146
[ci-image]: https://badgen.net/github/checks/visionmedia/bytes.js/master?label=ci
[ci-url]: https://github.com/visionmedia/bytes.js/actions?query=workflow%3Aci
Rosanny Sihombing's avatar
Rosanny Sihombing committed
147
148
149
150
[coveralls-image]: https://badgen.net/coveralls/c/github/visionmedia/bytes.js/master
[coveralls-url]: https://coveralls.io/r/visionmedia/bytes.js?branch=master
[downloads-image]: https://badgen.net/npm/dm/bytes
[downloads-url]: https://npmjs.org/package/bytes
151
[npm-image]: https://badgen.net/npm/v/bytes
Rosanny Sihombing's avatar
Rosanny Sihombing committed
152
[npm-url]: https://npmjs.org/package/bytes