Skip to content

Commit

Permalink
perf: avoid useless regex
Browse files Browse the repository at this point in the history
  • Loading branch information
cesco69 authored Jan 7, 2025
1 parent 86911d8 commit b0b3e72
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/lib/isFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import assertString from './util/assertString';
import isNullOrUndefined from './util/nullUndefinedCheck';
import { decimal } from './alpha';

const regExIsFloat = (str, options) => {
const float = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\\${options.locale ? decimal[options.locale] : '.'}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`);
return float.test(str);
}

Check failure on line 8 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Missing semicolon

Check failure on line 8 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Missing semicolon

Check failure on line 8 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Missing semicolon

Check failure on line 8 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Missing semicolon

Check failure on line 8 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Missing semicolon

Check failure on line 8 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Missing semicolon

Check failure on line 8 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Missing semicolon

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]+))?$`);
if (str === '' || str === '.' || str === ',' || str === '-' || str === '+') {
return false;
}
const value = parseFloat(str.replace(',', '.'));
return float.test(str) &&
return (options.locale ? regExIsFloat(str) : (!isNaN(value) || regExIsFloat(str)) ) &&

Check failure on line 17 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

There should be no spaces inside this paren

Check failure on line 17 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

There should be no spaces inside this paren

Check failure on line 17 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

There should be no spaces inside this paren

Check failure on line 17 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

There should be no spaces inside this paren

Check failure on line 17 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

There should be no spaces inside this paren

Check failure on line 17 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

There should be no spaces inside this paren

Check failure on line 17 in src/lib/isFloat.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

There should be no spaces inside this paren
(!options.hasOwnProperty('min') || isNullOrUndefined(options.min) || value >= options.min) &&
(!options.hasOwnProperty('max') || isNullOrUndefined(options.max) || value <= options.max) &&
(!options.hasOwnProperty('lt') || isNullOrUndefined(options.lt) || value < options.lt) &&
Expand Down

0 comments on commit b0b3e72

Please sign in to comment.