bytes
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Bytes is a highly versatile Node.js utility used for parsing a string of bytes into actual bytes and vice-versa. It is an excellent tool when you need to convert values like '1TB' into their byte equivalent (1099511627776), and also performs the inverse operation. This user-friendly utility plays a vital role in managing and converting digital storage units.
You can make the most of Bytes by installing it through the npm registry, using the command npm install bytes
. Once installed, you need to require bytes in your JavaScript file like so:
var bytes = require('bytes');
You now have access to the various functions bytes offers like bytes.format
and bytes.parse
.
For example, if you want to convert a number value to a string representation, you use:
bytes(1024);
// output: '1KB'
Or if you want to parse a string value into bytes:
bytes('1KB');
// output: 1024
The bytes.format
function converts a numeric value in bytes into a string:
bytes.format(1024);
// output: '1KB'
You can further customize the output with different options for decimal places, thousands separators, and more.
bytes.format(1024, {unitSeparator: ' '});
// output: '1 KB'
The bytes.parse
function does the opposite, it converts a string representation of a value into bytes:
bytes.parse('1KB');
// output: 1024
Please note that Bytes utility uses powers of two for conversion, staying consistent with digital storage conventions.
The comprehensive documentation for Bytes can be found directly on its npm registry page (https://www.npmjs.com/package/bytes). It provides a detailed overview of the installation process, functions, parameters, return types, and real-life examples. Should you need a deeper understanding of different functions or versions, you can browse the GitHub documentation linked from the npm page (https://github.com/visionmedia/bytes.js.git).