Comparing version 0.0.7 to 0.0.8
@@ -388,22 +388,37 @@ | ||
*/ | ||
humanize.filesize = function(filesize, kilo, decimals, decPoint, thousandsSep) { | ||
humanize.filesize = function(filesize, kilo, decimals, decPoint, thousandsSep, suffixSep) { | ||
kilo = (kilo === undefined) ? 1024 : kilo; | ||
decimals = isNaN(decimals) ? 2 : Math.abs(decimals); | ||
decPoint = (decPoint === undefined) ? '.' : decPoint; | ||
thousandsSep = (thousandsSep === undefined) ? ',' : thousandsSep; | ||
if (filesize <= 0) { return '0 bytes'; } | ||
if (filesize < kilo && decimals === undefined) { decimals = 0; } | ||
if (suffixSep === undefined) { suffixSep = ' '; } | ||
return humanize.intword(filesize, ['bytes', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb'], kilo, decimals, decPoint, thousandsSep, suffixSep); | ||
}; | ||
var thresholds = [1]; | ||
var units = ['bytes', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb']; | ||
if (filesize < kilo) { return humanize.numberFormat(filesize, 0) + ' ' + units[0]; } | ||
/** | ||
* Formats the value like a 'human-readable' number (i.e. '13 K', '4.1 M', '102', etc). | ||
* | ||
* For example: | ||
* If value is 123456789, the output would be 117.7 M. | ||
*/ | ||
humanize.intword = function(number, units, kilo, decimals, decPoint, thousandsSep, suffixSep) { | ||
var humanized, unit; | ||
for (var i = 1; i < units.length; i++) { | ||
thresholds[i] = thresholds[i-1] * kilo; | ||
if (filesize < thresholds[i]) { | ||
return humanize.numberFormat(filesize / thresholds[i-1], decimals, decPoint, thousandsSep) + ' ' + units[i-1]; | ||
units = units || ['', 'K', 'M', 'B', 'T'], | ||
unit = units.length - 1, | ||
kilo = kilo || 1000, | ||
decimals = isNaN(decimals) ? 2 : Math.abs(decimals), | ||
decPoint = decPoint || '.', | ||
thousandsSep = thousandsSep || ',', | ||
suffixSep = suffixSep || ''; | ||
for (var i=0; i < units.length; i++) { | ||
if (number < Math.pow(kilo, i+1)) { | ||
unit = i; | ||
break; | ||
} | ||
} | ||
humanized = number / Math.pow(kilo, unit); | ||
// use the last unit if we drop out to here | ||
return humanize.numberFormat(filesize / thresholds[units.length - 1], decimals, decPoint, thousandsSep) + ' ' + units[units.length - 1]; | ||
var suffix = units[unit] ? suffixSep + units[unit] : ''; | ||
return humanize.numberFormat(humanized, decimals, decPoint, thousandsSep) + suffix; | ||
}; | ||
@@ -410,0 +425,0 @@ |
@@ -19,3 +19,3 @@ { | ||
"main": "humanize.js", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"dependencies": {}, | ||
@@ -22,0 +22,0 @@ "devDependencies": { |
@@ -332,2 +332,27 @@ var should = require('should'); | ||
describe('#intword', function() { | ||
it('should be able to use the defaults properly', function() { | ||
humanize.intword(12).should.equal('12.00'); | ||
humanize.intword(999).should.equal('999.00'); | ||
humanize.intword(1001).should.equal('1.00K'); | ||
humanize.intword(Math.pow(1000, 2)).should.equal('1.00M'); | ||
humanize.intword(Math.pow(1000, 3)).should.equal('1.00B'); | ||
humanize.intword(Math.pow(1000, 4)).should.equal('1.00T'); | ||
humanize.intword(1234567890).should.equal('1.23B'); | ||
}); | ||
it('should be able to change units or kilo to a different value', function() { | ||
var units = ['ones', 'thousands', 'millions', 'billions', 'trillions']; | ||
humanize.intword(12, units, 1000, 0, '.', ',', ' ').should.equal('12 ones'); | ||
humanize.intword(999, units, 1000, 0, '.', ',', ' ').should.equal('999 ones'); | ||
humanize.intword(1024, units, 1000, 0, '.', ',', ' ').should.equal('1 thousands'); | ||
humanize.intword(Math.pow(1000, 2), units, 1000, 0, '.', ',', ' ').should.equal('1 millions'); | ||
humanize.intword(Math.pow(1000, 3), units, 1000, 0, '.', ',', ' ').should.equal('1 billions'); | ||
humanize.intword(Math.pow(1000, 4), units, 1000, 0, '.', ',', ' ').should.equal('1 trillions'); | ||
humanize.intword(1234567890, units, 1000, 0, '.', ',', ' ').should.equal('1 billions'); | ||
}); | ||
}); | ||
describe('#linebreaks', function() { | ||
@@ -334,0 +359,0 @@ it('should wrap the string with <p> tags', function() { |
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
46393
155438
8
1009