Skip to content

Commit

Permalink
Initial release.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Hallberg committed Feb 12, 2017
1 parent 96bed9e commit cfd1f68
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
52 changes: 52 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function getPath(zip) {
return zip.split('').map((x) => parseInt(x));
}

let zipTree = null;
function getByZipCode(zip) {
if (zipTree === null) {
zipTree = require('./zip-tree.json');
}
const p = getPath(zip + '');
return zipTree[p[0]] && zipTree[p[0]][p[1]] && zipTree[p[0]][p[1]][p[2]] && zipTree[p[0]][p[1]][p[2]][p[3]] && zipTree[p[0]][p[1]][p[2]][p[3]][p[4]]
? zipTree[p[0]][p[1]][p[2]][p[3]][p[4]]
: null;
}

let locTree = null;
function distance(lat, long, op) {
return Math.pow(lat - op.lat, 2) + Math.pow(long - op.long, 2);
}
function getByLocation(lat, long) {
if (locTree === null) {
locTree = require('./loc-tree.json');
}
let latPath = Math.floor(Math.abs(lat) * 10);
let longPath = Math.floor(Math.abs(long) * 10);
if (longPath < 1000) {
longPath = '0' + longPath;
}
const p = getPath(latPath + '' + longPath);
let curr = locTree.points;
for (let i = 0; i < p.length; i++) {
if (!curr[p[i]]) {
return null;
}
curr = curr[p[i]];
}
let d = distance(lat, long, locTree.index[curr[0]]);
let index = 0;
for (let i = 1; i < curr.length; i++) {
let nd = distance(lat, long, locTree.index[curr[i]]);
if (nd < d) {
d = nd;
index = i;
}
}
return locTree.index[curr[index]];
}

module.exports = {
getByLocation: getByLocation,
getByZipCode: getByZipCode
};
1 change: 1 addition & 0 deletions loc-tree.json

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "zips",
"version": "1.0.0",
"description": "Light, fast, tree-based way to get cities by zipcode and location.",
"main": "index.js",
"scripts": {
"test": "node test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/crhallberg/zips.git"
},
"keywords": [
"zipcodes",
"zip",
"code",
"geo",
"zipcode",
"postal",
"code"
],
"author": "Chris Hallberg",
"license": "MIT",
"bugs": {
"url": "https://github.com/crhallberg/zips/issues"
},
"homepage": "https://github.com/crhallberg/zips#readme"
}
35 changes: 35 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function toMillis(start) {
return Math.round(process.hrtime(start)[1] / 1e3) / 1e3;
}

const library = require('./index');
// Zip code
let start = process.hrtime();
library.getByZipCode('08848');
console.log('by zip - cold', toMillis(start), 'ms');
let avgStart = process.hrtime();
for (let i = 0; i < 5; i++) {
start = process.hrtime();
library.getByZipCode('19085');
console.log('by zip - warm', toMillis(start), 'ms');
start = process.hrtime();
library.getByZipCode('19026');
console.log('by zip - warm', toMillis(start), 'ms');
}
console.log('by zip warm avg', toMillis(avgStart) / 10, 'ms');
console.log();

// Location
start = process.hrtime();
library.getByLocation(40.5929, -75.1025);
console.log('by loc - cold', toMillis(start), 'ms');
avgStart = process.hrtime();
for (let i = 0; i < 5; i++) {
start = process.hrtime();
library.getByLocation(40.0399, -75.3459);
console.log('by loc - warm', toMillis(start), 'ms');
start = process.hrtime();
library.getByLocation(39.9503, -75.304);
console.log('by loc - warm', toMillis(start), 'ms');
}
console.log('by loc warm avg', toMillis(avgStart) / 10, 'ms');
1 change: 1 addition & 0 deletions zip-tree.json

Large diffs are not rendered by default.

0 comments on commit cfd1f68

Please sign in to comment.