Last active
August 11, 2020 15:19
-
-
Save ermogenes/7cd0d1d6e62fca0b405a2039d4f3937e to your computer and use it in GitHub Desktop.
Verificação de DAC de SQL (IPTU) da Prefeitura de São Paulo/SP em JavaScript
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
const CalculaSQLDigito = (strNumeroSQLSemDigito) => { | |
const peso = [4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
if (strNumeroSQLSemDigito.length > 16) | |
throw "Número máximo de dígitos excedido"; | |
let soma = 0; | |
let i = 15; | |
for ( | |
let posicaoAtual = strNumeroSQLSemDigito.length - 1; | |
posicaoAtual >= 0; | |
posicaoAtual = posicaoAtual - 1 | |
) { | |
soma = soma + parseInt(strNumeroSQLSemDigito[posicaoAtual]) * peso[i]; | |
i--; | |
} | |
let digito = soma % 11; | |
return digito == 10 ? 1 : digito; | |
}; | |
const SQLValido = (strSQLComDigito) => { | |
if (strSQLComDigito.length != 11 || !/[0-9]{11}/.test(strSQLComDigito)) | |
return false; | |
const strSQLSemDigito = strSQLComDigito.slice(0, 10); | |
const strDigitoInformado = strSQLComDigito.slice(10); | |
return CalculaSQLDigito(strSQLSemDigito).toString() === strDigitoInformado; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment