isInt.js 1.1 KB

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