Readme.md 3.8 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]
Rosanny Sihombing's avatar
Rosanny Sihombing committed
5
[![Build Status][travis-image]][travis-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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[![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');
```

#### 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` |
Rosanny Sihombing's avatar
Rosanny Sihombing committed
44
| thousandsSeparator | `string``null` | Example of values: `' '`, `','` and `.`... Default value to `''`. |
Rosanny Sihombing's avatar
Rosanny Sihombing committed
45
46
47
48
49
50
51
52
53
54
55
56
| 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
Rosanny Sihombing's avatar
Rosanny Sihombing committed
57
bytes(1024);
Rosanny Sihombing's avatar
Rosanny Sihombing committed
58
59
// output: '1KB'

Rosanny Sihombing's avatar
Rosanny Sihombing committed
60
bytes(1000);
Rosanny Sihombing's avatar
Rosanny Sihombing committed
61
62
// output: '1000B'

Rosanny Sihombing's avatar
Rosanny Sihombing committed
63
bytes(1000, {thousandsSeparator: ' '});
Rosanny Sihombing's avatar
Rosanny Sihombing committed
64
65
// output: '1 000B'

Rosanny Sihombing's avatar
Rosanny Sihombing committed
66
bytes(1024 * 1.7, {decimalPlaces: 0});
Rosanny Sihombing's avatar
Rosanny Sihombing committed
67
68
// output: '2KB'

Rosanny Sihombing's avatar
Rosanny Sihombing committed
69
bytes(1024, {unitSeparator: ' '});
Rosanny Sihombing's avatar
Rosanny Sihombing committed
70
// output: '1 KB'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
71

Rosanny Sihombing's avatar
Rosanny Sihombing committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
```

#### 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
Rosanny Sihombing's avatar
Rosanny Sihombing committed
105
bytes('1KB');
Rosanny Sihombing's avatar
Rosanny Sihombing committed
106
107
// output: 1024

Rosanny Sihombing's avatar
Rosanny Sihombing committed
108
bytes('1024');
Rosanny Sihombing's avatar
Rosanny Sihombing committed
109
110
// output: 1024

Rosanny Sihombing's avatar
Rosanny Sihombing committed
111
112
bytes(1024);
// output: 1KB
Rosanny Sihombing's avatar
Rosanny Sihombing committed
113
114
```

Rosanny Sihombing's avatar
Rosanny Sihombing committed
115
## License 
Rosanny Sihombing's avatar
Rosanny Sihombing committed
116
117
118
119
120
121
122

[MIT](LICENSE)

[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
Rosanny Sihombing's avatar
Rosanny Sihombing committed
123
[npm-image]: https://badgen.net/npm/node/bytes
Rosanny Sihombing's avatar
Rosanny Sihombing committed
124
[npm-url]: https://npmjs.org/package/bytes
Rosanny Sihombing's avatar
Rosanny Sihombing committed
125
126
[travis-image]: https://badgen.net/travis/visionmedia/bytes.js/master
[travis-url]: https://travis-ci.org/visionmedia/bytes.js