From fa6d80dee9de498d80ba328a34dfb44756c20864 Mon Sep 17 00:00:00 2001 From: Casey Barton Date: Mon, 15 Nov 2021 11:29:40 -0500 Subject: [PATCH 1/2] Support single-character units for parsing --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 4975bfb..6b2bc82 100644 --- a/index.js +++ b/index.js @@ -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. @@ -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); From cd7f5bc165129776867aea5c4ac29461a7a3920d Mon Sep 17 00:00:00 2001 From: Casey Barton Date: Mon, 15 Nov 2021 12:27:34 -0500 Subject: [PATCH 2/2] Added tests --- test/byte-parse.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/byte-parse.js b/test/byte-parse.js index 8891602..4b33088 100644 --- a/test/byte-parse.js +++ b/test/byte-parse.js @@ -26,16 +26,22 @@ 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(){ @@ -43,6 +49,8 @@ describe('Test byte parse function', 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('1m'), 1 * Math.pow(1024, 2)); + assert.equal(bytes.parse('1M'), 1 * Math.pow(1024, 2)); }); it('Should parse GB', function(){ @@ -50,6 +58,8 @@ describe('Test byte parse function', 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('1g'), 1 * Math.pow(1024, 3)); + assert.equal(bytes.parse('1G'), 1 * Math.pow(1024, 3)); }); it('Should parse TB', function(){ @@ -57,16 +67,22 @@ describe('Test byte parse function', 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('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(){ @@ -74,16 +90,22 @@ describe('Test byte parse function', 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('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(){