Version 1
[yaffs-website] / node_modules / bytes / Readme.md
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
7 Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa.
8
9 ## Usage
10
11 ```js
12 var bytes = require('bytes');
13 ```
14
15 #### bytes.format(number value, [options]): string|null
16
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
18  rounded.
19
20 **Arguments**
21
22 | Name    | Type   | Description        |
23 |---------|--------|--------------------|
24 | value   | `number` | Value in bytes     |
25 | options | `Object` | Conversion options |
26
27 **Options**
28
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
35 **Returns**
36
37 | Name    | Type        | Description             |
38 |---------|-------------|-------------------------|
39 | results | `string`|`null` | Return null upon error. String value otherwise. |
40
41 **Example**
42
43 ```js
44 bytes(1024);
45 // output: '1kB'
46
47 bytes(1000);
48 // output: '1000B'
49
50 bytes(1000, {thousandsSeparator: ' '});
51 // output: '1 000B'
52
53 bytes(1024 * 1.7, {decimalPlaces: 0});
54 // output: '2kB'
55 ```
56
57 #### bytes.parse(string value): number|null
58
59 Parse the string value into an integer in bytes. If no unit is given, it is assumed the value is in bytes.
60
61 **Arguments**
62
63 | Name          | Type   | Description        |
64 |---------------|--------|--------------------|
65 | value   | `string` | String to parse.   |
66
67 **Returns**
68
69 | Name    | Type        | Description             |
70 |---------|-------------|-------------------------|
71 | results | `number`|`null` | Return null upon error. Value in bytes otherwise. |
72
73 **Example**
74
75 ```js
76 bytes('1kB');
77 // output: 1024
78
79 bytes('1024');
80 // output: 1024
81 ```
82
83 ## Installation
84
85 ```bash
86 npm install bytes --save
87 component install visionmedia/bytes.js
88 ```
89
90 ## License 
91
92 [![npm](https://img.shields.io/npm/l/express.svg)](https://github.com/visionmedia/bytes.js/blob/master/LICENSE)
93
94 [downloads-image]: https://img.shields.io/npm/dm/bytes.svg
95 [downloads-url]: https://npmjs.org/package/bytes
96 [npm-image]: https://img.shields.io/npm/v/bytes.svg
97 [npm-url]: https://npmjs.org/package/bytes
98 [travis-image]: https://img.shields.io/travis/visionmedia/bytes.js/master.svg
99 [travis-url]: https://travis-ci.org/visionmedia/bytes.js