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

Support single-character units for parsing #60

Open
wants to merge 2 commits 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
3 changes: 2 additions & 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 Expand Up @@ -156,6 +156,7 @@ function parse(val) {
// Retrieve the value and the unit
floatValue = parseFloat(results[1]);
unit = results[4].toLowerCase();
if (unit.length === 1) unit += 'b';
}

return Math.floor(map[unit] * floatValue);
Expand Down
22 changes: 22 additions & 0 deletions test/byte-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,64 +26,86 @@ describe('Test byte parse function', function(){
assert.equal(bytes.parse('1KB'), 1 * Math.pow(1024, 1));
assert.equal(bytes.parse('1Kb'), 1 * Math.pow(1024, 1));
assert.equal(bytes.parse('1kB'), 1 * Math.pow(1024, 1));
assert.equal(bytes.parse('1k'), 1 * Math.pow(1024, 1));
assert.equal(bytes.parse('1K'), 1 * Math.pow(1024, 1));

assert.equal(bytes.parse('0.5kb'), 0.5 * Math.pow(1024, 1));
assert.equal(bytes.parse('0.5KB'), 0.5 * Math.pow(1024, 1));
assert.equal(bytes.parse('0.5Kb'), 0.5 * Math.pow(1024, 1));
assert.equal(bytes.parse('0.5kB'), 0.5 * Math.pow(1024, 1));
assert.equal(bytes.parse('0.5k'), 0.5 * Math.pow(1024, 1));
assert.equal(bytes.parse('0.5K'), 0.5 * Math.pow(1024, 1));

assert.equal(bytes.parse('1.5kb'), 1.5 * Math.pow(1024, 1));
assert.equal(bytes.parse('1.5KB'), 1.5 * Math.pow(1024, 1));
assert.equal(bytes.parse('1.5Kb'), 1.5 * Math.pow(1024, 1));
assert.equal(bytes.parse('1.5kB'), 1.5 * Math.pow(1024, 1));
assert.equal(bytes.parse('1.5k'), 1.5 * Math.pow(1024, 1));
assert.equal(bytes.parse('1.5K'), 1.5 * Math.pow(1024, 1));
});

it('Should parse MB', function(){
assert.equal(bytes.parse('1mb'), 1 * Math.pow(1024, 2));
assert.equal(bytes.parse('1MB'), 1 * Math.pow(1024, 2));
assert.equal(bytes.parse('1Mb'), 1 * Math.pow(1024, 2));
assert.equal(bytes.parse('1mB'), 1 * Math.pow(1024, 2));
assert.equal(bytes.parse('1m'), 1 * Math.pow(1024, 2));
assert.equal(bytes.parse('1M'), 1 * Math.pow(1024, 2));
});

it('Should parse GB', function(){
assert.equal(bytes.parse('1gb'), 1 * Math.pow(1024, 3));
assert.equal(bytes.parse('1GB'), 1 * Math.pow(1024, 3));
assert.equal(bytes.parse('1Gb'), 1 * Math.pow(1024, 3));
assert.equal(bytes.parse('1gB'), 1 * Math.pow(1024, 3));
assert.equal(bytes.parse('1g'), 1 * Math.pow(1024, 3));
assert.equal(bytes.parse('1G'), 1 * Math.pow(1024, 3));
});

it('Should parse TB', function(){
assert.equal(bytes.parse('1tb'), 1 * Math.pow(1024, 4));
assert.equal(bytes.parse('1TB'), 1 * Math.pow(1024, 4));
assert.equal(bytes.parse('1Tb'), 1 * Math.pow(1024, 4));
assert.equal(bytes.parse('1tB'), 1 * Math.pow(1024, 4));
assert.equal(bytes.parse('1t'), 1 * Math.pow(1024, 4));
assert.equal(bytes.parse('1T'), 1 * Math.pow(1024, 4));

assert.equal(bytes.parse('0.5tb'), 0.5 * Math.pow(1024, 4));
assert.equal(bytes.parse('0.5TB'), 0.5 * Math.pow(1024, 4));
assert.equal(bytes.parse('0.5Tb'), 0.5 * Math.pow(1024, 4));
assert.equal(bytes.parse('0.5tB'), 0.5 * Math.pow(1024, 4));
assert.equal(bytes.parse('0.5t'), 0.5 * Math.pow(1024, 4));
assert.equal(bytes.parse('0.5T'), 0.5 * Math.pow(1024, 4));

assert.equal(bytes.parse('1.5tb'), 1.5 * Math.pow(1024, 4));
assert.equal(bytes.parse('1.5TB'), 1.5 * Math.pow(1024, 4));
assert.equal(bytes.parse('1.5Tb'), 1.5 * Math.pow(1024, 4));
assert.equal(bytes.parse('1.5tB'), 1.5 * Math.pow(1024, 4));
assert.equal(bytes.parse('1.5t'), 1.5 * Math.pow(1024, 4));
assert.equal(bytes.parse('1.5T'), 1.5 * Math.pow(1024, 4));
});

it('Should parse PB', function(){
assert.equal(bytes.parse('1pb'), 1 * Math.pow(1024, 5));
assert.equal(bytes.parse('1PB'), 1 * Math.pow(1024, 5));
assert.equal(bytes.parse('1Pb'), 1 * Math.pow(1024, 5));
assert.equal(bytes.parse('1pB'), 1 * Math.pow(1024, 5));
assert.equal(bytes.parse('1p'), 1 * Math.pow(1024, 5));
assert.equal(bytes.parse('1P'), 1 * Math.pow(1024, 5));

assert.equal(bytes.parse('0.5pb'), 0.5 * Math.pow(1024, 5));
assert.equal(bytes.parse('0.5PB'), 0.5 * Math.pow(1024, 5));
assert.equal(bytes.parse('0.5Pb'), 0.5 * Math.pow(1024, 5));
assert.equal(bytes.parse('0.5pB'), 0.5 * Math.pow(1024, 5));
assert.equal(bytes.parse('0.5p'), 0.5 * Math.pow(1024, 5));
assert.equal(bytes.parse('0.5P'), 0.5 * Math.pow(1024, 5));

assert.equal(bytes.parse('1.5pb'), 1.5 * Math.pow(1024, 5));
assert.equal(bytes.parse('1.5PB'), 1.5 * Math.pow(1024, 5));
assert.equal(bytes.parse('1.5Pb'), 1.5 * Math.pow(1024, 5));
assert.equal(bytes.parse('1.5pB'), 1.5 * Math.pow(1024, 5));
assert.equal(bytes.parse('1.5p'), 1.5 * Math.pow(1024, 5));
assert.equal(bytes.parse('1.5P'), 1.5 * Math.pow(1024, 5));
});

it('Should assume bytes when no units', function(){
Expand Down