validation-box
Version:
The only validation library - with flexible regex - you need.
83 lines (82 loc) • 2.85 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/validators/countries/brasil.ts
var brasil_exports = {};
__export(brasil_exports, {
validateCNPJ: () => validateCNPJ,
validateCPF: () => validateCPF,
validatePhoneBR: () => validatePhoneBR
});
module.exports = __toCommonJS(brasil_exports);
var validateCPF = (cpf) => {
cpf = cpf.replace(/\D/g, "");
if (cpf.length !== 11) return false;
if (/^(\d)\1{10}$/.test(cpf)) return false;
let sum = 0;
for (let i = 0; i < 9; i++) {
sum += parseInt(cpf.charAt(i)) * (10 - i);
}
let remainder = sum * 10 % 11;
if (remainder === 10 || remainder === 11) remainder = 0;
if (remainder !== parseInt(cpf.charAt(9))) return false;
sum = 0;
for (let i = 0; i < 10; i++) {
sum += parseInt(cpf.charAt(i)) * (11 - i);
}
remainder = sum * 10 % 11;
if (remainder === 10 || remainder === 11) remainder = 0;
if (remainder !== parseInt(cpf.charAt(10))) return false;
return true;
};
var validateCNPJ = (cnpj) => {
cnpj = cnpj.replace(/\D/g, "");
if (cnpj.length !== 14) return false;
if (/^(\d)\1{13}$/.test(cnpj)) return false;
let sum = 0;
let weight = 5;
for (let i = 0; i < 12; i++) {
sum += parseInt(cnpj.charAt(i)) * weight;
weight = weight === 2 ? 9 : weight - 1;
}
let remainder = sum % 11;
let digit1 = remainder < 2 ? 0 : 11 - remainder;
sum = 0;
weight = 6;
for (let i = 0; i < 13; i++) {
sum += parseInt(cnpj.charAt(i)) * weight;
weight = weight === 2 ? 9 : weight - 1;
}
remainder = sum % 11;
let digit2 = remainder < 2 ? 0 : 11 - remainder;
return digit1 === parseInt(cnpj.charAt(12)) && digit2 === parseInt(cnpj.charAt(13));
};
var validatePhoneBR = (phone, requireCountryCode = false) => {
const countryCodeRegex = requireCountryCode ? "(\\+55|55)" : "(\\+55|55)?";
const regex = new RegExp(
`^${countryCodeRegex}\\s?\\d{2}\\s?\\d{4,5}\\s?\\d{4}$`
);
return regex.test(phone);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
validateCNPJ,
validateCPF,
validatePhoneBR
});
//# sourceMappingURL=brasil.js.map