Untitled
4 years ago in Plain Text
var TEST = function () {
const range = [-2147483648, 2147483647];
const isOutOfRange = (num) => {
if (num < range[0]) {
return range[0];
}
if (range[1] < num) {
return range[1];
}
return false;
}
return {
exec: function (str) {
let len = str.length, i = -1, resNum = 0, tempNumStr = '';
while (i <= len) {
const thisChar = str[++i];
if (/\d/.test(thisChar)) {
tempNumStr += thisChar;
continue;
}
if (thisChar === ' ') {
continue;
}
if (/\+|\-/.test(thisChar)) {
resNum += parseInt(tempNumStr) || 0;
let check = isOutOfRange(resNum);
if (check) {
return check;
}
tempNumStr = thisChar;
continue;
}
resNum += parseInt(tempNumStr) || 0;
let check = isOutOfRange(resNum);
if (check) {
return check;
}
break;
}
return resNum;
}
};
}();