-
Notifications
You must be signed in to change notification settings - Fork 7
/
cnpj.go
43 lines (37 loc) · 961 Bytes
/
cnpj.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package brdocs
import (
"fmt"
"github.com/brazanation/go-documents/internal"
"github.com/brazanation/go-documents/internal/calculator"
)
const CnpjType internal.DocumentType = "Cnpj"
const (
cnpjLength int = 14
cnpjNumberOfDigits int = 2
cnpjValidatorRegex string = `^([\d]{2})([\d]{3})([\d]{3})([\d]{4})([\d]{2})$`
cnpjFormatterRegex string = "$1.$2.$3/$4-$5"
)
type cnpj struct {
}
// NewCnpj is the constructor of cnpj
func NewCnpj(number string) (internal.Document, error) {
d, err := internal.NewDocument(
CnpjType,
number,
cnpjLength,
cnpjNumberOfDigits,
cnpjFormatterRegex,
cnpjValidatorRegex,
cnpj{},
)
return d, err
}
func (cnpj cnpj) CalculateDigit(base string) string {
c := calculator.NewModule11(base)
c.UseComplementaryInsteadOfModule()
c.ReplaceWhen(0, 10, 11)
firstDigit := c.Calculate()
c.AddDigit(firstDigit)
secondDigit := c.Calculate()
return fmt.Sprintf("%d%d", firstDigit, secondDigit)
}