From 205834da84bdd8767ae77896b8f371a10a8d13c4 Mon Sep 17 00:00:00 2001 From: Vivek Halder <139625193+VivekHalder@users.noreply.github.com> Date: Tue, 1 Oct 2024 12:35:09 +0530 Subject: [PATCH 1/2] feat: Add scientific notation support to isNumeric validator - Updated the isNumeric function to handle scientific notation values. - Modified the regular expression to account for both positive and negative exponents using 'e' or 'E'. - Introduced an option to handle locales with different decimal separators. - Preserved backward compatibility by ensuring the no_symbols option continues to function correctly. - This change validates inputs like 1e5, 1.23E-5, and other variations of scientific notation. --- src/lib/isNumeric.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/lib/isNumeric.js b/src/lib/isNumeric.js index 4cc7ea5b3..cdb3028b2 100644 --- a/src/lib/isNumeric.js +++ b/src/lib/isNumeric.js @@ -3,10 +3,17 @@ import { decimal } from './alpha'; const numericNoSymbols = /^[0-9]+$/; -export default function isNumeric(str, options) { - assertString(str); - if (options && options.no_symbols) { - return numericNoSymbols.test(str); - } - return (new RegExp(`^[+-]?([0-9]*[${(options || {}).locale ? decimal[options.locale] : '.'}])?[0-9]+$`)).test(str); +export default function isNumeric(str, options = { no_symbols: false }) { + assertString(str); // verify if the str is a string, if not report a TypeError + + // destructure options to extract the required properties + const { locale, no_symbols } = options; + + // deciding the separator upfront (default separator is '.') + const decimalSeparator = locale ? decimal[locale] : '.'; + + // setting the regex depending on the value of no_symbols + const regex = no_symbols ? numericNoSymbols : `^[+-]?([0-9]*[${ decimalSeparator }])?[0-9]+([eE][+-]?[0-9]+)?$`; + + return regex.test(str); } From 2a7e46a794ff71bdae6b6ff754b33a698e5cfb5e Mon Sep 17 00:00:00 2001 From: Vivek Halder <139625193+VivekHalder@users.noreply.github.com> Date: Tue, 1 Oct 2024 13:08:01 +0530 Subject: [PATCH 2/2] fix(isNumeric): resolve ESLint template literal spacing issues - Removed unexpected spaces inside template literals in the isNumeric function. - This fix addresses ESLint errors related to the `template-curly-spacing` rule, ensuring code style consistency. - The changes allow the code to pass linting checks, improving overall code quality. --- src/lib/isNumeric.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/isNumeric.js b/src/lib/isNumeric.js index cdb3028b2..f0fadc9e8 100644 --- a/src/lib/isNumeric.js +++ b/src/lib/isNumeric.js @@ -13,7 +13,7 @@ export default function isNumeric(str, options = { no_symbols: false }) { const decimalSeparator = locale ? decimal[locale] : '.'; // setting the regex depending on the value of no_symbols - const regex = no_symbols ? numericNoSymbols : `^[+-]?([0-9]*[${ decimalSeparator }])?[0-9]+([eE][+-]?[0-9]+)?$`; + const regex = no_symbols ? numericNoSymbols : `^[+-]?([0-9]*[${decimalSeparator}])?[0-9]+([eE][+-]?[0-9]+)?$`; return regex.test(str); }