Comparing version 0.1.0 to 0.2.0
39
index.js
@@ -5,6 +5,19 @@ 'use strict'; | ||
var getUnitsObj = function(options){ | ||
var base = { | ||
// Prefixes for multiples of bits (b) or bytes (B) | ||
// Decimal Binary | ||
// ================================================ | ||
// Value Metric | Value JEDEC IEC | ||
// 1000 k kilo | 1024 K kilo Ki kibi | ||
// 1000^2 M mega | 1024^2 M mega Mi mebi | ||
// 1000^3 G giga | 1024^3 G giga Gi gibi | ||
// 1000^4 T tera | 1024^4 – – Ti tebi | ||
// 1000^5 P peta | 1024^5 – – Pi pebi | ||
// 1000^6 E exa | 1024^6 – – Ei exbi | ||
// 1000^7 Z zetta | 1024^7 – – Zi zebi | ||
// 1000^8 Y yotta | 1024^8 – – Yi yobi | ||
var getUnitsObj = function(units, decimal){ | ||
var decimalUnits = { | ||
B: 'B', | ||
KB: 'KB', | ||
KB: 'kB', | ||
MB: 'MB', | ||
@@ -19,7 +32,15 @@ GB: 'GB', | ||
return _.extend(base, options.units); | ||
if (!decimal) { | ||
decimalUnits['KB'] = 'KB'; | ||
} | ||
return _.extend(decimalUnits, units); | ||
}; | ||
var convert = function(value, units, inputUnit, outputUnit, k){ | ||
inputUnit = inputUnit.toUpperCase(); | ||
var i = _.indexOf(units, inputUnit); | ||
if (i < 0){ | ||
i = 0; | ||
} | ||
value = value * Math.pow(k, i); | ||
@@ -32,6 +53,7 @@ i = _.indexOf(units, outputUnit); | ||
options = options || {}; | ||
var units = getUnitsObj(options); | ||
var format = options.format || '%s %s'; | ||
var decimal = !options.binary; | ||
var k = decimal ? 1000 : 1024; | ||
var units = getUnitsObj(options.units, decimal); | ||
var unitKeys = _.keys(units); | ||
var format = options.format || '%s %s'; | ||
var k = options.useBits ? 1000 : 1024; | ||
var digits = options.digits || 0; | ||
@@ -45,3 +67,3 @@ var inputUnit = options.from || 'B'; | ||
if (typeof bytes === 'string'){ | ||
bytes = parseInt(bytes); | ||
bytes = parseInt(bytes, 10); | ||
} | ||
@@ -54,2 +76,3 @@ | ||
// convert from bytes to either the desired output unit or the highest unit prefix | ||
if (outputUnit){ | ||
@@ -56,0 +79,0 @@ value = convert(bytes, unitKeys, 'B', outputUnit, k); |
{ | ||
"name": "8bits", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A Javascript library for manipulating and converting byte values", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,2 +5,4 @@ # 8bits [![Build Status](https://travis-ci.org/akenn/8bits.svg?branch=master)](https://travis-ci.org/akenn/8bits) | ||
There are two primary ways to represent byte sizes: SI units (decimal / base-10 / 10^3) and IEC units (binary; / base 2 / 2^10). `8bits` supports both of formats, with the default being decimal. You can read more on this subject [here](http://en.wikipedia.org/wiki/Binary_prefix) and [here](https://pacoup.com/2009/05/26/kb-kb-kib-whats-up-with-that/). | ||
## Install | ||
@@ -17,15 +19,21 @@ | ||
byte(1023); | ||
//=> 1023 B | ||
byte(789); | ||
//=> 789 B | ||
byte(1025); | ||
//=> 1 KB | ||
byte(1000); | ||
//=> 1 kB | ||
// convert from one format to another | ||
byte(512000, {from: 'KB', to: 'MB'}); | ||
// Setting the number of significant digits | ||
byte(1024, {digits: 2}); | ||
//=> 1.02 kB | ||
// Using binary instead of decimal | ||
byte(1024, {digits: 2, binary: true}); | ||
//=> 1.00 KB | ||
// Converting from one prefix to another | ||
byte(500000, {from: 'kB', to: 'MB'}); | ||
//=> 500 MB | ||
// use bits instead of bytes | ||
byte(1024, {from: 'B', to: 'KB', useBits: true, digits: 2}); | ||
//=> 1.02 KB | ||
byte(512000, {from: 'kB', to: 'MB', binary: true}); | ||
//=> 500 MB | ||
``` | ||
@@ -35,6 +43,4 @@ | ||
- Add better examples | ||
- Support pluralization | ||
- Retrieve byte value from human-readable value (i.e. 1 MB => 1048576) | ||
- More tests! | ||
- Improve Readme | ||
@@ -41,0 +47,0 @@ ## License |
50
test.js
@@ -7,9 +7,9 @@ 'use strict'; | ||
expect(byte(0)).to.equal('0 B'); | ||
expect(byte(1023)).to.equal('1023 B'); | ||
expect(byte(1024)).to.equal('1 KB'); | ||
expect(byte(2048)).to.equal('2 KB'); | ||
expect(byte(1000)).to.equal('1 kB'); | ||
expect(byte(1024)).to.equal('1 kB'); | ||
expect(byte(2048)).to.equal('2 kB'); | ||
expect(byte(1048576)).to.equal('1 MB'); | ||
}); | ||
it('should allow custom units', function () { | ||
it('should allow overriding units', function () { | ||
var options = { | ||
@@ -21,38 +21,36 @@ units: { | ||
}; | ||
expect(byte(1023, options)).to.equal('1023 Bytes'); | ||
expect(byte(1024, options)).to.equal('1 KB'); | ||
expect(byte(100, options)).to.equal('100 Bytes'); | ||
expect(byte(1024, options)).to.equal('1 kB'); | ||
expect(byte(1048576, options)).to.equal('1 Megabytes'); | ||
}); | ||
it('should support custom formats', function () { | ||
it('should support custom formatting', function () { | ||
expect(byte(0, { | ||
format: '%s - %s' | ||
})).to.equal('0 - B'); | ||
expect(byte(1024, { | ||
format: '%s%s' | ||
})).to.equal('1KB'); | ||
}); | ||
})).to.equal('1kB'); | ||
it('should support base-10', function () { | ||
expect(byte(999, {useBits: true})).to.equal('999 B'); | ||
expect(byte(1023, {useBits: true})).to.equal('1 KB'); | ||
expect(byte(1024, {from: 'B', to: 'KB', useBits: true, digits: 2})).to.equal('1.02 KB'); | ||
expect(byte(1023, {useBits: true, digits: 2})).to.equal('1.02 KB'); | ||
expect(byte(1024, { | ||
format: '%s%s', | ||
digits: 2 | ||
})).to.equal('1.02kB'); | ||
}); | ||
it('should support specifying number of useBits places', function () { | ||
var options = { | ||
useBits: true, | ||
digits: 5 | ||
}; | ||
expect(byte(999, options)).to.equal('999.00000 B'); | ||
expect(byte(1048576, options)).to.equal('1.04858 MB'); | ||
it('should support binary format', function () { | ||
expect(byte(999, {binary: true})).to.equal('999 B'); | ||
expect(byte(1023, {binary: true})).to.equal('1023 B'); | ||
expect(byte(1024, {from: 'B', to: 'KB', binary: true, digits: 2})).to.equal('1.00 KB'); | ||
expect(byte(1023, {binary: true, digits: 2})).to.equal('1023.00 B'); | ||
}); | ||
it('should convert from one unit to another', function () { | ||
expect(byte(100, {from: 'KB', to: 'B'})).to.equal('102400 B'); | ||
expect(byte(100, {from: 'KB', to: 'B', useBits: true})).to.equal('100000 B'); | ||
expect(byte(524288000, {from: 'B', to: 'MB'})).to.equal('500 MB'); | ||
expect(byte(512000, {from: 'KB', to: 'MB'})).to.equal('500 MB'); | ||
expect(byte(500, {from: 'MB', to: 'B'})).to.equal('524288000 B'); | ||
expect(byte(100, {from: 'kB', to: 'B'})).to.equal('100000 B'); | ||
expect(byte(100, {from: 'kB', to: 'B', binary: true})).to.equal('102400 B'); | ||
expect(byte(100, {from: 'kB', to: 'B', useBits: true})).to.equal('100000 B'); | ||
expect(byte(524288000, {from: 'B', to: 'MB'})).to.equal('524 MB'); | ||
expect(byte(512000, {from: 'kB', to: 'MB'})).to.equal('512 MB'); | ||
expect(byte(500, {from: 'MB', to: 'B'})).to.equal('500000000 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
6067
124
47