Skip to content

Commit 581b2b5

Browse files
committed
Fix return value for un-parsable strings
fixes #61
1 parent 1925beb commit 581b2b5

File tree

4 files changed

+11
-0
lines changed

4 files changed

+11
-0
lines changed

History.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
==========
3+
4+
* Fix return value for un-parsable strings
5+
16
3.1.1 / 2021-11-15
27
==================
38

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,9 @@ function parse(val) {
162162
unit = results[4].toLowerCase();
163163
}
164164

165+
if (isNaN(floatValue)) {
166+
return null;
167+
}
168+
165169
return Math.floor(map[unit] * floatValue);
166170
}

test/byte-parse.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('Test byte parse function', function(){
1212
assert.strictEqual(bytes.parse(NaN), null);
1313
assert.strictEqual(bytes.parse(function(){}), null);
1414
assert.strictEqual(bytes.parse({}), null);
15+
assert.strictEqual(bytes.parse('foobar'), null);
1516
});
1617

1718
it('Should parse raw number', function(){

test/bytes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('Test constructor', function(){
1616
assert.strictEqual(bytes(NaN), null);
1717
assert.strictEqual(bytes(function(){}), null);
1818
assert.strictEqual(bytes({}), null);
19+
assert.strictEqual(bytes('foobar'), null);
1920
});
2021

2122
it('Should be able to parse a string into a number', function(){

0 commit comments

Comments
 (0)