Skip to content

Commit 13d7045

Browse files
peterdemartiniprofnandaa
authored andcommitted
fix(toFloat): verify the string can be safely converted to a float (#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).
1 parent ea39867 commit 13d7045

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

src/lib/toFloat.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import assertString from './util/assertString';
1+
import isFloat from './isFloat';
22

33
export default function toFloat(str) {
4-
assertString(str);
4+
if (!isFloat(str)) return NaN;
5+
56
return parseFloat(str);
67
}

test/sanitizers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ describe('Sanitizers', () => {
137137
'2.': 2.0,
138138
'-2.5': -2.5,
139139
'.5': 0.5,
140+
'2020-01-06T14:31:00.135Z': NaN,
140141
foo: NaN,
141142
},
142143
});

test/validators.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,8 @@ describe('Validators', () => {
23352335
'',
23362336
'.',
23372337
'foo',
2338+
'20.foo',
2339+
'2020-01-06T14:31:00.135Z',
23382340
],
23392341
});
23402342

@@ -3187,6 +3189,7 @@ describe('Validators', () => {
31873189
'101',
31883190
'foo',
31893191
'',
3192+
'2020-01-06T14:31:00.135Z',
31903193
],
31913194
});
31923195
});

0 commit comments

Comments
 (0)