-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Hallberg
committed
Feb 12, 2017
1 parent
96bed9e
commit cfd1f68
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
Large diffs are not rendered by default.
Oops, something went wrong.