-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcep.go
38 lines (32 loc) · 834 Bytes
/
cep.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
package brazil
import (
"fmt"
"math/rand"
"regexp"
"strconv"
"time"
)
// NewCEPValidator creates a CEP number validator, containing inner mask and validation functions which can be
// accessed via Mask and Validate methods.
func NewCEPValidator() Validator {
return &validator{
maskFunc: func(value string) string {
value = onlyDigits(value)
return fmt.Sprintf("%s-%s", value[:5], value[5:])
},
validationFunc: func(value string) error {
value = onlyDigits(value)
if !regexp.MustCompile(`^\d{8}$`).MatchString(value) {
return ErrInvalidCEPFormat
}
return nil
},
}
}
// RandomCEPNumber generates a random valid CEP number
func RandomCEPNumber() string {
seed := time.Now().UnixNano()
source := rand.NewSource(seed)
r := rand.New(source)
return strconv.Itoa(r.Intn(89999999) + 10000000)
}