UNPKG

cnpj-alfa-validator

Version:

Um validador de CNPJ alfanumérico

3 lines (2 loc) 1.3 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=function(){function e(){}return e.isValid=function(r){if(!this.regexCaracteresNaoPermitidos.test(r)){var a=this.removeMascaraCNPJ(r);if(this.regexCNPJ.test(a)&&a!==e.cnpjZerado)return a.substring(this.tamanhoCNPJSemDV)===this.calculaDV(a.substring(0,this.tamanhoCNPJSemDV))}return!1},e.calculaDV=function(e){if(!this.regexCaracteresNaoPermitidos.test(e)){var r=this.removeMascaraCNPJ(e);if(this.regexCNPJSemDV.test(r)&&r!==this.cnpjZerado.substring(0,this.tamanhoCNPJSemDV)){for(var a=0,t=0,s=0;s<this.tamanhoCNPJSemDV;s++){var o=r.charCodeAt(s)-this.valorBase;a+=o*this.pesosDV[s+1],t+=o*this.pesosDV[s]}var i=a%11<2?0:11-a%11;return""+i+((t+=i*this.pesosDV[this.tamanhoCNPJSemDV])%11<2?0:11-t%11)}}throw new Error("Não é possível calcular o DV pois o CNPJ fornecido é inválido")},e.removeMascaraCNPJ=function(e){return e.replace(this.regexCaracteresMascara,"")},e}();e.tamanhoCNPJSemDV=12,e.regexCNPJSemDV=/^([A-Z\d]){12}$/,e.regexCNPJ=/^([A-Z\d]){12}(\d){2}$/,e.regexCaracteresMascara=/[./-]/g,e.regexCaracteresNaoPermitidos=/[^A-Z\d./-]/i,e.valorBase="0".charCodeAt(0),e.pesosDV=[6,5,4,3,2,9,8,7,6,5,4,3,2],e.cnpjZerado="00000000000000",exports.CNPJ=e; //# sourceMappingURL=cnpj-alfa-validator.cjs.production.min.js.map