format-num
Advanced tools
Comparing version 1.0.0 to 1.1.0
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 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
5983
59
73
2
2
1
+ Addedlodash@^4.17.11
+ Addedlodash@4.17.21(transitive)