Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(isFloat) Fix , passing as float #2020

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/isFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function isFloat(str, options) {
assertString(str);
options = options || {};
const float = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\\${options.locale ? decimal[options.locale] : '.'}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`);
Copy link
Contributor

@gibson042 gibson042 Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const float = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\\${options.locale ? decimal[options.locale] : '.'}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`);
// largely derived from ECMAScript, with optional leading sign and allowing unnecessary leading zeroes
// but not underscore separators
// https://tc39.es/ecma262/multipage/ecmascript-language-lexical-grammar.html#prod-DecimalLiteral
const float = new RegExp(`^[-+]?(?:[0-9]+|(?=.[0-9]))(?:\\${options.locale ? decimal[options.locale] : '.'}[0-9]*)?(?:[eE][-+]?[0-9]+)?$`);

(and if you take this suggestion, then the following if is unnecessary)

if (str === '' || str === '.' || str === '-' || str === '+') {
if (str === '' || str === '.' || str === ',' || str === '-' || str === '+') {
return false;
}
const value = parseFloat(str.replace(',', '.'));
Expand Down
1 change: 1 addition & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3947,6 +3947,7 @@ describe('Validators', () => {
' ',
'',
'.',
',',
'foo',
'20.foo',
'2020-01-06T14:31:00.135Z',
Expand Down