-
Notifications
You must be signed in to change notification settings - Fork 7
/
cpf.go
44 lines (38 loc) · 986 Bytes
/
cpf.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
44
package brdocs
import (
"fmt"
"github.com/brazanation/go-documents/internal"
"github.com/brazanation/go-documents/internal/calculator"
)
const CpfType internal.DocumentType = "Cpf"
const (
cpfLength int = 11
cpfNumberOfDigits int = 2
cpfValidatorRegex string = `^([\d]{3})([\d]{3})([\d]{3})([\d]{2})$`
cpfFormatterRegex string = "$1.$2.$3-$4"
)
type cpf struct {
}
// NewCpf is the constructor of cpf
func NewCpf(number string) (internal.Document, error) {
d, err := internal.NewDocument(
CpfType,
number,
cpfLength,
cpfNumberOfDigits,
cpfFormatterRegex,
cpfValidatorRegex,
cpf{},
)
return d, err
}
func (c cpf) CalculateDigit(base string) string {
m := calculator.NewModule11(base)
m.WithMultipliersInterval(cpfNumberOfDigits, cpfLength)
m.UseComplementaryInsteadOfModule()
m.ReplaceWhen(0, 10, 11)
firstDigit := m.Calculate()
m.AddDigit(firstDigit)
secondDigit := m.Calculate()
return fmt.Sprintf("%d%d", firstDigit, secondDigit)
}