Skip to content

Instantly share code, notes, and snippets.

@trfiladelfo
Last active October 3, 2022 17:10
Show Gist options
  • Save trfiladelfo/0abfb4bc93bd77e94e74a8ba52c8e06a to your computer and use it in GitHub Desktop.
Save trfiladelfo/0abfb4bc93bd77e94e74a8ba52c8e06a to your computer and use it in GitHub Desktop.
Validador de CNPJ em Kotlin
fun isCNPJ(document: String): Boolean {
if (document.isEmpty()) return false
val numbers = document.filter { it.isDigit() }.map {
it.toString().toInt()
}.toMutableList()
if (numbers.size != 14) return false
//repeticao
if (numbers.all { it == numbers[0] }) return false
//digito 1
val dv1 = 11 - (arrayOf(5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2).mapIndexed { index, i ->
i * numbers[index]
}).sum().rem(11)
numbers.add(dv1)
//digito 2
val dv2 = 11 - (arrayOf(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2).mapIndexed { index, i ->
i * numbers[index]
}).sum().rem(11)
return numbers[12] == dv1 && numbers[13] == dv2
}
@Burketa
Copy link

Burketa commented Oct 3, 2022

Oie :D

No digito verificador 1, precisou fazer uma adaptação pra checar se dv1 >= 10, se for, dv1 deveria receber 0

Fonte: Link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment