Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Separação do MOD11 da validação #14

Merged
merged 5 commits into from
Aug 17, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
- Torna o parametro de limite obrigatório
  • Loading branch information
lleao committed Aug 17, 2020
commit 1f25efb95d486cbfca1442398b214044c7fb4012
11 changes: 6 additions & 5 deletions src/validations/mod11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
* 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. Caso não informado,
* assume que o limite é o tamanho(string.length) do valor informado
* 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 = (
export const mod11: (v: string, l: number) => number = (
clearValue: string,
limite?: number,
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 || valor.length - 1)) mult = 2;
if (++mult > limite){
mult = 2
};
}
const dv = ((sum * 10) % 11) % 10;
return dv;
Expand Down