Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
human-format
Advanced tools
The human-format npm package is designed to help format numbers into human-readable strings. It is particularly useful for converting large numbers into more digestible formats, such as converting bytes into kilobytes, megabytes, etc., or formatting large numbers with appropriate suffixes.
Formatting Numbers with Suffixes
This feature allows you to format large numbers with appropriate suffixes like 'K' for thousands, 'M' for millions, etc. The code sample demonstrates how to format the number 1234567 into '1.23M'.
const humanFormat = require('human-format');
const formattedNumber = humanFormat(1234567);
console.log(formattedNumber); // Output: '1.23M'
Formatting Bytes
This feature is specifically for formatting byte values into human-readable strings with appropriate units like KB, MB, GB, etc. The code sample shows how to format 1234567890 bytes into '1.23GB'.
const humanFormat = require('human-format');
const formattedBytes = humanFormat.bytes(1234567890);
console.log(formattedBytes); // Output: '1.23GB'
Custom Units
This feature allows you to define custom units and scales for formatting. The code sample demonstrates how to create a custom unit 'banana' and format the number 1234567 into '1.23Mbanana'.
const humanFormat = require('human-format');
const customUnits = new humanFormat.Scale({
'unit': 'banana',
'base': 1000,
'prefixes': {
'': 1,
'k': 1000,
'M': 1000000
}
});
const formattedCustom = humanFormat(1234567, { scale: customUnits });
console.log(formattedCustom); // Output: '1.23Mbanana'
The numeral package is a library for formatting and manipulating numbers. It provides a wide range of formatting options, including currency, percentages, and more. Compared to human-format, numeral offers more comprehensive number formatting capabilities but may be more complex to use for simple human-readable formatting.
The filesize package is specifically designed for formatting file sizes into human-readable strings. It supports various units like bytes, kilobytes, megabytes, etc. While it is similar to the bytes formatting feature of human-format, filesize is more specialized and offers more options for file size formatting.
The pretty-bytes package is a simple utility for converting bytes into a human-readable string. It is lightweight and easy to use, making it a good alternative for those who only need to format byte values. Compared to human-format, pretty-bytes is more focused and has fewer features but is very straightforward for its specific use case.
Converts a number to/from a human readable string:
1337
↔1.34kB
Installation of the npm package:
> npm install --save human-format
Then require the package:
var humanFormat = require('human-format');
You can directly use the build provided at unpkg.com:
<script src="https://unpkg.com/human-format@0.9/dist/human-format.js"></script>
humanFormat(1337)
//=> '1.34 k'
// The number of decimals can be changed.
humanFormat(1337, {
decimals: 1
})
//=> '1.3 k'
// Units and scales can be specified.
humanFormat(65536, {
scale: 'binary',
unit: 'B'
})
//=> 64 kiB
// A custom separator can be specified.
humanFormat(1337, {
separator: ' - '
})
//=> 1.34 - k
// Custom scales can be created!
var timeScale = new humanFormat.Scale({
seconds: 1,
minutes: 60,
hours: 3600,
days: 86400,
months: 2592000,
})
humanFormat(26729235, { scale: timeScale })
//=> 10.31 months
// You can force a prefix to be used.
humanFormat(100, { unit: 'm', prefix: 'k' })
//=> 0.1 km
// You can access the raw result.
humanFormat.raw(100, { prefix: 'k' })
//=> {
// prefix: 'k',
// value: 0.09999999999999999 // Close value, not rounded.
// }
humanFormat.parse('1.34 kiB', { scale: 'binary' })
//=> 1372.16
// Fallbacks when possible if the prefix is incorrectly cased.
humanFormat.parse('1 g')
// => 1000000000
// You can access the raw result.
humanFormat.parse.raw('1.34 kB')
//=> {
// factor: 1000,
// prefix: 'k',
// unit: 'B',
// value: 1.34
//}
Contributions are very welcomed, either on the documentation or on the code.
You may:
Contributors:
ISC © Julien Fontanet
FAQs
Converts a number to/from a human readable string: `1337` ↔ `1.34kB`
We found that human-format demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.