Socket
Socket
Sign inDemoInstall

format-num

Package Overview
Dependencies
2
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.1.0

LICENSE

63

index.js
const parseNum = require('parse-num')
const memoize = require('lodash/memoize')

@@ -20,4 +21,9 @@ /* global Intl */

function formatNum (number, opts) {
opts = renameKeyShortcuts(Object.assign({}, defaultOptions, opts))
const getNumberFormatter = memoize(
(opts) => new Intl.NumberFormat([opts.locale], opts),
(opts) => JSON.stringify(opts)
)
const formatNum = (number, opts) => {
opts = renameKeyShortcuts(Object.assign(Object.create(null), defaultOptions, opts))
number = parseNum(number)

@@ -30,35 +36,40 @@

const nf = new Intl.NumberFormat([opts.locale], Object.assign({}, opts, { style: 'decimal' }))
const nf = getNumberFormatter(Object.assign(Object.create(null), opts, { style: 'decimal' }))
return nf.format(number)
}
function renameKeyShortcuts (opts) {
// expand 'min' to 'minimum', 'max' to 'maximum'
Object.keys(opts).forEach(function (key) {
if (!key.includes('minimum') && key.startsWith('min')) {
opts[key.replace('min', 'minimum')] = opts[key]
delete opts[key]
}
if (!key.includes('maximum') && key.startsWith('max')) {
opts[key.replace('max', 'maximum')] = opts[key]
delete opts[key]
}
const renameKeyShortcuts = (opts) => {
Object.keys(opts).forEach((key) => {
expandMin(opts, key)
expandMax(opts, key)
})
Object.keys(opts).forEach(function (key) {
if (key.startsWith('minimum') && !key.endsWith('Digits')) {
opts[key + 'Digits'] = opts[key]
delete opts[key]
}
Object.keys(opts).forEach((key) => addDigits(opts, key))
if (key.startsWith('maximum') && !key.endsWith('Digits')) {
opts[key + 'Digits'] = opts[key]
delete opts[key]
}
})
return opts
}
const expandMin = (opts, key) => expand(opts, key, 'min', 'minimum')
const expandMax = (opts, key) => expand(opts, key, 'max', 'maximum')
const expand = (opts, key, shorthand, full) => {
if (!key.includes(full) && key.startsWith(shorthand)) {
replaceKey(opts, key, key.replace(shorthand, full))
}
}
const addDigits = (opts, key) => {
if (
(key.startsWith('minimum') || key.startsWith('maximum')) &&
!key.endsWith('Digits')
) {
replaceKey(opts, key, key + 'Digits')
}
}
const replaceKey = (obj, oldKey, newKey) => {
obj[newKey] = obj[oldKey]
delete obj[oldKey]
}
module.exports = formatNum
{
"name": "format-num",
"version": "1.0.0",
"version": "1.1.0",
"description": "Format numbers.",

@@ -32,7 +32,9 @@ "main": "index.js",

"dependencies": {
"parse-num": "^1.0.0"
"parse-num": "^1.0.0",
"lodash": "^4.17.11"
},
"devDependendies": {
"standard": "5.x"
"devDependencies": {
"ava": "0.22.x",
"standard": "6.x"
}
}

@@ -14,3 +14,3 @@ format-num

npm install --save `format-num`
npm install --save format-num

@@ -67,2 +67,3 @@

of this library.
- [number-unit](https://github.com/ExodusMovement/number-unit): Numbers with units. Easily convert numbers to from different units.

@@ -69,0 +70,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc