Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ module.exports.parse = parse;

var formatThousandsRegExp = /\B(?=(\d{3})+(?!\d))/g;

var formatDecimalsRegExp = /(?:\.0*|(\.[^0]+)0+)$/;

var map = {
b: 1,
kb: 1 << 10,
Expand Down Expand Up @@ -112,8 +110,8 @@ function format(value, options) {
var val = value / map[unit.toLowerCase()];
var str = val.toFixed(decimalPlaces);

if (!fixedDecimals) {
str = str.replace(formatDecimalsRegExp, '$1');
if (!fixedDecimals && str.indexOf('.') !== -1) {
str = str.replace(/0+$/, '').replace(/\.$/, '');
}

if (thousandsSeparator) {
Expand Down
5 changes: 5 additions & 0 deletions test/byte-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ describe('Test byte format function', function(){
assert.equal(bytes.format(1.1005 * kb, {decimalPlaces: 4}).toLowerCase(), '1.1005kb');
});

it('Should strip trailing zeros when a fractional zero is interior', function(){
assert.equal(bytes.format(1536, {decimalPlaces: 3}), '1.5KB');
assert.equal(bytes.format(1075, {decimalPlaces: 3}), '1.05KB');
});

it('Should support fixed decimal places', function(){
assert.equal(bytes.format(kb, {decimalPlaces: 3, fixedDecimals: true}).toLowerCase(), '1.000kb');
});
Expand Down