Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of byte strings lacking leading 0 before decimal #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var map = {
pb: Math.pow(1024, 5),
};

var parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb|pb)$/i;
var parseRegExp = /^((-|\+)?(\d*(?:\.\d+)?)) *(kb|mb|gb|tb|pb)$/i;

/**
* Convert the given value in bytes into a string or parse to string to an integer in bytes.
Expand Down
7 changes: 7 additions & 0 deletions test/byte-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Test byte parse function', function(){
assert.strictEqual(bytes.parse(function(){}), null);
assert.strictEqual(bytes.parse({}), null);
assert.strictEqual(bytes.parse('foobar'), null);
assert.strictEqual(bytes.parse('.5'), null);
});

it('Should parse raw number', function(){
Expand Down Expand Up @@ -97,6 +98,7 @@ describe('Test byte parse function', function(){
it('Should accept negative values', function(){
assert.equal(bytes.parse('-1'), -1);
assert.equal(bytes.parse('-1024'), -1024);
assert.equal(bytes.parse('-.5TB'), -0.5 * Math.pow(1024, 4));
assert.equal(bytes.parse('-1.5TB'), -1.5 * Math.pow(1024, 4));
});

Expand All @@ -108,4 +110,9 @@ describe('Test byte parse function', function(){
it('Should allow whitespace', function(){
assert.equal(bytes.parse('1 TB'), 1 * Math.pow(1024, 4));
});

it('Should parse decimal strings without a leading 0', function(){
assert.equal(bytes.parse('.5GB'), bytes.parse('0.5GB'));
assert.equal(bytes.parse('.25tb'), bytes.parse('0.25TB'));
});
});