UNPKG

br-docs-validator

Version:
217 lines (211 loc) 5.87 kB
"use strict"; 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/index.ts var src_exports = {}; __export(src_exports, { cnpj: () => cnpj_exports, cpf: () => cpf_exports, generic: () => generic_exports, utils: () => utils_exports }); module.exports = __toCommonJS(src_exports); // src/cpf.ts var cpf_exports = {}; __export(cpf_exports, { isValid: () => isValid, punctuated: () => punctuated }); var cpfBlacklist = [ "00000000000", "11111111111", "22222222222", "33333333333", "44444444444", "55555555555", "66666666666", "77777777777", "88888888888", "99999999999", "12345678909" ]; function format(cpf) { return (cpf || "").replace(/[.-]/g, ""); } function validFirstDigit(cpf) { const digit = cpf[9]; if (!digit) { return false; } const firstDigit = parseInt(digit, 10); const initialNine = cpf.slice(0, 9).split("").map((n) => parseInt(n, 10)); const multiplied = initialNine.map( (number, index) => number * (cpf.length - (index + 1)) ); const sum = multiplied.reduce((acc, current) => acc + current, 0); const mod = sum % 11; if (mod < 2) { return firstDigit === 0; } return 11 - mod === firstDigit; } function validSecondDigit(cpf) { const digit = cpf[10]; if (!digit) { return false; } const secondDigit = parseInt(digit, 10); const initialTen = cpf.slice(0, 10).split("").map((n) => parseInt(n, 10)); const multiplied = initialTen.map( (number, index) => number * (cpf.length - index) ); const sum = multiplied.reduce((acc, current) => acc + current, 0); const mod = sum % 11; if (mod < 2) { return secondDigit === 0; } return 11 - mod === secondDigit; } function isValid(cpf) { const formatedCpf = format(cpf); if (!formatedCpf) return false; if (formatedCpf.length !== 11) return false; if (cpfBlacklist.includes(formatedCpf)) return false; const firstDigitIsValid = validFirstDigit(formatedCpf); const secondDigitIsValid = validSecondDigit(formatedCpf); return firstDigitIsValid && secondDigitIsValid; } function punctuated(cpf) { return cpf.replace(/^(\d{3})(\d{3})(\d{3})(\d{2})$/, "$1.$2.$3-$4"); } // src/cnpj.ts var cnpj_exports = {}; __export(cnpj_exports, { isValid: () => isValid2, punctuated: () => punctuated2 }); var cnpjBlacklist = [ "00000000000000", "11111111111111", "22222222222222", "33333333333333", "44444444444444", "55555555555555", "66666666666666", "77777777777777", "88888888888888", "99999999999999" ]; function format2(cnpj) { return (cnpj || "").replace(/[-\\/.]/g, ""); } function validFirstDigit2(cnpj) { const digit = cnpj[12]; if (!digit) { return false; } const firstDigit = parseInt(digit, 10); const initialTwelve = cnpj.slice(0, 12).split("").map((n) => parseInt(n, 10)); const multiplied = initialTwelve.map((number, index) => { if (index <= 3) { return number * (5 - index); } return number * (13 - index); }); const sum = multiplied.reduce((acc, current) => acc + current, 0); const mod = sum % 11; if (mod < 2) { return firstDigit === 0; } return 11 - mod === firstDigit; } function validSecondDigit2(cnpj) { const digit = cnpj[13]; if (!digit) { return false; } const secondDigit = parseInt(digit, 10); const firstThirteen = cnpj.slice(0, 13).split("").map((n) => parseInt(n, 10)); const multiplied = firstThirteen.map((number, index) => { if (index <= 4) { return number * (6 - index); } return number * (14 - index); }); const sum = multiplied.reduce((acc, current) => acc + current, 0); const mod = sum % 11; if (mod < 2) { return secondDigit === 0; } return 11 - mod === secondDigit; } function isValid2(cnpj) { const formatedCnpj = format2(cnpj); if (!formatedCnpj) return false; if (formatedCnpj.length !== 14) return false; if (cnpjBlacklist.includes(formatedCnpj)) return false; const firstDigitIsValid = validFirstDigit2(formatedCnpj); const secondDigitIsValid = validSecondDigit2(formatedCnpj); return firstDigitIsValid && secondDigitIsValid; } function punctuated2(cnpj) { return cnpj.replace( /^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/, "$1.$2.$3/$4-$5" ); } // src/utils.ts var utils_exports = {}; __export(utils_exports, { strip: () => strip }); function strip(value) { return (value || "").replace(/[^\d]/g, ""); } // src/generic.ts var generic_exports = {}; __export(generic_exports, { isValid: () => isValid3, punctuated: () => punctuated3 }); function isValid3(cpfOrCnpj) { const formatedCpfOrCnpj = utils_exports.strip(cpfOrCnpj); if (formatedCpfOrCnpj.length === 14) { return cnpj_exports.isValid(cpfOrCnpj); } if (formatedCpfOrCnpj.length === 11) { return cpf_exports.isValid(cpfOrCnpj); } return false; } function punctuated3(cpfOrCnpj) { if (cpfOrCnpj.length === 14) { return cnpj_exports.punctuated(cpfOrCnpj); } if (cpfOrCnpj.length === 11) { return cpf_exports.punctuated(cpfOrCnpj); } return ""; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { cnpj, cpf, generic, utils });