-
Notifications
You must be signed in to change notification settings - Fork 28
/
cep.go
64 lines (56 loc) · 1.72 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package brdoc
import (
"regexp"
"strconv"
)
// Regexp pattern for CEP.
var (
CEPRegexp = regexp.MustCompile(`^\d{5}-?\d{3}$`)
)
// IsCEP verifies if `doc` is a valid CEP.
// `ufs` represents the possible Federative Units the CEP should matches.
// If none is provided, it validates the document for any state/district.
func IsCEP(doc string, ufs ...FederativeUnit) bool {
if !CEPRegexp.MatchString(doc) {
return false
}
h, _ := strconv.Atoi(doc[0:3])
if len(ufs) == 0 {
return h >= 10
}
for _, uf := range ufs {
if (uf == SP && h >= 10 && h <= 199) ||
(uf == RJ && h >= 200 && h <= 289) ||
(uf == ES && h >= 290 && h <= 299) ||
(uf == MG && h >= 300 && h <= 399) ||
(uf == BA && h >= 400 && h <= 489) ||
(uf == SE && h >= 490 && h <= 499) ||
(uf == PE && h >= 500 && h <= 569) ||
(uf == AL && h >= 570 && h <= 579) ||
(uf == PB && h >= 580 && h <= 589) ||
(uf == RN && h >= 590 && h <= 599) ||
(uf == CE && h >= 600 && h <= 639) ||
(uf == PI && h >= 640 && h <= 649) ||
(uf == MA && h >= 650 && h <= 659) ||
(uf == PA && h >= 660 && h <= 688) ||
(uf == AP && h == 689) ||
(uf == AM && h >= 690 && h <= 692) ||
(uf == RR && h == 693) ||
(uf == AM && h >= 694 && h <= 698) ||
(uf == AC && h == 699) ||
(uf == DF && h >= 700 && h <= 727) ||
(uf == GO && h >= 728 && h <= 729) ||
(uf == DF && h >= 730 && h <= 736) ||
(uf == GO && h >= 737 && h <= 767) ||
(uf == RO && h >= 768 && h <= 769) ||
(uf == TO && h >= 770 && h <= 779) ||
(uf == MT && h >= 780 && h <= 788) ||
(uf == MS && h >= 790 && h <= 799) ||
(uf == PR && h >= 800 && h <= 879) ||
(uf == SC && h >= 880 && h <= 899) ||
(uf == RS && h >= 900 && h <= 999) {
return true
}
}
return false
}