-
-
Save f3llps/a5ff113a1af644286d3cf5ae8eb367e7 to your computer and use it in GitHub Desktop.
Validador de CPF 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 isCPF(document: String): Boolean { | |
if (document.isEmpty()) return false | |
val numbers = document.filter { it.isDigit() }.map { | |
it.toString().toInt() | |
} | |
if (numbers.size != 11) return false | |
//repeticao | |
if (numbers.all { it == numbers[0] }) return false | |
//digito 1 | |
val dv1 = ((0..8).sumOf { (it + 1) * numbers[it] }).rem(11).let { | |
if (it >= 10) 0 else it | |
} | |
val dv2 = ((0..8).sumOf { it * numbers[it] }.let { (it + (dv1 * 9)).rem(11) }).let { | |
if (it >= 10) 0 else it | |
} | |
return numbers[9] == dv1 && numbers[10] == dv2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment