
Security News
TC39 Advances 11 Proposals for Math Precision, Binary APIs, and More
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
The prettysize npm package is a utility for converting file sizes into human-readable strings. It simplifies the process of displaying file sizes in a more understandable format, such as converting bytes into kilobytes, megabytes, etc.
Basic Usage
This feature allows you to convert a file size in bytes to a human-readable string. In this example, 123456789 bytes is converted to '117.7 MB'.
const prettysize = require('prettysize');
console.log(prettysize(123456789)); // '117.7 MB'
Customizing Units
This feature allows you to customize the units used in the conversion. By passing `true` as the second argument, the output uses binary units (MiB) instead of decimal units (MB).
const prettysize = require('prettysize');
console.log(prettysize(123456789, true)); // '117.7 MiB'
Specifying Number of Decimal Places
This feature allows you to specify the number of decimal places in the output. In this example, the output is formatted to two decimal places.
const prettysize = require('prettysize');
console.log(prettysize(123456789, {places: 2})); // '117.74 MB'
The filesize package is another utility for converting file sizes to human-readable strings. It offers more customization options compared to prettysize, such as different rounding methods, output formats (e.g., string, array, object), and support for various locales.
The pretty-bytes package is a lightweight utility for converting bytes to a human-readable string. It is similar to prettysize but focuses on simplicity and ease of use. It supports both metric and binary units and allows for customization of the number of decimal places.
Helper utility to provide pretty printed file sizes (best used for logging or CLI output)
npm install prettysize
const pretty = require('prettysize');
let str = pretty(1024);
//str = "1 kB"
str = pretty(1024 * 1024);
//str = "1 MB"
str = pretty(123456789);
//str = "117.7 MB"
It supports the following sizes:
pretty(123456, true, true, 2);
/*
First arg is size
Second argument is to remove the space from the output
Third argument is to use a single character for the size.
Forth argument is the number of decimal places to return, default is 1.
Fith argument is to return a converted number without the size string.
*/
let str = pretty(1024 * 1024, true);
//str = "1MB"
str = pretty(123456789, {nospace: true}) // pretty(123456789, true, true);
//str = "117.7M"
str = pretty(123456789, {one: true}) // pretty(123456789, false, true);
//str = "117.7 M"
str = pretty(123456789, {one: true, places: 2}) // pretty(123456789, false, true, 2);
//str = "117.74 M"
str = pretty(123456789, {one: true, places: 3}) // pretty(123456789, false, true, 3);
//str = "117.738 M"
str = pretty(123456789, {numOnly: true}) // pretty(123456789, false, false, false, true);
//num = 117.7
FAQs
Convert bytes to other sizes for prettier logging
The npm package prettysize receives a total of 156,111 weekly downloads. As such, prettysize popularity was classified as popular.
We found that prettysize demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.