isInt.js 985 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isInt;
  6. var _assertString = require('./util/assertString');
  7. var _assertString2 = _interopRequireDefault(_assertString);
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. var int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/;
  10. var intLeadingZeroes = /^[-+]?[0-9]+$/;
  11. function isInt(str, options) {
  12. (0, _assertString2.default)(str);
  13. options = options || {};
  14. // Get the regex to use for testing, based on whether
  15. // leading zeroes are allowed or not.
  16. var regex = options.hasOwnProperty('allow_leading_zeroes') && options.allow_leading_zeroes ? intLeadingZeroes : int;
  17. // Check min/max
  18. var minCheckPassed = !options.hasOwnProperty('min') || str >= options.min;
  19. var maxCheckPassed = !options.hasOwnProperty('max') || str <= options.max;
  20. return regex.test(str) && minCheckPassed && maxCheckPassed;
  21. }
  22. module.exports = exports['default'];