tangled
alpha
login
or
join now
cuducos.me
/
go-cnpj
0
fork
atom
🇧🇷 CNPJ validation in Go
0
fork
atom
overview
issues
pulls
pipelines
Adds Base, Order and CheckDigit
Eduardo Cuducos
4 years ago
06e49ada
4d215db0
+131
-13
2 changed files
expand all
collapse all
unified
split
cnpj.go
cnpj_test.go
+32
-7
cnpj.go
reviewed
···
7
7
"strings"
8
8
)
9
9
10
10
-
11
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
25
-
func IsValid(n string) bool {
23
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
40
-
r1 := []int64{5,4,3,2,9,8,7,6,5,4,3,2}
41
41
-
r2 := []int64{6,5,4,3,2,9,8,7,6,5,4,3,2}
38
38
+
r1 := []int64{5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}
39
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
46
-
func Mask(n string) string {
44
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
55
-
func Unmask(n string) string {
53
53
+
func Unmask(n string) string {
56
54
return regexp.MustCompile(`\D`).ReplaceAllString(n, "")
57
55
}
56
56
+
57
57
+
//Base returns the first 8 digits of the CNPJ number
58
58
+
func Base(n string) string {
59
59
+
if !IsValid(n) {
60
60
+
return ""
61
61
+
}
62
62
+
63
63
+
return Unmask(n)[0:8]
64
64
+
}
65
65
+
66
66
+
//Order returns the 9th, 10th, 11th and 12th digits of the CNPJ number.
67
67
+
func Order(n string) string {
68
68
+
if !IsValid(n) {
69
69
+
return ""
70
70
+
}
71
71
+
72
72
+
return Unmask(n)[8:12]
73
73
+
}
74
74
+
75
75
+
//CheckDigit returns the last 2 digits of the CNPJ number.
76
76
+
func CheckDigit(n string) string {
77
77
+
if !IsValid(n) {
78
78
+
return ""
79
79
+
}
80
80
+
81
81
+
return Unmask(n)[12:14]
82
82
+
}
+99
-6
cnpj_test.go
reviewed
···
7
7
8
8
func TestMask(t *testing.T) {
9
9
for _, tc := range []struct {
10
10
-
cpf string
10
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
17
-
if got := Mask(tc.cpf); tc.expected != got {
18
18
-
t.Errorf("Mask(\"%s\") = %v; expected %s", tc.cpf, got, tc.expected)
17
17
+
if got := Mask(tc.cnpj); tc.expected != got {
18
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
31
-
cpf string
31
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
41
-
if got := IsValid(tc.cpf); tc.expected != got {
42
42
-
t.Errorf("IsValid(%v) = %v; expected %v", tc.cpf, got, tc.expected)
41
41
+
if got := IsValid(tc.cnpj); tc.expected != got {
42
42
+
t.Errorf("IsValid(%v) = %v; expected %v", tc.cnpj, got, tc.expected)
43
43
+
}
44
44
+
}
45
45
+
}
46
46
+
47
47
+
func TestBase(t *testing.T) {
48
48
+
for _, tc := range []struct {
49
49
+
cnpj string
50
50
+
expected string
51
51
+
}{
52
52
+
{"11222333000181", "11222333"},
53
53
+
{"11.222.333/0001-81", "11222333"},
54
54
+
{"123", ""},
55
55
+
} {
56
56
+
if got := Base(tc.cnpj); tc.expected != got {
57
57
+
t.Errorf("Base(%s) = %s; expected %s", tc.cnpj, got, tc.expected)
58
58
+
}
59
59
+
60
60
+
}
61
61
+
}
62
62
+
63
63
+
func TestOrder(t *testing.T) {
64
64
+
for _, tc := range []struct {
65
65
+
cnpj string
66
66
+
expected string
67
67
+
}{
68
68
+
{"11222333000181", "0001"},
69
69
+
{"11.222.333/0001-81", "0001"},
70
70
+
{"123", ""},
71
71
+
} {
72
72
+
if got := Order(tc.cnpj); tc.expected != got {
73
73
+
t.Errorf("Order(%s) = %s; expected %s", tc.cnpj, got, tc.expected)
74
74
+
}
75
75
+
76
76
+
}
77
77
+
}
78
78
+
79
79
+
func TestCheckDigit(t *testing.T) {
80
80
+
for _, tc := range []struct {
81
81
+
cnpj string
82
82
+
expected string
83
83
+
}{
84
84
+
{"11222333000181", "81"},
85
85
+
{"11.222.333/0001-81", "81"},
86
86
+
{"123", ""},
87
87
+
} {
88
88
+
if got := CheckDigit(tc.cnpj); tc.expected != got {
89
89
+
t.Errorf("CheckDigit(%s) = %s; expected %s", tc.cnpj, got, tc.expected)
43
90
}
91
91
+
44
92
}
45
93
}
46
94
···
73
121
fmt.Println(Unmask("11.111.111/1111-11"))
74
122
// Output: 11111111111111
75
123
}
124
124
+
125
125
+
func ExampleBase_validMasked() {
126
126
+
fmt.Println(Base("11.222.333/0001-81"))
127
127
+
// Output: 11222333
128
128
+
}
129
129
+
130
130
+
func ExampleBase_validUnmasked() {
131
131
+
fmt.Println(Base("11222333000181"))
132
132
+
// Output: 11222333
133
133
+
}
134
134
+
135
135
+
func ExampleBase_invalid() {
136
136
+
fmt.Println(Base("123"))
137
137
+
// Output:
138
138
+
}
139
139
+
140
140
+
func ExampleOrder_validMasked() {
141
141
+
fmt.Println(Order("11.222.333/0001-81"))
142
142
+
// Output: 0001
143
143
+
}
144
144
+
145
145
+
func ExampleOrder_validUnmasked() {
146
146
+
fmt.Println(Order("11222333000181"))
147
147
+
// Output: 0001
148
148
+
}
149
149
+
150
150
+
func ExampleOrder_invalid() {
151
151
+
fmt.Println(Order("123"))
152
152
+
// Output:
153
153
+
}
154
154
+
155
155
+
func ExampleCheckDigit_validMasked() {
156
156
+
fmt.Println(CheckDigit("11.222.333/0001-81"))
157
157
+
// Output: 81
158
158
+
}
159
159
+
160
160
+
func ExampleCheckDigit_validUnmasked() {
161
161
+
fmt.Println(CheckDigit("11222333000181"))
162
162
+
// Output: 81
163
163
+
}
164
164
+
165
165
+
func ExampleCheckDigit_invalid() {
166
166
+
fmt.Println(CheckDigit("123"))
167
167
+
// Output:
168
168
+
}