Last active
October 3, 2022 17:10
-
-
Save trfiladelfo/0abfb4bc93bd77e94e74a8ba52c8e06a to your computer and use it in GitHub Desktop.
Validador de CNPJ em Kotlin
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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oie :D
No digito verificador 1, precisou fazer uma adaptação pra checar se
dv1 >= 10
, se for, dv1 deveria receber 0Fonte: Link