Skip to content

Commit

Permalink
add support for 11-digit Titulo Eleitoral numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioltonon committed Sep 13, 2020
1 parent 68e4067 commit 9a12a8a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions titulo_eleitoral.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ type tituloEleitoralNumber string

func ParseTituloEleitoral(number string) (tituloEleitoral, error) {
number = regexp.MustCompile(`[^0-9]`).ReplaceAllString(number, "")
if len(number) != 12 && len(number) != 14 {
switch len(number) {
case 11:
number = "0" + number
case 12:
// OK
default:
return tituloEleitoral{}, ErrIncorrectLenghtTituloEleitoralNumber
}

Expand Down Expand Up @@ -72,13 +77,14 @@ func RandomTituloEleitoralNumber(mask bool) string {
number, _ := strconv.Atoi(string(digitlessTitulo[i]))
sum += number * (i + 2)
}

firstDigit := sum % 11
if firstDigit == 0 {
switch firstDigit {
case 0:
if stateCodeString == "01" || stateCodeString == "02" {
firstDigit = 1
}
}
if firstDigit == 10 {
case 10:
firstDigit = 0
}

Expand All @@ -90,12 +96,12 @@ func RandomTituloEleitoralNumber(mask bool) string {
}
sum = sum + firstDigit*9
secondDigit := sum % 11
if secondDigit == 0 {
switch secondDigit {
case 0:
if stateCodeString == "01" || stateCodeString == "02" {
secondDigit = 1
}
}
if secondDigit == 10 {
case 10:
secondDigit = 0
}

Expand Down

0 comments on commit 9a12a8a

Please sign in to comment.