Skip to content
Open
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
17 changes: 16 additions & 1 deletion src/lib/isFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@ import assertString from './util/assertString';
import isNullOrUndefined from './util/nullUndefinedCheck';
import { decimal } from './alpha';

// The pattern only varies with the decimal separator, which comes from a fixed set of
// locales, so the compiled regexes are cached instead of being rebuilt on every call.
const floatRegexByDecimal = new Map();

function floatRegex(decimalSeparator) {
let regex = floatRegexByDecimal.get(decimalSeparator);

if (!regex) {
regex = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\\${decimalSeparator}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`);
floatRegexByDecimal.set(decimalSeparator, regex);
}

return regex;
}

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]+))?$`);
const float = floatRegex(options.locale ? decimal[options.locale] : '.');
if (str === '' || str === '.' || str === ',' || str === '-' || str === '+') {
return false;
}
Expand Down
Loading