-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
isInt.js
22 lines (17 loc) · 1.01 KB
/
isInt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import assertString from './util/assertString';
import isNullOrUndefined from './util/nullUndefinedCheck';
const int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/;
const intLeadingZeroes = /^[-+]?[0-9]+$/;
export default function isInt(str, options) {
assertString(str);
options = options || {};
// Get the regex to use for testing, based on whether
// leading zeroes are allowed or not.
const regex = options.allow_leading_zeroes === false ? int : intLeadingZeroes;
// Check min/max/lt/gt
let minCheckPassed = (!options.hasOwnProperty('min') || isNullOrUndefined(options.min) || str >= options.min);
let maxCheckPassed = (!options.hasOwnProperty('max') || isNullOrUndefined(options.max) || str <= options.max);
let ltCheckPassed = (!options.hasOwnProperty('lt') || isNullOrUndefined(options.lt) || str < options.lt);
let gtCheckPassed = (!options.hasOwnProperty('gt') || isNullOrUndefined(options.gt) || str > options.gt);
return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed;
}