From a77f767d89dbe99de9857c4457d783cbed39d081 Mon Sep 17 00:00:00 2001 From: peterdemartini Date: Tue, 7 Jan 2020 15:26:56 -0700 Subject: [PATCH] fix(toFloat): verify the string can be safely converted to a float (#1226) 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). --- src/lib/toFloat.js | 5 +++-- test/sanitizers.js | 1 + test/validators.js | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib/toFloat.js b/src/lib/toFloat.js index eb3eb5a33..fa7d7b7c7 100644 --- a/src/lib/toFloat.js +++ b/src/lib/toFloat.js @@ -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); } diff --git a/test/sanitizers.js b/test/sanitizers.js index 869b34f04..c523fe029 100644 --- a/test/sanitizers.js +++ b/test/sanitizers.js @@ -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, }, }); diff --git a/test/validators.js b/test/validators.js index 8a9e77569..c2f2a2fb9 100644 --- a/test/validators.js +++ b/test/validators.js @@ -2335,6 +2335,8 @@ describe('Validators', () => { '', '.', 'foo', + '20.foo', + '2020-01-06T14:31:00.135Z', ], }); @@ -3187,6 +3189,7 @@ describe('Validators', () => { '101', 'foo', '', + '2020-01-06T14:31:00.135Z', ], }); });