Skip to content

Commit

Permalink
fix(toFloat): verify the string can be safely converted to a float (#…
Browse files Browse the repository at this point in the history
…1227)

This is needed because parseFloat('2020-01-06T14:31:00.135Z') will return 2020, even though it should return NaN. This fixes an issue checking isDivisibleBy('2020-01-06T14:31:00.135Z', 2).
  • Loading branch information
peterdemartini authored and profnandaa committed Jan 9, 2020
1 parent ea39867 commit 13d7045
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/lib/toFloat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assertString from './util/assertString';
import isFloat from './isFloat';

export default function toFloat(str) {
assertString(str);
if (!isFloat(str)) return NaN;

return parseFloat(str);
}
1 change: 1 addition & 0 deletions test/sanitizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ describe('Sanitizers', () => {
'2.': 2.0,
'-2.5': -2.5,
'.5': 0.5,
'2020-01-06T14:31:00.135Z': NaN,
foo: NaN,
},
});
Expand Down
3 changes: 3 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2335,6 +2335,8 @@ describe('Validators', () => {
'',
'.',
'foo',
'20.foo',
'2020-01-06T14:31:00.135Z',
],
});

Expand Down Expand Up @@ -3187,6 +3189,7 @@ describe('Validators', () => {
'101',
'foo',
'',
'2020-01-06T14:31:00.135Z',
],
});
});
Expand Down

0 comments on commit 13d7045

Please sign in to comment.