-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from lleao/feature/desacoplarMOD11
- Separação do MOD11 da validação
- Loading branch information
Showing
6 changed files
with
86 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
/lib | ||
coverage | ||
coverage | ||
.history/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Calcula o MOD11 para o número informado | ||
* @param valor Número base para o calculo | ||
* @param limite Limite da casa de multiplicação | ||
* Para CPF assume o valor de 12, para CNPJ o valor de 9. | ||
* @returns Retorna o DV calculado | ||
*/ | ||
export const mod11: (v: string, l: number) => number = ( | ||
clearValue: string, | ||
limite: number, | ||
) => { | ||
const valor = String(clearValue).replace(/\D/g, ''); | ||
let sum = 0; | ||
let mult = 2; | ||
|
||
for (let i = valor.length - 1; i >= 0; i--) { | ||
sum += mult * +valor[i]; | ||
if (++mult > limite) { | ||
mult = 2; | ||
} | ||
} | ||
const dv = ((sum * 10) % 11) % 10; | ||
return dv; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,35 @@ | ||
import { mod11 } from './mod11'; | ||
|
||
/** | ||
* Função que valida se a string é apenas | ||
* números repetidos. | ||
* | ||
* @param ref String númerica | ||
* @returns True se for contida por apenas caracteres repetidos, | ||
* false caso contrário | ||
*/ | ||
const isRepeated = (ref: string) => { | ||
const ret = ref.replace(new RegExp(ref[0], 'g'), '').trim().length === 0; | ||
return ret; | ||
}; | ||
/** | ||
* Valida o CPF. A entrada pode ser com ou sem máscaras. | ||
* O tamanho deve ser respeitado como em '000.000.000-00' ou '00000000000'. | ||
* @param value | ||
*/ | ||
export function validateCPF(value: string): boolean { | ||
// Campo sem máscara | ||
const clearValue = String(value).replace(/\D/g, ''); | ||
let sum = 0; | ||
let rest; | ||
|
||
if (clearValue === '') return false; | ||
|
||
if (clearValue === '00000000000') return false; | ||
|
||
for (let i = 1; i <= 9; i += 1) | ||
sum += +clearValue.substring(i - 1, i) * (11 - i); | ||
|
||
rest = (sum * 10) % 11; | ||
|
||
if (rest === 10 || rest === 11) rest = 0; | ||
|
||
if (rest !== +clearValue.substring(9, 10)) return false; | ||
|
||
sum = 0; | ||
|
||
for (let i = 1; i <= 10; i += 1) | ||
sum += +clearValue.substring(i - 1, i) * (12 - i); | ||
|
||
rest = (sum * 10) % 11; | ||
|
||
if (rest === 10 || rest === 11) rest = 0; | ||
|
||
if (rest !== +clearValue.substring(10, 11)) return false; | ||
|
||
return true; | ||
// O CPF possui 2 DVs, excluíndo para validar | ||
const valWithoutDvs = clearValue.substring(0, clearValue.length - 2); | ||
// Valida se está vazio ou é valor repetido | ||
if (!clearValue || isRepeated(clearValue)) { | ||
return false; | ||
} | ||
// Calcula o primeiro DV | ||
const dv1 = mod11(valWithoutDvs, 12); | ||
// Calcula o segundo DV2 | ||
const dv2 = mod11(valWithoutDvs + dv1, 12); | ||
// Compara com a informação passada como paramêtro | ||
return valWithoutDvs + dv1 + dv2 === clearValue; | ||
} |