@cloudcome/utils-core
Version:
cloudcome core utils
67 lines (66 loc) • 2.1 kB
JavaScript
const specialRE = /[.*+?^=!:${}()|[\]/\\-]/g;
function regexpEscape(string) {
return string.replace(specialRE, "\\$&");
}
const EMAIL_RE = /^\w+[-+.\w]*@([a-z\d-]+\.)+[a-z]{2,5}$/i;
function isEmail(value) {
return EMAIL_RE.test(value);
}
const PHONE_RE = /^1\d{10}$/;
function isPhone(value) {
return PHONE_RE.test(value);
}
const IDNO_RE = /^(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|7[1]|8[1-2]|9[1])\d{4}(18|19|20)\d{2}[01]\d[0123]\d{4}[\dxX]$/;
function isIDNo(value) {
const isSameFormat = IDNO_RE.test(value);
if (!isSameFormat) return false;
const year = Number(value.slice(6, 10));
const month = Number(value.slice(10, 12));
const date = Number(value.slice(12, 14));
const d = new Date(year, month - 1, date);
const isSameDate = d.getFullYear() === year && d.getMonth() + 1 === month && d.getDate() === date;
if (!isSameDate) return false;
const coefficientList = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
const residueList = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
let sum = 0;
for (let start = 0; start < 17; start++) {
sum += Number(value.slice(start, start + 1)) * coefficientList[start];
}
return residueList[sum % 11] === value.slice(-1);
}
const URL_RE = /^https?:\/\/(([a-z\d-]+\.)+[a-z]{2,5}|(\d{1,3}\.){3}\d{1,3})(:[1-9]\d{0,4})?(\/|\/[\w#!:.?+=&%@'/()-]+)?$/i;
function isURL(value) {
return URL_RE.test(value);
}
const IPV4_RE = /^(?:(?:\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/;
function isIPV4(value) {
return IPV4_RE.test(value);
}
const INTEGER_RE = /^(-?[1-9]\d*|0)$/;
function isInteger(value) {
return INTEGER_RE.test(value);
}
const FLOAT_RE = /^-?([1-9]\d*|0)\.\d+$/;
function isFloat(value) {
return FLOAT_RE.test(value);
}
function isNumerical(value) {
return isInteger(value) || isFloat(value);
}
const DIGIT_RE = /^\d+$/;
function isDigit(value) {
return DIGIT_RE.test(value);
}
export {
isDigit,
isEmail,
isFloat,
isIDNo,
isIPV4,
isInteger,
isNumerical,
isPhone,
isURL,
regexpEscape
};
//# sourceMappingURL=regexp.mjs.map