Skip to content

Commit

Permalink
Implements voter registration
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagompalte authored and paemuri committed Jan 2, 2025
1 parent 005e8d5 commit a060587
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![go.dev][badge-2-img]][badge-2-link]
[![Go Report Card][badge-3-img]][badge-3-link]

CPF, CNPJ, CEP, CNH, PIS/PASEP, RENAVAM, CNS and license plate validator for Go!
CPF, CNPJ, CEP, CNH, PIS/PASEP, RENAVAM, CNS, license plate and voter registration validator for Go!

Everything in this file, but the [License](#license) section, is in
portuguese.
Expand All @@ -13,7 +13,7 @@ portuguese.

BR Doc é um pacote para validação, tanto do formato quanto dos dígitos,
de documentos brasileiros, como CPF, CNPJ, CEP, CNH, PIS/PASEP, RENAVAM, placa
veicular e RG (só SP e RJ).
veicular, RG (só SP e RJ) e título de eleitor.

Aceito PRs de todas as formas. Está permitido escrever em português,
também. :)
Expand All @@ -33,6 +33,7 @@ Principais funções:
- `func IsMercosulPlate(doc string) bool`
- `func IsCNS(doc string) bool`
- `func IsRG(doc string, uf FederativeUnit) bool`
- `func IsVoterID(doc string) bool`

## Coisas a fazer

Expand All @@ -43,6 +44,7 @@ Principais funções:
- [x] validação de RENAVAM (obrigado @leogregianin!)
- [x] validação de placa veicular
- [x] validação de CNS (obrigado @renatosuero!)
- [x] validação de título de eleitor (obrigado @tiagompalte!)
- [ ] validação de RG
- [x] SP (obrigado @robas!)
- [x] RJ (obrigado @robas!)
Expand All @@ -59,7 +61,6 @@ submitted for inclusion in the work by you shall be in the public
domain, without any additional terms or conditions.

[1]: ./LICENSE

[badge-1-img]: https://img.shields.io/github/license/paemuri/brdoc?style=flat-square
[badge-1-link]: https://github.com/paemuri/brdoc/blob/master/LICENSE
[badge-2-img]: https://img.shields.io/badge/go.dev-reference-007d9c?style=flat-square&logo=go&logoColor=white
Expand Down
39 changes: 39 additions & 0 deletions voterid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package brdoc

import (
"fmt"
"strconv"
)

// IsVoterID verifies if the given string is a valid voter registration document
func IsVoterID(doc string) bool {
if !allDigit(doc) || len(doc) != 12 {
return false
}

docRune := []rune(doc)
docUF := fmt.Sprintf("%d%d", toInt(docRune[8]), toInt(docRune[9]))
docUFInt, _ := strconv.Atoi(docUF)
if docUFInt < 1 || docUFInt > 28 {
return false
}

sumA := 0
for i, digit := range doc[:len(doc)-4] {
sumA += toInt(digit) * (i + 2)
}
dv1 := dvMod11(sumA)

sumB := toInt(docRune[8])*7 + toInt(docRune[9])*8 + dv1*9
dv2 := dvMod11(sumB)

return dv1 == toInt(docRune[10]) && dv2 == toInt(docRune[11])
}

func dvMod11(num int) int {
mod := num % 11
if mod == 10 || mod == 11 {
return 0
}
return mod
}
52 changes: 52 additions & 0 deletions voterid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package brdoc

import "testing"

func TestIsVoterID(t *testing.T) {
for i, tc := range []struct {
name string
expected bool
v string
}{
{"Invalid_ShouldReturnFalse", false, "3467875434578764345789654"},
{"Invalid_ShouldReturnFalse", false, ""},
{"Invalid_ShouldReturnFalse", false, "AAAAAAAAAAA"},
{"Invalid_ShouldReturnFalse", false, "915 5017 0193 0306"},
{"Invalid_ShouldReturnFalse", false, "174 2241 7133 0004"},
{"Invalid_ShouldReturnFalse", false, "259 7557 3388 0001"},
{"Invalid_ShouldReturnFalse", false, "808-2536-1743-0486"},
{"Invalid_ShouldReturnFalse", false, "9999 0236 0200 834"},
{"Invalid_ShouldReturnFalse", false, "111122223333"},
{"Invalid_ShouldReturnFalse", false, "098756718298"},
{"Valid_ShouldReturnTrue", true, "381026662437"},
{"Valid_ShouldReturnTrue", true, "048751641724"},
{"Valid_ShouldReturnTrue", true, "285823622500"},
{"Valid_ShouldReturnTrue", true, "115180722291"},
{"Valid_ShouldReturnTrue", true, "455422250574"},
{"Valid_ShouldReturnTrue", true, "122364350701"},
{"Valid_ShouldReturnTrue", true, "103464042020"},
{"Valid_ShouldReturnTrue", true, "737736771015"},
{"Valid_ShouldReturnTrue", true, "222803251481"},
{"Valid_ShouldReturnTrue", true, "877174621180"},
{"Valid_ShouldReturnTrue", true, "837133461872"},
{"Valid_ShouldReturnTrue", true, "686457161910"},
{"Valid_ShouldReturnTrue", true, "300020430205"},
{"Valid_ShouldReturnTrue", true, "704478161317"},
{"Valid_ShouldReturnTrue", true, "875456481201"},
{"Valid_ShouldReturnTrue", true, "513443030671"},
{"Valid_ShouldReturnTrue", true, "816231560876"},
{"Valid_ShouldReturnTrue", true, "211404870302"},
{"Valid_ShouldReturnTrue", true, "557212661694"},
{"Valid_ShouldReturnTrue", true, "353615220469"},
{"Valid_ShouldReturnTrue", true, "350436562380"},
{"Valid_ShouldReturnTrue", true, "684185562631"},
{"Valid_ShouldReturnTrue", true, "035200670175"},
{"Valid_ShouldReturnTrue", true, "468332130981"},
{"Valid_ShouldReturnTrue", true, "034315432186"},
{"Valid_ShouldReturnTrue", true, "426044362739"},
} {
t.Run(testName(i, tc.name), func(t *testing.T) {
assertEqual(t, tc.expected, IsVoterID(tc.v))
})
}
}

0 comments on commit a060587

Please sign in to comment.