-
Notifications
You must be signed in to change notification settings - Fork 7
/
cnh.go
53 lines (45 loc) · 1.17 KB
/
cnh.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
45
46
47
48
49
50
51
52
53
package brdocs
import (
"fmt"
"github.com/brazanation/go-documents/internal"
"github.com/brazanation/go-documents/internal/calculator"
)
const CnhType internal.DocumentType = "Cnh"
const (
cnhLength int = 11
cnhNumberOfDigits int = 2
cnhValidatorRegex string = `^([\d]{11})$`
cnhFormatterRegex string = "$1"
)
type cnh struct {
}
// NewCnh is the constructor of cnh
func NewCnh(number string) (internal.Document, error) {
d, err := internal.NewDocument(
CnhType,
number,
cnhLength,
cnhNumberOfDigits,
cnhFormatterRegex,
cnhValidatorRegex,
cnh{},
)
return d, err
}
func (c cnh) CalculateDigit(baseNumber string) string {
firstDigit := c.calculateFirstDigit(baseNumber)
secondDigit := c.calculateSecondDigit(baseNumber)
return fmt.Sprintf("%d%d", firstDigit, secondDigit)
}
func (c cnh) calculateFirstDigit(baseNumber string) int {
m := calculator.NewModule11(baseNumber)
m.WithMultipliersInterval(1, 9)
m.ReplaceWhen(0, 10, 11)
return m.Calculate()
}
func (c cnh) calculateSecondDigit(baseNumber string) int {
m := calculator.NewModule11(baseNumber)
m.WithMultipliers(9, 8, 7, 6, 5, 4, 3, 2, 1)
m.ReplaceWhen(0, 10, 11)
return m.Calculate()
}