isFloat.js 776 B

12345678910111213141516
  1. import assertString from './util/assertString';
  2. import { decimal } from './alpha';
  3. export default function isFloat(str, options) {
  4. assertString(str);
  5. options = options || {};
  6. var _float = new RegExp("^(?:[-+])?(?:[0-9]+)?(?:\\".concat(options.locale ? decimal[options.locale] : '.', "[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$"));
  7. if (str === '' || str === '.' || str === '-' || str === '+') {
  8. return false;
  9. }
  10. var value = parseFloat(str.replace(',', '.'));
  11. return _float.test(str) && (!options.hasOwnProperty('min') || value >= options.min) && (!options.hasOwnProperty('max') || value <= options.max) && (!options.hasOwnProperty('lt') || value < options.lt) && (!options.hasOwnProperty('gt') || value > options.gt);
  12. }
  13. export var locales = Object.keys(decimal);