From aa95d5461807704f47f09b8343c30b0be30ba88b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20Sima=CC=83o?= Date: Wed, 29 Sep 2021 22:16:31 -0300 Subject: [PATCH] cnpj on the way --- README.md | 2 +- cnpj/lib.go | 16 ++++++++++------ cnpj/lib_test.go | 5 +++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f011391..17b29f3 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/cnpj/lib.go b/cnpj/lib.go index 0339b94..58e4b35 100644 --- a/cnpj/lib.go +++ b/cnpj/lib.go @@ -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] } diff --git a/cnpj/lib_test.go b/cnpj/lib_test.go index 41992b2..3585098 100644 --- a/cnpj/lib_test.go +++ b/cnpj/lib_test.go @@ -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)