Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

8bits

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

8bits - npm Package Compare versions

Comparing version 0.2.5 to 0.2.6

lib/convert.js

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');

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc