Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
comma-number
Advanced tools
The comma-number npm package is a simple utility for formatting numbers with commas as thousands separators. It is useful for improving the readability of large numbers in applications.
Basic Number Formatting
This feature allows you to format a number by adding commas as thousands separators. The code sample demonstrates how to use the comma-number package to format the number 1234567 into '1,234,567'.
const commaNumber = require('comma-number');
const formattedNumber = commaNumber(1234567);
console.log(formattedNumber); // Output: '1,234,567'
Custom Separator
Comma-number also allows you to specify a custom separator instead of the default comma. In this example, a period is used as the separator, resulting in '1.234.567'.
const commaNumber = require('comma-number');
const formattedNumber = commaNumber(1234567, '.');
console.log(formattedNumber); // Output: '1.234.567'
Numeral.js is a library for formatting and manipulating numbers. It offers more extensive functionality compared to comma-number, including currency formatting, percentage formatting, and more. While comma-number focuses on simple comma separation, numeral.js provides a broader range of number formatting options.
The accounting.js library is designed for number, money, and currency formatting. It provides similar functionality to comma-number for adding thousands separators but also includes features for currency formatting and number rounding. It is more comprehensive than comma-number for financial applications.
Format-number is another library for formatting numbers with custom separators and decimal places. It is similar to comma-number in its basic functionality but offers additional options for customizing the format, such as specifying the number of decimal places.
$ npm install --save comma-number
const commaNumber = require('comma-number')
commaNumber(1000) // "1,000"
commaNumber(-1000) // "-1,000"
commaNumber(-1000, '.') // "-1.000"
commaNumber(1000.12) // "1,000.12"
commaNumber(-1000.12) // "-1,000.12"
commaNumber('-1000,12', '.', ',') // "-1.000,12"
// make a new function using custom separator and decimal char:
const format = commaNumber.bindWith('_', '!')
// use it as you would commaNumber().
format(1000) // "1_000"
format(-1000) // "-1_000"
format(1000.12) // "1_000!12"
format(-1000.12) // "-1_000!12"
Revised implementation changes the API a bit:
string
and number
is returned as is, not as '0'
.bindWith
function to use a currying style to bind options for a reusable format function.Other changes:
Parameters:
Returns:
The commaNumber
function accepts these same parameters as the second and third params. This prevents using currying to bind them and reuse that bound function.
The bindWith
function accepts the options and returns a function bound with them.
// the default commaNumber uses a comma separator and period for decimal char.
var commaNumber = require('comma-number')
// can build a custom version using bindWith.
, format = commaNumber.bindWith('_', '!')
, result1 = commaNumber(1234567.89)
, result2 = format('1234567.89')
console.log(result1) // outputs: 1,234,567.89
console.log(result2) // outputs: 1_234_567!89
# run tests via tap
$ npm test
# benchmark current implementation versus previous
npm run benchmark
# get coverage info by default with testing:
npm test
The rewrite has a considerable performance increase from the previous version.
I converted the benchmark output from my machine into a table.
It compares the performance of version 1.1.0 with 2.0.0. The inputs with decimals can only be processed by the new version so those show as "invalid" for the previous version.
FAQs
Format a number with commas or custom character
The npm package comma-number receives a total of 152,022 weekly downloads. As such, comma-number popularity was classified as popular.
We found that comma-number 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.