fix(isFloat): reject bare-exponent strings like "e5" - #2831
Draft
nikolauspschuetz wants to merge 1 commit into
Draft
fix(isFloat): reject bare-exponent strings like "e5"#2831nikolauspschuetz wants to merge 1 commit into
nikolauspschuetz wants to merge 1 commit into
Conversation
isFloat returned true for strings that are only an exponent with no
mantissa ("e5", "E5", ".e3", "e-3", "+e5") because every segment of
the validation regex is optional. parseFloat returns NaN for these, so
the values are not floats.
Guard the result with !Number.isNaN(value). Computing value with a
locale-aware decimal separator (instead of a hardcoded comma) keeps the
NaN check correct for non-'.'/',' locales such as ar-JO.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2831 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2600 2601 +1
Branches 659 659
=========================================
+ Hits 2600 2601 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
isFloatreturnstruefor strings that are only an exponent, with no mantissa:The validation regex makes every segment optional — sign, integer part, fraction, and exponent — so a string consisting of just an exponent matches. The function never rejects the
NaNthat its ownparseFloatproduces.The bug only surfaces on the common no-
min/maxcall: when a bound is supplied,NaN >= minisfalseand the string is (incidentally) rejected.Fix
Guard the result with
!Number.isNaN(value).valuewas computed asparseFloat(str.replace(',', '.'))— only the comma separator was normalized. To keep theNaNcheck correct for locales whose decimal separator is neither.nor,(e.g.ar-JO, which uses٫),valueis now computed with the locale-aware separator. This is a no-op for the default/en/de-DElocales and makes the guard sound across all locales.Test
Added
'e5','E5','.e3','e-3','+e5'to theinvalidlist of the defaultisFloattest.Verified fails-before / passes-after: with the src change reverted the
should validate floatssuite fails on the new cases; with the fix the full suite (default,en-AU,de-DE,ar-JO, and themin/max/lt/gtblocks) passes.