Skip to content

Commit ba3ae20

Browse files
committed
♻️ Improve KotlinDocs
1 parent 0c9e0d8 commit ba3ae20

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/main/kotlin/br/com/colman/simplecpfvalidator/CpfValidator.kt

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Leonardo Colman Lopes
2+
* Copyright 2024 Leonardo Colman Lopes
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
@file:Suppress("MagicNumber")
17+
1718
package br.com.colman.simplecpfvalidator
1819

1920
import kotlin.math.abs
@@ -39,7 +40,7 @@ import kotlin.math.abs
3940
*/
4041
fun String.isCpf(charactersToIgnore: List<Char> = listOf('.', '-')): Boolean {
4142
val cleanCpf = this.filterNot { it in charactersToIgnore }
42-
if (cleanCpf.containsInvalidCPFChars() || cleanCpf.isInvalidCpfSize() || cleanCpf.isBlacklistedCpf()) return false
43+
if (cleanCpf.containsInvalidCPFChars() || cleanCpf.isInvalidCpfSize() || cleanCpf.isInvalidCpf()) return false
4344
return cleanCpf.hasValidVerificationDigits()
4445
}
4546

@@ -60,14 +61,14 @@ fun String.isCpf(charactersToIgnore: List<Char> = listOf('.', '-')): Boolean {
6061
* @see [https://pt.wikipedia.org/wiki/Cadastro_de_pessoas_f%C3%ADsicas]
6162
* @see [http://normas.receita.fazenda.gov.br/sijut2consulta/link.action?visao=anotado&idAto=1893]
6263
*/
63-
fun Long.isCpf() : Boolean {
64+
fun Long.isCpf(): Boolean {
6465
val absNumber = abs(this)
6566
return absNumber.toString().isCpf()
6667
}
6768

6869
private fun String.containsInvalidCPFChars() = this.any { !it.isDigit() }
6970
private fun String.isInvalidCpfSize() = this.length != 11
70-
private fun String.isBlacklistedCpf() = this in blacklistedCpfs
71+
private fun String.isInvalidCpf() = this in invalidCpfs
7172

7273
// Algorithm from https://www.somatematica.com.br/faq/cpf.php
7374
private fun String.hasValidVerificationDigits(): Boolean {
@@ -101,7 +102,7 @@ private fun List<Int>.calculateFirstVerificationDigit(): Int {
101102
val sum = firstNineDigits.withIndex().sumOf { (index, element) -> weights[index] * element }
102103

103104
val remainder = sum % 11
104-
return if(remainder < 2) 0 else 11 - remainder
105+
return if (remainder < 2) 0 else 11 - remainder
105106
}
106107

107108
private fun List<Int>.calculateSecondVerificationDigit(firstDigit: Int): Int {
@@ -127,7 +128,11 @@ private fun List<Int>.calculateSecondVerificationDigit(firstDigit: Int): Int {
127128
return if (remainder < 2) 0 else 11 - remainder
128129
}
129130

130-
private val blacklistedCpfs = listOf(
131+
/**
132+
* These CPFs although are numerically valid (i.e. [hasValidVerificationDigits] are considered invalid as per CPF
133+
* specification
134+
*/
135+
private val invalidCpfs = listOf(
131136
"00000000000",
132137
"11111111111",
133138
"22222222222",

0 commit comments

Comments
 (0)