Skip to content

Commit d3e0e35

Browse files
author
heathdutton
committed
Add AZ, EE, MW, KR, SG
1 parent f3156f6 commit d3e0e35

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Free postal code validation for the web.
1212

1313
## Usage
1414

15-
`<script src="https://cdn.jsdelivr.net/gh/zipcodes/zipcodes.js@1.0/zipcodes.min.js"></script>`
15+
`<script src="https://cdn.jsdelivr.net/gh/zipcodes/zipcodes.js@1/zipcodes.min.js"></script>`
1616

1717
```
1818
getZipCode('US', '90210', function (result) {
@@ -70,7 +70,7 @@ Country codes should follow [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/I
7070
| <img src="https://flags.zipcodes.gdn/png/256/AU.png" height="12" width="23"> | AU | Australia |||
7171
| <img src="https://flags.zipcodes.gdn/png/256/AW.png" height="12" width="23"> | AW | Aruba |||
7272
| <img src="https://flags.zipcodes.gdn/png/256/AX.png" height="12" width="23"> | AX | sort |||
73-
| <img src="https://flags.zipcodes.gdn/png/256/AZ.png" height="12" width="23"> | AZ | Azerbaijan || |
73+
| <img src="https://flags.zipcodes.gdn/png/256/AZ.png" height="12" width="23"> | AZ | Azerbaijan || |
7474
| <img src="https://flags.zipcodes.gdn/png/256/BA.png" height="12" width="23"> | BA | Bosnia and Herzegovina |||
7575
| <img src="https://flags.zipcodes.gdn/png/256/BB.png" height="12" width="23"> | BB | Barbados |||
7676
| <img src="https://flags.zipcodes.gdn/png/256/BD.png" height="12" width="23"> | BD | Bangladesh |||
@@ -176,7 +176,7 @@ Country codes should follow [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/I
176176
| <img src="https://flags.zipcodes.gdn/png/256/KM.png" height="12" width="23"> | KM | Comoros |||
177177
| <img src="https://flags.zipcodes.gdn/png/256/KN.png" height="12" width="23"> | KN | Saint Kitts and Nevis |||
178178
| <img src="https://flags.zipcodes.gdn/png/256/KP.png" height="12" width="23"> | KP | North Korea |||
179-
| <img src="https://flags.zipcodes.gdn/png/256/KR.png" height="12" width="23"> | KR | South Korea || |
179+
| <img src="https://flags.zipcodes.gdn/png/256/KR.png" height="12" width="23"> | KR | South Korea || |
180180
| <img src="https://flags.zipcodes.gdn/png/256/KW.png" height="12" width="23"> | KW | Kuwait |||
181181
| <img src="https://flags.zipcodes.gdn/png/256/KY.png" height="12" width="23"> | KY | Cayman Islands |||
182182
| <img src="https://flags.zipcodes.gdn/png/256/KZ.png" height="12" width="23"> | KZ | Kazakhstan |||
@@ -252,7 +252,7 @@ Country codes should follow [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/I
252252
| <img src="https://flags.zipcodes.gdn/png/256/SC.png" height="12" width="23"> | SC | Seychelles |||
253253
| <img src="https://flags.zipcodes.gdn/png/256/SD.png" height="12" width="23"> | SD | Sudan |||
254254
| <img src="https://flags.zipcodes.gdn/png/256/SE.png" height="12" width="23"> | SE | Sweden |||
255-
| <img src="https://flags.zipcodes.gdn/png/256/SG.png" height="12" width="23"> | SG | Singapore || |
255+
| <img src="https://flags.zipcodes.gdn/png/256/SG.png" height="12" width="23"> | SG | Singapore || |
256256
| <img src="https://flags.zipcodes.gdn/png/256/SH.png" height="12" width="23"> | SH | Saint Helena, Ascension and Tristan da Cunha |||
257257
| <img src="https://flags.zipcodes.gdn/png/256/SI.png" height="12" width="23"> | SI | Slovenia |||
258258
| <img src="https://flags.zipcodes.gdn/png/256/SJ.png" height="12" width="23"> | SJ | Svalbard and Jan Mayen |||

update.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
error_reporting(E_ALL);
66
ini_set('display_errors', '1');
77

8-
$cwd = dirname(realpath(__FILE__));
9-
$zipfile = $cwd.'/allCountries.zip';
10-
$txtfile = $cwd.'/allCountries.txt';
8+
$cwd = dirname(realpath(__FILE__));
9+
$zipfile = $cwd.'/allCountries.zip';
10+
$txtfile = $cwd.'/allCountries.txt';
1111
$statsOnly = false;
12-
$regenKey = false;
13-
$keyArr = $regenKey ? [] : json_decode(file_get_contents($cwd.'/key.json'), true);
12+
$regenKey = false;
13+
$keyArr = $regenKey ? [] : json_decode(file_get_contents($cwd.'/key.json'), true);
1414

1515
// Download http://download.geonames.org/export/zip/allCountries.zip
1616
if (!file_exists($txtfile)) {
@@ -37,7 +37,7 @@
3737
echo "Cleaning up from previous build...\n";
3838
clean($cwd);
3939

40-
$row = 0;
40+
$row = 0;
4141
$headers = [
4242
'countryCode', // iso country code, 2 characters
4343
'postalCode', // varchar(20)
@@ -52,15 +52,15 @@
5252
'longitude', // estimated longitude (wgs84)
5353
'accuracy', // accuracy of lat/lng from 1=estimated to 6=centroid
5454
];
55-
$stats = [];
55+
$stats = [];
5656
if (($handle = fopen($txtfile, 'r')) !== false) {
5757
while (($data = fgetcsv($handle, 1000, "\t")) !== false) {
5858
$place = new stdClass();
5959
foreach ($data as $key => $value) {
60-
$keyname = $headers[$key];
60+
$keyname = $headers[$key];
6161
$place->{$keyname} = $value;
6262
}
63-
if (!empty($place->countryCode) && !empty($place->postalCode)) {
63+
if (!empty($place->countryCode) || !empty($place->postalCode)) {
6464
$countryCode = strtoupper($place->countryCode);
6565

6666
if ($statsOnly) {
@@ -71,8 +71,8 @@
7171

7272
if (!isset($stats[$countryCode])) {
7373
$stats[$countryCode] = [
74-
'count' => 1,
75-
'lowest' => $minCode,
74+
'count' => 1,
75+
'lowest' => $minCode,
7676
'highest' => $minCode,
7777
];
7878
} else {
@@ -86,14 +86,15 @@
8686
continue;
8787
}
8888

89-
$minOnly = in_array($countryCode, ['JP', 'PT']);
89+
$minCodeRounded = minPostalRounded($place->postalCode, $place->countryCode, $keyArr);
90+
$minOnly = in_array($countryCode, ['JP', 'PT']);
9091
echo 'Creating entry for '.$place->countryCode.' '.$place->postalCode.($minOnly ? ' (min only)' : '').' in index '.$minCodeRounded."\n";
9192
if (!is_dir($cwd.'/'.$countryCode)) {
9293
mkdir($cwd.'/'.$countryCode);
9394
}
9495

9596
if (!file_exists($cwd.'/'.$countryCode.'/test.jsonp')) {
96-
$test = new stdClass();
97+
$test = new stdClass();
9798
$test->success = true;
9899
file_put_contents($cwd.'/'.$countryCode.'/test.jsonp', 'zipCodesTestCallback('.json_encode($test).');');
99100
}
@@ -108,13 +109,12 @@
108109
mkdir($cwd.'/'.$countryCode.'/min');
109110
}
110111

111-
$minCodeRounded = minPostalRounded($place->postalCode, $place->countryCode, $keyArr);
112112
$codes = [];
113113
if (file_exists($cwd.'/'.$countryCode.'/min/'.$minCodeRounded.'.json')) {
114114
$codes = json_decode(file_get_contents($cwd.'/'.$countryCode.'/min/'.$minCodeRounded.'.json'), true);
115115
}
116116
$codes[$place->postalCode] = $data;
117-
$codesJSON = json_encode($codes);
117+
$codesJSON = json_encode($codes);
118118
file_put_contents(
119119
$cwd.'/'.$countryCode.'/min/'.$minCodeRounded.'.json',
120120
$codesJSON
@@ -138,9 +138,9 @@
138138
$key[$country] = [];
139139
if ($stat['count'] > 100000) {
140140
$chars = 3;
141-
} elseif($stat['count'] > 10000) {
141+
} elseif ($stat['count'] > 10000) {
142142
$chars = 2;
143-
} elseif($stat['count'] > 1000) {
143+
} elseif ($stat['count'] > 1000) {
144144
$chars = 1;
145145
} else {
146146
$chars = 0;
@@ -161,8 +161,9 @@
161161

162162
/**
163163
* Recursively delete files within a directory based on type.
164+
*
164165
* @param $dir
165-
* @param array $types
166+
* @param array $types
166167
*/
167168
function clean($dir, $types = ['json', 'jsonp'])
168169
{
@@ -200,7 +201,7 @@ function minPostalRounded($postalCode, $country, $keyArr = [])
200201
{
201202
$result = 0;
202203

203-
if ($keyArr[$country] > 1) {
204+
if (isset($keyArr[$country]) && $keyArr[$country] > 1) {
204205

205206
// Only allow uppercase.
206207
$postalCode = strtoupper($postalCode);

zipcodes.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var getZipCode = function (country, zip, callback) {
1616
AT: 2,
1717
AU: 2,
1818
AX: 0,
19+
AZ: 0,
1920
BD: 1,
2021
BE: 1,
2122
BG: 1,
@@ -31,6 +32,7 @@ var getZipCode = function (country, zip, callback) {
3132
DK: 1,
3233
DO: 0,
3334
DZ: 2,
35+
EE: 0,
3436
ES: 2,
3537
FI: 1,
3638
FO: 0,
@@ -51,6 +53,7 @@ var getZipCode = function (country, zip, callback) {
5153
IT: 2,
5254
JE: 0,
5355
JP: 3,
56+
KR: 2,
5457
LI: 0,
5558
LK: 1,
5659
LT: 2,
@@ -65,6 +68,7 @@ var getZipCode = function (country, zip, callback) {
6568
MT: 0,
6669
MX: 3,
6770
MY: 1,
71+
MW: 0,
6872
NC: 0,
6973
NL: 1,
7074
NO: 1,
@@ -79,6 +83,7 @@ var getZipCode = function (country, zip, callback) {
7983
RO: 2,
8084
RU: 2,
8185
SE: 2,
86+
SG: 3,
8287
SI: 0,
8388
SJ: 0,
8489
SK: 1,
@@ -112,10 +117,11 @@ var getZipCode = function (country, zip, callback) {
112117
IE: /^(([a-zA-Z]{2}(\s(([a-zA-Z0-9]{1})|(\d{2})))?)|([a-zA-Z]{3}))$/,
113118
IS_MG: /^[0-9]{3}$/,
114119
JP: /^\d{3}(-\d{4})?$/,
115-
KR: /^\d{3}-\d{3}$/,
120+
KR: /^\d{3}(-)?\d{3}$/,
116121
LV: /^([a-zA-Z]{2}-)?(\d{4})$/,
117122
MT: /^[a-zA-Z]{3}\s\d{2,4}$/,
118123
MV: /^\d{4,5}$/,
124+
MW: /^\d{6,7}$/,
119125
NL: /^(\d{4})\s?[a-zA-Z]{2}$/,
120126
PL: /^\d{2}(-)?\d{3}$/,
121127
PT: /^\d{4}(-)?\d{3}$/,

0 commit comments

Comments
 (0)