Comparing version 0.2.5 to 0.2.6
59
index.js
@@ -5,2 +5,3 @@ 'use strict'; | ||
var sprintf = require('underscore.string/sprintf'); | ||
var convert = require('./lib/convert'); | ||
@@ -26,40 +27,2 @@ function parseOptions(options) { | ||
var convert = function(value, units, inputUnit, outputUnit, decimal){ | ||
var unitKeys = _.keys(units); | ||
var converted; | ||
var k = decimal ? 1000 : 1024; | ||
var i; | ||
if (inputUnit) { | ||
inputUnit = inputUnit.toUpperCase(); | ||
} else { | ||
inputUnit = 'B'; | ||
} | ||
i = _.indexOf(unitKeys, inputUnit); | ||
if (i < 0){ | ||
i = 0; | ||
} | ||
value = value * Math.pow(k, i); | ||
if (outputUnit) { | ||
outputUnit = outputUnit.toUpperCase(); | ||
i = _.indexOf(unitKeys, outputUnit); | ||
} else { | ||
value = (value / Math.pow(k, i)); | ||
i = Math.floor(Math.log(value) / Math.log(k)); | ||
} | ||
converted = (value / Math.pow(k, i)); | ||
return { | ||
value: converted, | ||
unit: i < unitKeys.length ? units[unitKeys[i]] : units[_.last(unitKeys)] | ||
}; | ||
}; | ||
module.exports = function(bytes, options){ | ||
@@ -78,10 +41,5 @@ var config = parseOptions(options); | ||
if (bytes < 1){ | ||
return sprintf(config.outputFormat, (bytes).toString(), 'B'); | ||
} | ||
// convert to bytes | ||
if (config.convert.from !== 'B'){ | ||
var tmp = convert(bytes, config.units, config.convert.from, 'B', config.decimal); | ||
bytes = tmp.value; | ||
bytes = convert(bytes, config.units, config.convert.from, 'B', config.decimal).value; | ||
} | ||
@@ -91,12 +49,13 @@ | ||
value = 0; | ||
unit = units.B; | ||
} else { | ||
var tmp = convert(bytes, config.units, 'B', config.convert.to, config.decimal); | ||
value = tmp.value; | ||
unit = tmp.unit; | ||
unit = config.units.B; | ||
return sprintf(config.outputFormat, value, unit); | ||
} | ||
var converted = convert(bytes, config.units, 'B', config.convert.to, config.decimal); | ||
value = converted.value; | ||
unit = converted.unit; | ||
if (config.minSigFigs && value.toString().replace('.', '').length < config.minSigFigs){ | ||
value = value.toPrecision(config.minSigFigs); | ||
} else { | ||
} else if (value > 1 || unit !== 'B') { | ||
value = value.toFixed(config.digits); | ||
@@ -103,0 +62,0 @@ } |
{ | ||
"name": "8bits", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "A Javascript library for manipulating and converting byte values", | ||
@@ -26,3 +26,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"binary-prefix": "^1.0.0", | ||
"binary-prefix": "^1.1.0", | ||
"lodash": "^3.6.0", | ||
@@ -29,0 +29,0 @@ "underscore.string": "^3.0.3" |
@@ -76,2 +76,4 @@ 'use strict'; | ||
expect(byte('')).to.equal('0 B'); | ||
expect(byte(false)).to.equal('0 B'); | ||
expect(byte(0)).to.equal('0 B'); | ||
}); | ||
@@ -82,2 +84,3 @@ | ||
expect(byte(0.99)).to.equal('0.99 B'); | ||
expect(byte(0.00001)).to.equal('0.00001 B'); | ||
expect(byte(1)).to.equal('1 B'); | ||
@@ -84,0 +87,0 @@ expect(byte(0)).to.equal('0 B'); |
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
8642
7
172
Updatedbinary-prefix@^1.1.0