🇧🇷 CNPJ validation in Go

Adds Base, Order and CheckDigit

+131 -13
+32 -7
cnpj.go
··· 7 7 "strings" 8 8 ) 9 9 10 - 11 - 12 10 func checksum(ds []int64, ref []int64) int64 { 13 11 var s int64 14 12 for i, n := range ref { ··· 22 20 } 23 21 24 22 //IsValid checks whether CNPJ number is valid or not 25 - func IsValid(n string) bool { 23 + func IsValid(n string) bool { 26 24 u := Unmask(n) 27 25 if len(u) != 14 { 28 26 return false ··· 37 35 ds[i] = c 38 36 } 39 37 40 - r1 := []int64{5,4,3,2,9,8,7,6,5,4,3,2} 41 - r2 := []int64{6,5,4,3,2,9,8,7,6,5,4,3,2} 38 + r1 := []int64{5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2} 39 + r2 := []int64{6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2} 42 40 return checksum(ds, r1) == ds[12] && checksum(ds, r2) == ds[13] 43 41 } 44 42 45 43 //Mask returns the CNPJ number formatted 46 - func Mask(n string) string { 44 + func Mask(n string) string { 47 45 u := Unmask(n) 48 46 if len(u) != 14 { 49 47 return n ··· 52 50 } 53 51 54 52 //Unmask removes any non-digit (numeric) from the CNPJ number 55 - func Unmask(n string) string { 53 + func Unmask(n string) string { 56 54 return regexp.MustCompile(`\D`).ReplaceAllString(n, "") 57 55 } 56 + 57 + //Base returns the first 8 digits of the CNPJ number 58 + func Base(n string) string { 59 + if !IsValid(n) { 60 + return "" 61 + } 62 + 63 + return Unmask(n)[0:8] 64 + } 65 + 66 + //Order returns the 9th, 10th, 11th and 12th digits of the CNPJ number. 67 + func Order(n string) string { 68 + if !IsValid(n) { 69 + return "" 70 + } 71 + 72 + return Unmask(n)[8:12] 73 + } 74 + 75 + //CheckDigit returns the last 2 digits of the CNPJ number. 76 + func CheckDigit(n string) string { 77 + if !IsValid(n) { 78 + return "" 79 + } 80 + 81 + return Unmask(n)[12:14] 82 + }
+99 -6
cnpj_test.go
··· 7 7 8 8 func TestMask(t *testing.T) { 9 9 for _, tc := range []struct { 10 - cpf string 10 + cnpj string 11 11 expected string 12 12 }{ 13 13 {"11111111111111", "11.111.111/1111-11"}, 14 14 {"123456", "123456"}, 15 15 {"11223344556677889900", "11223344556677889900"}, 16 16 } { 17 - if got := Mask(tc.cpf); tc.expected != got { 18 - t.Errorf("Mask(\"%s\") = %v; expected %s", tc.cpf, got, tc.expected) 17 + if got := Mask(tc.cnpj); tc.expected != got { 18 + t.Errorf("Mask(\"%s\") = %v; expected %s", tc.cnpj, got, tc.expected) 19 19 } 20 20 } 21 21 } ··· 28 28 29 29 func TestIsValid(t *testing.T) { 30 30 for _, tc := range []struct { 31 - cpf string 31 + cnpj string 32 32 expected bool 33 33 }{ 34 34 {"11222333000181", true}, ··· 38 38 {"12.345.678 9012-34", false}, 39 39 {"AB.CDE.FGH/IJKL-MN", false}, 40 40 } { 41 - if got := IsValid(tc.cpf); tc.expected != got { 42 - t.Errorf("IsValid(%v) = %v; expected %v", tc.cpf, got, tc.expected) 41 + if got := IsValid(tc.cnpj); tc.expected != got { 42 + t.Errorf("IsValid(%v) = %v; expected %v", tc.cnpj, got, tc.expected) 43 + } 44 + } 45 + } 46 + 47 + func TestBase(t *testing.T) { 48 + for _, tc := range []struct { 49 + cnpj string 50 + expected string 51 + }{ 52 + {"11222333000181", "11222333"}, 53 + {"11.222.333/0001-81", "11222333"}, 54 + {"123", ""}, 55 + } { 56 + if got := Base(tc.cnpj); tc.expected != got { 57 + t.Errorf("Base(%s) = %s; expected %s", tc.cnpj, got, tc.expected) 58 + } 59 + 60 + } 61 + } 62 + 63 + func TestOrder(t *testing.T) { 64 + for _, tc := range []struct { 65 + cnpj string 66 + expected string 67 + }{ 68 + {"11222333000181", "0001"}, 69 + {"11.222.333/0001-81", "0001"}, 70 + {"123", ""}, 71 + } { 72 + if got := Order(tc.cnpj); tc.expected != got { 73 + t.Errorf("Order(%s) = %s; expected %s", tc.cnpj, got, tc.expected) 74 + } 75 + 76 + } 77 + } 78 + 79 + func TestCheckDigit(t *testing.T) { 80 + for _, tc := range []struct { 81 + cnpj string 82 + expected string 83 + }{ 84 + {"11222333000181", "81"}, 85 + {"11.222.333/0001-81", "81"}, 86 + {"123", ""}, 87 + } { 88 + if got := CheckDigit(tc.cnpj); tc.expected != got { 89 + t.Errorf("CheckDigit(%s) = %s; expected %s", tc.cnpj, got, tc.expected) 43 90 } 91 + 44 92 } 45 93 } 46 94 ··· 73 121 fmt.Println(Unmask("11.111.111/1111-11")) 74 122 // Output: 11111111111111 75 123 } 124 + 125 + func ExampleBase_validMasked() { 126 + fmt.Println(Base("11.222.333/0001-81")) 127 + // Output: 11222333 128 + } 129 + 130 + func ExampleBase_validUnmasked() { 131 + fmt.Println(Base("11222333000181")) 132 + // Output: 11222333 133 + } 134 + 135 + func ExampleBase_invalid() { 136 + fmt.Println(Base("123")) 137 + // Output: 138 + } 139 + 140 + func ExampleOrder_validMasked() { 141 + fmt.Println(Order("11.222.333/0001-81")) 142 + // Output: 0001 143 + } 144 + 145 + func ExampleOrder_validUnmasked() { 146 + fmt.Println(Order("11222333000181")) 147 + // Output: 0001 148 + } 149 + 150 + func ExampleOrder_invalid() { 151 + fmt.Println(Order("123")) 152 + // Output: 153 + } 154 + 155 + func ExampleCheckDigit_validMasked() { 156 + fmt.Println(CheckDigit("11.222.333/0001-81")) 157 + // Output: 81 158 + } 159 + 160 + func ExampleCheckDigit_validUnmasked() { 161 + fmt.Println(CheckDigit("11222333000181")) 162 + // Output: 81 163 + } 164 + 165 + func ExampleCheckDigit_invalid() { 166 + fmt.Println(CheckDigit("123")) 167 + // Output: 168 + }