-
Notifications
You must be signed in to change notification settings - Fork 96
/
postalcodes.go
246 lines (176 loc) · 5.05 KB
/
postalcodes.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package randomdata
import (
"fmt"
"math"
"strings"
)
// Supported formats obtained from:
// * http://www.geopostcodes.com/GeoPC_Postal_codes_formats
// PostalCode yields a random postal/zip code for the given 2-letter country code.
//
// These codes are not guaranteed to refer to actually locations.
// They merely follow the correct format as far as letters and digits goes.
// Where possible, the function enforces valid ranges of letters and digits.
func PostalCode(countrycode string) string {
switch strings.ToUpper(countrycode) {
case "LS", "MG", "IS", "OM", "PG":
return Digits(3)
case "AM", "GE", "NZ", "NE", "NO", "PY", "ZA", "MZ", "SJ", "LI", "AL",
"BD", "CV", "GL":
return Digits(4)
case "DZ", "BA", "KH", "DO", "EG", "EE", "GP", "GT", "ID", "IL", "JO",
"KW", "MQ", "MX", "LK", "SD", "TR", "UA", "US", "CR", "IQ", "KV", "MY",
"MN", "ME", "PK", "SM", "MA", "UY", "EH", "ZM":
return Digits(5)
case "BY", "CN", "IN", "KZ", "KG", "NG", "RO", "RU", "SG", "TJ", "TM", "UZ", "VN":
return Digits(6)
case "CL":
return Digits(7)
case "IR":
return Digits(10)
case "FO":
return "FO " + Digits(3)
case "AF":
return BoundedDigits(2, 10, 43) + BoundedDigits(2, 1, 99)
case "AU", "AT", "BE", "BG", "CY", "DK", "ET", "GW", "HU", "LR", "MK", "PH",
"CH", "TN", "VE":
return BoundedDigits(4, 1000, 9999)
case "SV":
return "CP " + BoundedDigits(4, 1000, 9999)
case "HT":
return "HT" + Digits(4)
case "LB":
return Digits(4) + " " + Digits(4)
case "LU":
return BoundedDigits(4, 6600, 6999)
case "MD":
return "MD-" + BoundedDigits(4, 1000, 9999)
case "HR":
return "HR-" + Digits(5)
case "CU":
return "CP " + BoundedDigits(5, 10000, 99999)
case "FI":
// Last digit is usually 0 but can, in some cases, be 1 or 5.
switch privateRand.Intn(2) {
case 0:
return Digits(4) + "0"
case 1:
return Digits(4) + "1"
}
return Digits(4) + "5"
case "FR", "GF", "PF", "YT", "MC", "RE", "BL", "MF", "PM", "RS", "TH":
return BoundedDigits(5, 10000, 99999)
case "DE":
return BoundedDigits(5, 1000, 99999)
case "GR":
return BoundedDigits(3, 100, 999) + " " + Digits(2)
case "HN":
return "CM" + Digits(4)
case "IT", "VA":
return BoundedDigits(5, 10, 99999)
case "KE":
return BoundedDigits(5, 100, 99999)
case "LA":
return BoundedDigits(5, 1000, 99999)
case "MH":
return BoundedDigits(5, 96960, 96970)
case "FM":
return "FM" + BoundedDigits(5, 96941, 96944)
case "MM":
return BoundedDigits(2, 1, 14) + Digits(3)
case "NP":
return BoundedDigits(5, 10700, 56311)
case "NC":
return "98" + Digits(3)
case "PW":
return "PW96940"
case "PR":
return "PR " + Digits(5)
case "SA":
return BoundedDigits(5, 10000, 99999) + "-" + BoundedDigits(4, 1000, 9999)
case "ES":
return BoundedDigits(2, 1, 52) + BoundedDigits(3, 100, 999)
case "WF":
return "986" + Digits(2)
case "SZ":
return Letters(1) + Digits(3)
case "BM":
return Letters(2) + Digits(2)
case "AD":
return Letters(2) + Digits(3)
case "BN", "AZ", "VG", "PE":
return Letters(2) + Digits(4)
case "BB":
return Letters(2) + Digits(5)
case "EC":
return Letters(2) + Digits(6)
case "MT":
return Letters(3) + Digits(4)
case "JM":
return "JM" + Letters(3) + Digits(2)
case "AR":
return Letters(1) + Digits(4) + Letters(3)
case "CA":
return Letters(1) + Digits(1) + Letters(1) + Digits(1) + Letters(1) + Digits(1)
case "FK", "TC":
return Letters(4) + Digits(1) + Letters(2)
case "GG", "IM", "JE":
return Letters(2) + Digits(2) + Letters(2)
case "GB":
return Letters(2) + Digits(1) + " " + Digits(1) + Letters(2)
case "KY":
return Letters(2) + Digits(1) + "-" + Digits(4)
case "JP":
return Digits(3) + "-" + Digits(4)
case "LV", "SI":
return Letters(2) + "-" + Digits(4)
case "LT":
return Letters(2) + "-" + Digits(5)
case "SE", "TW":
return Digits(5)
case "MV":
return Digits(2) + "-" + Digits(2)
case "PL":
return Digits(2) + "-" + Digits(3)
case "NI":
return Digits(3) + "-" + Digits(3) + "-" + Digits(1)
case "KR":
return Digits(3) + "-" + Digits(3)
case "PT":
return Digits(4) + "-" + Digits(3)
case "NL":
return Digits(4) + Letters(2)
case "BR":
return Digits(5) + "-" + Digits(3)
}
return ""
}
// Letters generates a string of N random leters (A-Z).
func Letters(letters int) string {
list := make([]byte, letters)
for i := range list {
list[i] = byte(privateRand.Intn('Z'-'A') + 'A')
}
return string(list)
}
// Digits generates a string of N random digits, padded with zeros if necessary.
func Digits(digits int) string {
max := int(math.Pow10(digits)) - 1
num := privateRand.Intn(max)
format := fmt.Sprintf("%%0%dd", digits)
return fmt.Sprintf(format, num)
}
// BoundedDigits generates a string of N random digits, padded with zeros if necessary.
// The output is restricted to the given range.
func BoundedDigits(digits, low, high int) string {
if low > high {
low, high = high, low
}
max := int(math.Pow10(digits)) - 1
if high > max {
high = max
}
num := privateRand.Intn(high-low+1) + low
format := fmt.Sprintf("%%0%dd", digits)
return fmt.Sprintf(format, num)
}