Convert bytes to a human readable string: 1337 → 1.34 kB
Installations
npm install pretty-bytes
Developer
sindresorhus
Developer Guide
Module System
ESM
Min. Node Version
^14.13.1 || >=16.0.0
Typescript Support
Yes
Node Version
16.20.0
NPM Version
9.2.0
Statistics
1,119 Stars
76 Commits
81 Forks
12 Watching
1 Branches
17 Contributors
Updated on 28 Nov 2024
Languages
JavaScript (95.24%)
TypeScript (4.76%)
Total Downloads
Cumulative downloads
Total Downloads
2,795,966,230
Last day
-6.9%
2,535,841
Compared to previous day
Last week
1.3%
14,595,027
Compared to previous week
Last month
7.4%
61,877,280
Compared to previous month
Last year
3%
687,918,535
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
pretty-bytes
Convert bytes to a human readable string:
1337
→1.34 kB
Useful for displaying file sizes for humans.
Note that it uses base-10 (e.g. kilobyte). Read about the difference between kilobyte and kibibyte.
Install
1npm install pretty-bytes
Usage
1import prettyBytes from 'pretty-bytes'; 2 3prettyBytes(1337); 4//=> '1.34 kB' 5 6prettyBytes(100); 7//=> '100 B' 8 9// Display with units of bits 10prettyBytes(1337, {bits: true}); 11//=> '1.34 kbit' 12 13// Display file size differences 14prettyBytes(42, {signed: true}); 15//=> '+42 B' 16 17// Localized output using German locale 18prettyBytes(1337, {locale: 'de'}); 19//=> '1,34 kB'
API
prettyBytes(number, options?)
number
Type: number
The number to format.
options
Type: object
signed
Type: boolean
Default: false
Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.
bits
Type: boolean
Default: false
Format the number as bits instead of bytes. This can be useful when, for example, referring to bit rate.
binary
Type: boolean
Default: false
Format the number using the Binary Prefix instead of the SI Prefix. This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes.
locale
Type: boolean | string
Default: false
(No localization)
Important: Only the number and decimal separator are localized. The unit title is not and will not be localized.
- If
true
: Localize the output using the system/browser locale. - If
string
: Expects a BCP 47 language tag (For example:en
,de
, …) - If
string[]
: Expects a list of BCP 47 language tags (For example:en
,de
, …)
minimumFractionDigits
Type: number
Default: undefined
The minimum number of fraction digits to display.
If neither minimumFractionDigits
or maximumFractionDigits
are set, the default behavior is to round to 3 significant digits.
1import prettyBytes from 'pretty-bytes'; 2 3// Show the number with at least 3 fractional digits 4prettyBytes(1900, {minimumFractionDigits: 3}); 5//=> '1.900 kB' 6 7prettyBytes(1900); 8//=> '1.9 kB'
maximumFractionDigits
Type: number
Default: undefined
The maximum number of fraction digits to display.
If neither minimumFractionDigits
or maximumFractionDigits
are set, the default behavior is to round to 3 significant digits.
1import prettyBytes from 'pretty-bytes'; 2 3// Show the number with at most 1 fractional digit 4prettyBytes(1920, {maximumFractionDigits: 1}); 5//=> '1.9 kB' 6 7prettyBytes(1920); 8//=> '1.92 kB'
space
Type: boolean
Default: true
Put a space between the number and unit.
1import prettyBytes from 'pretty-bytes'; 2 3prettyBytes(1920, {space: false}); 4//=> '1.9kB' 5 6prettyBytes(1920); 7//=> '1.92 kB'
Related
- pretty-bytes-cli - CLI for this module
- pretty-ms - Convert milliseconds to a human readable string
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
- Info: security policy file detected: .github/security.md:1
- Info: Found linked content: .github/security.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: .github/security.md:1
- Info: Found text in security policy: .github/security.md:1
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
Found 11/30 approved changesets -- score normalized to 3
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/pretty-bytes/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/pretty-bytes/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:21
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 11 are checked with a SAST tool
Score
4.3
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More