Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter zips that start with 0, and add it if necessary. #133

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ console.log(geo);
city: 'San Antonio',
ll: [ 29.4889, -98.3987 ],
metro: 641,
zip: 78218 }
zip: '78218' }
```

installation
Expand Down
10 changes: 5 additions & 5 deletions lib/geoip.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function lookup4(ip) {
geodata.region = locBuffer.toString('utf8', (locId * locRecordSize) + 2, (locId * locRecordSize) + 4).replace(/\u0000.*/, '');
geodata.ll = [locBuffer.readInt32BE((locId * locRecordSize) + 4) / 10000, locBuffer.readInt32BE((locId * locRecordSize) + 8) / 10000];
geodata.metro = locBuffer.readInt32BE((locId * locRecordSize) + 12);
geodata.zip = locBuffer.readInt32BE((locId * locRecordSize) + 16);
geodata.zip = utils.filterForLeadingZeroZipCode(locBuffer.readInt32BE((locId * locRecordSize) + 16), geodata.country);
geodata.city = locBuffer.toString('utf8', (locId * locRecordSize) + 20, (locId * locRecordSize) + locRecordSize).replace(/\u0000.*/, '');
}

Expand Down Expand Up @@ -283,15 +283,15 @@ function preload(callback) {
});
}
});

} else {
cb();
}
});
},
function () {
asyncCache.mainBuffer = new Buffer(datSize);

async.series([
function (cb2) {
fs.read(datFile, asyncCache.mainBuffer, 0, datSize, 0, cb2);
Expand Down Expand Up @@ -403,7 +403,7 @@ function preload6(callback) {
},
function () {
asyncCache6.mainBuffer = new Buffer(datSize);

async.series([
function (cb2) {
fs.read(datFile, asyncCache6.mainBuffer, 0, datSize, 0, cb2);
Expand Down Expand Up @@ -485,7 +485,7 @@ module.exports = {
return n;
},

// Start watching for data updates. The watcher waits one minute for file transfer to
// Start watching for data updates. The watcher waits one minute for file transfer to
// completete before triggering the callback.
startWatchingDataUpdate: function (callback) {
fsWatcher.makeFsWatchFilter(watcherName, geodatadir, 60*1000, function () {
Expand Down
10 changes: 9 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ utils.cmp6 = function(a, b) {
return 0;
};

utils.filterForLeadingZeroZipCode = function(zip, country) {
if (/^\d{4}$/.test(zip) && country === 'US') {
return `0${zip}`;
}

return zip.toString();
}

utils.isPrivateIP = function(addr) {
addr = addr.toString();

Expand Down Expand Up @@ -95,4 +103,4 @@ utils.ntoa6 = function(n) {
a = a.replace(/:$/, ']').replace(/:0+/g, ':').replace(/::+/, '::');

return a;
};
};
17 changes: 15 additions & 2 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
test.expect(2);

var ip = "95.23.1.184";
var expected = "Logroño";
var expected = "Arnedo";
var actual = geoip.lookup(ip);

test.ok(actual, "Should return a non-null value for " + ip);
Expand All @@ -36,7 +36,7 @@ module.exports = {

var actual = geoip.lookup("23.240.63.68");

test.equal(actual.city, "Santa Ana");
test.equal(actual.city, "Perris");
test.equal(actual.metro, 803);

test.done();
Expand All @@ -50,6 +50,19 @@ module.exports = {
test.equal(actual.city, "Granbury");
test.equal(actual.metro, 623);

test.done();
},

testLeadingZeroZipResult: function (test) {
test.expect(3);
// Boston, MA (US) are zip codes starting with a 0
var actual = geoip.lookup('76.19.11.68');
var other = geoip.lookup('66.249.79.92');

test.equal(actual.city, "Boston");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also test San Juan, PR which has zip codes 00901, 00907, 00909, 00911-00913, 00915, 00917, 00918, 00920, 00921, 00923-00927

test.equal(actual.zip, "02128");
test.equal(other.zip, "94043");

test.done();
}
};