human-format
Advanced tools
Comparing version 0.8.0 to 0.9.0
130
index.js
@@ -21,16 +21,12 @@ // UMD: https://github.com/umdjs/umd/blob/master/returnExports.js | ||
function assignBase (dst, src) { | ||
var prop | ||
for (prop in src) { | ||
if (has(src, prop)) { | ||
dst[prop] = src[prop] | ||
} | ||
} | ||
} | ||
function assign (dst, src) { | ||
var i, n | ||
for (i = 0, n = arguments.length; i < n; ++i) { | ||
var i, n, prop | ||
for (i = 1, n = arguments.length; i < n; ++i) { | ||
src = arguments[i] | ||
if (src) { | ||
assignBase(dst, src) | ||
if (src != null) { | ||
for (prop in src) { | ||
if (has(src, prop)) { | ||
dst[prop] = src[prop] | ||
} | ||
} | ||
} | ||
@@ -72,31 +68,8 @@ } | ||
return function has (obj, prop) { | ||
return obj && hasOwnProperty.call(obj, prop) | ||
return obj != null && hasOwnProperty.call(obj, prop) | ||
} | ||
})(Object.prototype.hasOwnProperty) | ||
var toString = (function (toString_) { | ||
return function toString (val) { | ||
return toString_.call(val) | ||
} | ||
})(Object.prototype.toString) | ||
function isDefined (val) { | ||
/* jshint eqnull:true */ | ||
return val != null | ||
} | ||
var isNumber = (function (tag) { | ||
return function isNumber (value) { | ||
return (value === value) && (toString(value) === tag) // eslint-disable-line no-self-compare | ||
} | ||
})(toString(0)) | ||
var isString = (function (tag) { | ||
return function isString (value) { | ||
return (toString(value) === tag) | ||
} | ||
})(toString('')) | ||
function resolve (container, entry) { | ||
while (isString(entry)) { | ||
while (typeof entry === 'string') { | ||
entry = container[entry] | ||
@@ -107,11 +80,2 @@ } | ||
function round (f, n) { | ||
if (!n) { | ||
return Math.round(f) | ||
} | ||
var p = Math.pow(10, n) | ||
return Math.round(f * p) / p | ||
} | ||
// ================================================================= | ||
@@ -156,4 +120,7 @@ | ||
var prefixes = {} | ||
if (initExp === undefined) { | ||
initExp = 0 | ||
} | ||
forEach(prefixesList, function (prefix, i) { | ||
prefixes[prefix] = Math.pow(base, i + (initExp || 0)) | ||
prefixes[prefix] = Math.pow(base, i + initExp) | ||
}) | ||
@@ -166,4 +133,2 @@ | ||
Scale.prototype.findPrefix = function Scale$findPrefix (value) { | ||
/* jshint bitwise: false */ | ||
var list = this._list | ||
@@ -191,4 +156,4 @@ var low = 0 | ||
if (!matches) { | ||
return null | ||
if (matches === null) { | ||
return | ||
} | ||
@@ -208,3 +173,3 @@ | ||
} else { | ||
return null | ||
return | ||
} | ||
@@ -241,10 +206,2 @@ | ||
var defaults = { | ||
scale: 'SI', | ||
// Strict mode prevents parsing of incorrectly cased prefixes. | ||
strict: false, | ||
// Unit to use for formatting. | ||
unit: '', | ||
// Decimal digits for formatting. | ||
@@ -254,5 +211,14 @@ decimals: 2, | ||
// separator to use between value and units | ||
separator: ' ' | ||
separator: ' ', | ||
// Unit to use for formatting. | ||
unit: '' | ||
} | ||
var rawDefaults = { | ||
scale: 'SI', | ||
// Strict mode prevents parsing of incorrectly cased prefixes. | ||
strict: false | ||
} | ||
function humanFormat (value, opts) { | ||
@@ -262,4 +228,5 @@ opts = assign({}, defaults, opts) | ||
var info = humanFormat$raw(value, opts) | ||
value = String(info.value) | ||
var suffix = info.prefix + opts.unit | ||
return round(info.value, opts.decimals) + (suffix ? opts.separator + suffix : '') | ||
return suffix === '' ? value : value + opts.separator + suffix | ||
} | ||
@@ -274,3 +241,3 @@ | ||
function humanFormat$parse$raw (str, opts) { | ||
if (!isString(str)) { | ||
if (typeof str !== 'string') { | ||
throw new TypeError('str must be a string') | ||
@@ -280,7 +247,7 @@ } | ||
// Merge default options. | ||
opts = assign({}, defaults, opts) | ||
opts = assign({}, rawDefaults, opts) | ||
// Get current scale. | ||
var scale = resolve(scales, opts.scale) | ||
if (!scale) { | ||
if (scale === undefined) { | ||
throw new Error('missing scale') | ||
@@ -296,3 +263,3 @@ } | ||
var info = scale.parse(str, opts.strict) | ||
if (!info) { | ||
if (info === undefined) { | ||
throw new Error('cannot parse str') | ||
@@ -313,3 +280,3 @@ } | ||
if (!isNumber(value)) { | ||
if (typeof value !== 'number' || Number.isNaN(value)) { | ||
throw new TypeError('value must be a number') | ||
@@ -319,13 +286,14 @@ } | ||
// Merge default options. | ||
opts = assign({}, defaults, opts) | ||
opts = assign({}, rawDefaults, opts) | ||
// Get current scale. | ||
var scale = resolve(scales, opts.scale) | ||
if (!scale) { | ||
if (scale === undefined) { | ||
throw new Error('missing scale') | ||
} | ||
var decimals = opts.decimals | ||
var prefix = opts.prefix | ||
var factor | ||
if (isDefined(prefix)) { | ||
if (prefix !== undefined) { | ||
if (!has(scale._prefixes, prefix)) { | ||
@@ -336,11 +304,25 @@ throw new Error('invalid prefix') | ||
factor = scale._prefixes[prefix] | ||
value /= factor | ||
if (decimals !== undefined) { | ||
var p = Math.pow(10, decimals) | ||
value = Math.round(value * p) / p | ||
} | ||
} else { | ||
var _ref = scale.findPrefix(value) | ||
factor = _ref.factor | ||
if (decimals !== undefined) { | ||
do { | ||
factor = _ref.factor | ||
var r = Math.pow(10, decimals) / factor | ||
value = Math.round(value * r) / r | ||
} while ((_ref = scale.findPrefix(value)).factor !== factor) | ||
} else { | ||
factor = _ref.factor | ||
} | ||
value /= factor | ||
prefix = _ref.prefix | ||
factor = _ref.factor | ||
} | ||
// Rebase using current factor. | ||
value /= factor | ||
return { | ||
@@ -347,0 +329,0 @@ prefix: prefix, |
{ | ||
"name": "human-format", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"license": "ISC", | ||
"description": "Converts a number to/from a human readable string: `1337` ↔ `1.34kB`", | ||
@@ -18,9 +19,4 @@ "keywords": [ | ||
], | ||
"scripts": { | ||
"commitmsg": "yarn test", | ||
"dev-test": "jest --watch", | ||
"prepublish": "mkdir -p dist && browserify -s humanFormat index.js | uglifyjs -c > dist/human-format.js", | ||
"pretest": "standard", | ||
"test": "jest" | ||
}, | ||
"homepage": "https://github.com/JsCommunity/human-format", | ||
"bugs": "https://github.com/JsCommunity/human-format/issues", | ||
"repository": { | ||
@@ -30,16 +26,31 @@ "type": "git", | ||
}, | ||
"bugs": "https://github.com/JsCommunity/human-format/issues", | ||
"author": "Julien Fontanet <julien.fontanet@isonoe.net> (http://julien.isonoe.net/)", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"browserify": "^14.1.0", | ||
"husky": "^0.13.1", | ||
"jest": "^19.0.2", | ||
"standard": "^9.0.0", | ||
"uglify-js": "^2.8.3" | ||
"author": { | ||
"name": "Julien Fontanet", | ||
"email": "julien.fontanet@isonoe.net" | ||
}, | ||
"preferGlobal": false, | ||
"files": [ | ||
"dist/", | ||
"index.js" | ||
] | ||
], | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^14.5.0", | ||
"husky": "^0.14.3", | ||
"jest": "^21.2.1", | ||
"standard": "^10.0.3", | ||
"uglify-js": "^3.1.8" | ||
}, | ||
"scripts": { | ||
"commitmsg": "yarn test", | ||
"dev-test": "jest --watch", | ||
"prepublishOnly": "mkdir -p dist && browserify -s humanFormat index.js | uglifyjs -c > dist/human-format.js", | ||
"pretest": "standard --fix", | ||
"test": "jest" | ||
}, | ||
"jest": { | ||
"collectCoverage": true, | ||
"testEnvironment": "node" | ||
} | ||
} |
@@ -31,3 +31,3 @@ # human-format | ||
```html | ||
<script src="https://unpkg.com/human-format@0.8/dist/human-format.js"></script> | ||
<script src="https://unpkg.com/human-format@0.9/dist/human-format.js"></script> | ||
``` | ||
@@ -34,0 +34,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
1
0
12384
4
269