cnpj on the way

master
Paulo Simão 2021-09-29 22:16:31 -03:00
parent 5b31c05c53
commit aa95d54618
3 changed files with 16 additions and 7 deletions

View File

@ -1,6 +1,6 @@
# Validators
Package with multiple complex validation entities (only CPF now)
Package with multiple complex validation entities (only CPF & CNPJ now)
Usage:

View File

@ -38,13 +38,15 @@ func Check(s string) bool {
for _, v := range nums[0 : len(nums)-2] {
d1 = d1 + (v * mul)
mul--
if mul == 2 {
if mul == 1 {
mul = 9
}
}
mod1 := 11 - ((d1) % 11)
if mod1 > 10 {
mod1 := ((d1) % 11)
if mod1 < 2 {
mod1 = 0
} else {
mod1 = 11 - mod1
}
d2 := 0
@ -52,14 +54,16 @@ func Check(s string) bool {
for _, v := range nums[0 : len(nums)-2] {
d2 = d2 + (v * mul)
mul--
if mul == 2 {
if mul == 1 {
mul = 9
}
}
d2 = d2 + (mod1 * mul)
mod2 := 11 - ((d2) % 11)
if mod2 > 10 {
mod2 := (d2) % 11
if mod2 < 2 {
mod2 = 0
} else {
mod2 = 11 - mod2
}
return mod1 == nums[len(nums)-2] && mod2 == nums[len(nums)-1]
}

View File

@ -12,6 +12,11 @@ func TestFormat(t *testing.T) {
func TestCheck(t *testing.T) {
for _, v := range []string{
"60.119.200/0001-90",
"77.791.482/0001-70",
"78.803.323/0001-00",
"92.632.766/0001-07",
"42.756.293/0001-81",
"59.663.919/0001-08",
} {
ret := Check(v)
log.Printf("%s=>%v", v, ret)