1matriz = matriz || {}; 2matriz.datatype = matriz.datatype || {} 3matriz.datatype.CPF = function (cpf) { 4 5 validaSomenteNumeros(); 6 validaTamanho(); 7 validaDigitoVerificador(); 8 9 return { 10 asString : function() { 11 return cpf; 12 } 13 }; 14 15 function validaSomenteNumeros() { 16 let regCpf = /^[0-9]+$/g; 17 let match = cpf.match(regCpf); 18 19 if (!match) { 20 throw new Error('CPF deve ser composto por apenas números.'); 21 } 22 return cpf; 23 } 24 25 function validaTamanho() { 26 if (cpf.length != 11) { 27 throw new Error("CPF deve possuir 11 digitos."); 28 } 29 return cpf; 30 } 31 32 function validaDigitoVerificador() { 33 var cpfDigito = cpf.substring(0, 9); 34 var primeiroDigito = calculaPrimeiro(cpfDigito); 35 var segundoDigito = calculaSegundo(cpfDigito, primeiroDigito); 36 37 var concatDigitos = `${primeiroDigito}${segundoDigito}`; 38 var concatCpf = `${cpf[9]}${cpf[10]}`; 39 40 41 if (concatDigitos !== concatCpf) { 42 throw new Error("CPF inválido, pois o digito verificador não está correto."); 43 } 44 45 } 46 47 function calculaPrimeiro(cpfDigito) { 48 var somaPrimeiro = somaDigito(cpfDigito); 49 return mod11(somaPrimeiro); 50 51 52 } 53 54 function calculaSegundo(cpfDigito, primeiroDigito) { 55 var cpfSegundo = cpfDigito.concat(primeiroDigito); 56 var somaSegundo = somaDigito(cpfSegundo); 57 return mod11(somaSegundo); 58 } 59 60 function somaDigito(cpfDigito) { 61 var inicio = cpfDigito.length - 1; 62 var fator = 2; 63 var soma = 0; 64 65 66 for (let i = inicio; i >= 0; i--) { 67 soma += (parseInt(cpfDigito[i]) * fator); 68 fator++; 69 70 } 71 72 return soma; 73 } 74 75 function mod11(soma) { 76 var mod11 = soma % 11; 77 if (mod11 < 2) { 78 return 0; 79 } else { 80 return 11 - mod11; 81 } 82 83 } 84 85};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.