isInt.js 862 B

12345678910111213141516
  1. import assertString from './util/assertString';
  2. var _int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/;
  3. var intLeadingZeroes = /^[-+]?[0-9]+$/;
  4. export default function isInt(str, options) {
  5. assertString(str);
  6. options = options || {}; // Get the regex to use for testing, based on whether
  7. // leading zeroes are allowed or not.
  8. var regex = options.hasOwnProperty('allow_leading_zeroes') && !options.allow_leading_zeroes ? _int : intLeadingZeroes; // Check min/max/lt/gt
  9. var minCheckPassed = !options.hasOwnProperty('min') || str >= options.min;
  10. var maxCheckPassed = !options.hasOwnProperty('max') || str <= options.max;
  11. var ltCheckPassed = !options.hasOwnProperty('lt') || str < options.lt;
  12. var gtCheckPassed = !options.hasOwnProperty('gt') || str > options.gt;
  13. return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed;
  14. }