-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_postnet.go
120 lines (112 loc) · 2.23 KB
/
code_postnet.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package barcode
import (
"fmt"
"strconv"
"strings"
)
var planet = map[int][]int{
0: {1, 1, 2, 2, 2},
1: {2, 2, 2, 1, 1},
2: {2, 2, 1, 2, 1},
3: {2, 2, 1, 1, 2},
4: {2, 1, 2, 2, 1},
5: {2, 1, 2, 1, 2},
6: {2, 1, 1, 2, 2},
7: {1, 2, 2, 2, 1},
8: {1, 2, 2, 1, 2},
9: {1, 2, 1, 2, 2},
}
var postnet = map[int][]int{
0: {2, 2, 1, 1, 1},
1: {1, 1, 1, 2, 2},
2: {1, 1, 2, 1, 2},
3: {1, 1, 2, 2, 1},
4: {1, 2, 1, 1, 2},
5: {1, 2, 1, 2, 1},
6: {1, 2, 2, 1, 1},
7: {2, 1, 1, 1, 2},
8: {2, 1, 1, 2, 1},
9: {2, 1, 2, 1, 1},
}
/** POSTNET and PLANET barcodes.
* Used by U.S. Postal Service for automated mail sorting
* @param code {string} zip code to represent.
* Must be a string containing a zip code of the form DDDDD or DDDDD-DDDD
* @param planet {bool} if true print the PLANET barcode, otherwise print POSTNET
*/
func barcodePOSTNET(code string, isPlanet bool) *barArray {
var barlen map[int][]int
if isPlanet {
barlen = planet
} else {
barlen = postnet
}
bararray := &barArray{
Code: code,
MaxW: 0,
MaxH: 2,
BCode: []bCode{},
}
// remove dashes
code = strings.Replace(code, "-", "", -1)
// remove whitespaces
code = strings.Replace(code, " ", "", -1)
if !onlyDigits(code) {
if !onlyDigits(code) {
fmt.Printf("[BARCODE] invalid POSTNET Code: %s must be Digit e.g.: 55555-1237\n", code)
return nil
}
}
// calculate checksum
sum := 0
for i := 0; i < len(code); i++ {
intVal, _ := strconv.Atoi(string(code[i]))
sum = sum + intVal
}
chkd := sum % 10
if chkd > 0 {
chkd = 10 - chkd
}
code = code + strconv.Itoa(chkd)
bararray.BCode = append(bararray.BCode, bCode{
T: true,
W: 1,
H: 2,
P: 0,
})
bararray.BCode = append(bararray.BCode, bCode{
T: false,
W: 1,
H: 2,
P: 0,
})
bararray.MaxW += 2
for i := 0; i < len(code); i++ {
for j := 0; j < 5; j++ {
intVal, _ := strconv.Atoi(string(code[i]))
h := barlen[intVal][j]
p := 1 / h
bararray.BCode = append(bararray.BCode, bCode{
T: true,
W: 1,
H: h,
P: p,
})
bararray.BCode = append(bararray.BCode, bCode{
T: false,
W: 1,
H: 2,
P: 0,
})
bararray.MaxW += 2
}
}
bararray.BCode = append(bararray.BCode, bCode{
T: true,
W: 1,
H: 2,
P: 0,
})
bararray.MaxW++
return bararray
}