Used to validate whether type of unknown value is an integer.
function isInteger(value) {
// Validate value is a number
if (typeof (value) !== "number" || isNaN(parseInt(value, 10))) {
return false;
}
// Validate number is an integer
if (Math.floor(value) !== value) {
return false;
}
// Validate number is finite
if (!isFinite(value)) {
return false;
}
return true;
}
Used to validate whether type of unknown value is an integer, and, if so, is it within the option lower and upper limits.
function isIntegerInRange(value, lowerLimit, upperLimit) {
// Validate value is an integer
if (!isInteger(value)) {
return false;
}
// Validate integer is in range
var hasLowerLimt = (typeof (lowerLimit) === "number"),
hasUpperLimt = (typeof (upperLimit) === "number");
return ((!hasLowerLimt || value >= lowerLimit) && (!hasUpperLimt || value <= upperLimit));
}
// Define public API
exports.isInteger = isInteger;
exports.isIntegerInRange = isIntegerInRange;
});