You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

15 lines
405 B
JavaScript

"use strict";
const integerRegex = /^-?[0-9]+(?:e[0-9]+)?$/;
// TODO: Other bases than 10
module.exports = function parseIntStrict(number) {
if (typeof number === "number" && Number.isInteger(number)) {
return number;
} else if (typeof number === "string" && integerRegex.test(number)) {
return parseInt(number);
} else {
throw new TypeError(`Input is not an integer or integer string`);
}
};