From 5948013a4e73a9084b6ef557c15c5a138671f0c6 Mon Sep 17 00:00:00 2001 From: Chris Hallberg Date: Mon, 12 Mar 2018 19:04:42 -0400 Subject: [PATCH 1/5] Add Prettier config --- .prettierrc.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .prettierrc.json diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..553dbe8 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,5 @@ +{ + "printWidth": 160, + "tabWidth": 4, + "trailingComma": "es5" +} From 7bb34093893f17e634b923cdce571085e5f0dcd1 Mon Sep 17 00:00:00 2001 From: PrettierCI Date: Mon, 12 Mar 2018 23:04:45 +0000 Subject: [PATCH 2/5] Sync with Prettier --- index.js | 125 +++++++++++++++++++++++++++---------------------------- 1 file changed, 61 insertions(+), 64 deletions(-) diff --git a/index.js b/index.js index 8ac8a98..0d5ab62 100644 --- a/index.js +++ b/index.js @@ -1,78 +1,75 @@ let zipTree = null; function getByZipCode(_zip) { - // Cast to string - let zip = String(_zip); - // Validate - // - Check zip length - if (zip.length < 5) { - zip = '00000'.substr(zip.length) + zip; - } else if (zip.length > 5) { - return null; - } - // - Reject non-numeric - if (zip.match(/\D/)) { - return null; - } - // Load data if not loaded - if (zipTree === null) { - zipTree = require('./data/zip-tree.json'); - } - const p = zip.split('').map((x) => parseInt(x, 10)); - const place = 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; - // Octal check - if (place === null && typeof _zip !== 'string' && _zip < 0o7777) { - return getByZipCode(_zip.toString(8)); - } - return place; + // Cast to string + let zip = String(_zip); + // Validate + // - Check zip length + if (zip.length < 5) { + zip = "00000".substr(zip.length) + zip; + } else if (zip.length > 5) { + return null; + } + // - Reject non-numeric + if (zip.match(/\D/)) { + return null; + } + // Load data if not loaded + if (zipTree === null) { + zipTree = require("./data/zip-tree.json"); + } + const p = zip.split("").map(x => parseInt(x, 10)); + const place = + 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; + // Octal check + if (place === null && typeof _zip !== "string" && _zip < 0o7777) { + return getByZipCode(_zip.toString(8)); + } + return place; } let locTree = null; function distance(lat, long, op) { - return Math.sqrt(Math.pow(lat - op.lat, 2) + Math.pow(long - op.long, 2)); + return Math.sqrt(Math.pow(lat - op.lat, 2) + Math.pow(long - op.long, 2)); } function getByLocation(lat, long, count) { - // Validate - if ( - typeof lat === 'undefined' || isNaN(parseInt(lat, 10)) - || typeof long === 'undefined' || isNaN(parseInt(long, 10)) - ) { - return null; - } - if (locTree === null) { - locTree = require('./data/loc-tree.json'); - } - const latIndex = Math.round(Math.abs(parseFloat(lat, 10))); - const longIndex = Math.round(Math.abs(parseFloat(long, 10))); - if (!locTree.zones[latIndex] || !locTree.zones[latIndex][longIndex]) { - return null; - } - const zone = locTree.zones[latIndex][longIndex]; - // Return 1 - if (!count || count === 1) { - let d = distance(lat, long, locTree.index[zone[0]]); - let index = 0; - for (let i = 1; i < zone.length; i += 1) { - const nd = distance(lat, long, locTree.index[zone[i]]); - if (nd < d) { - d = nd; - index = i; - } + // Validate + if (typeof lat === "undefined" || isNaN(parseInt(lat, 10)) || typeof long === "undefined" || isNaN(parseInt(long, 10))) { + return null; + } + if (locTree === null) { + locTree = require("./data/loc-tree.json"); + } + const latIndex = Math.round(Math.abs(parseFloat(lat, 10))); + const longIndex = Math.round(Math.abs(parseFloat(long, 10))); + if (!locTree.zones[latIndex] || !locTree.zones[latIndex][longIndex]) { + return null; + } + const zone = locTree.zones[latIndex][longIndex]; + // Return 1 + if (!count || count === 1) { + let d = distance(lat, long, locTree.index[zone[0]]); + let index = 0; + for (let i = 1; i < zone.length; i += 1) { + const nd = distance(lat, long, locTree.index[zone[i]]); + if (nd < d) { + d = nd; + index = i; + } + } + return locTree.index[zone[index]]; } - return locTree.index[zone[index]]; - } // Return multiple - const sortzone = zone.map((index) => { - const place = locTree.index[index]; - place.distance = distance(lat, long, place); - return place; - }); - return sortzone.sort((a, b) => a.distance - b.distance).slice(0, count); + const sortzone = zone.map(index => { + const place = locTree.index[index]; + place.distance = distance(lat, long, place); + return place; + }); + return sortzone.sort((a, b) => a.distance - b.distance).slice(0, count); } module.exports = { - getByLocation, - getByZipCode, + getByLocation, + getByZipCode, }; From 9b483b20c088a43651cace0ae0aeb8a56fe6c622 Mon Sep 17 00:00:00 2001 From: Chris Hallberg Date: Thu, 15 Mar 2018 11:55:45 -0400 Subject: [PATCH 3/5] Prettier: Edit configs. Format. --- .prettierrc.json | 1 - index.js | 13 +++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/.prettierrc.json b/.prettierrc.json index 553dbe8..d057b21 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -1,5 +1,4 @@ { - "printWidth": 160, "tabWidth": 4, "trailingComma": "es5" } diff --git a/index.js b/index.js index 0d5ab62..171784d 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,11 @@ function getByZipCode(_zip) { } const p = zip.split("").map(x => parseInt(x, 10)); const place = - 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]] && + 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; // Octal check @@ -35,7 +39,12 @@ function distance(lat, long, op) { } function getByLocation(lat, long, count) { // Validate - if (typeof lat === "undefined" || isNaN(parseInt(lat, 10)) || typeof long === "undefined" || isNaN(parseInt(long, 10))) { + if ( + typeof lat === "undefined" || + isNaN(parseInt(lat, 10)) || + typeof long === "undefined" || + isNaN(parseInt(long, 10)) + ) { return null; } if (locTree === null) { From e01b3fb5d13a7f4c3e6449eb7c10b1d602537a48 Mon Sep 17 00:00:00 2001 From: Chris Hallberg Date: Thu, 15 Mar 2018 11:57:09 -0400 Subject: [PATCH 4/5] Remove Codacy badge. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ac9d116..ef3c5e4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# zips [![Travis status](https://travis-ci.org/crhallberg/zips.svg?branch=master)](https://travis-ci.org/crhallberg/zips) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/405b20a50b0d4918ac7ee50f3ab1d9cb)](https://www.codacy.com/app/crhallberg/zips?utm_source=github.com&utm_medium=referral&utm_content=crhallberg/zips&utm_campaign=Badge_Grade) +# zips [![Travis status](https://travis-ci.org/crhallberg/zips.svg?branch=master)](https://travis-ci.org/crhallberg/zips) Light, fast, tree-based way to get cities by zipcode and location. ```bash From 1a59cd1500000dcf218714e1f2f7eef98dc418f4 Mon Sep 17 00:00:00 2001 From: Chris Hallberg Date: Thu, 15 Mar 2018 12:01:04 -0400 Subject: [PATCH 5/5] Prettier: README.md --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ef3c5e4..b6bb174 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # zips [![Travis status](https://travis-ci.org/crhallberg/zips.svg?branch=master)](https://travis-ci.org/crhallberg/zips) + Light, fast, tree-based way to get cities by zipcode and location. ```bash @@ -6,7 +7,7 @@ npm install zips ``` ```javascript -const zips = require('zips'); +const zips = require("zips"); ``` U.S. postal codes only. For now. @@ -26,8 +27,9 @@ Returns the closest place object to the specified coordinates. ### Why? I saw a lot of zip database modules on npm, but I was displeased with three aspects - - Old data (zips uses 2010 US Census data from [geonames.org](http://www.geonames.org/), the most up-to-date I could find). - - Huge, raw data (zips' data is pre-processed so it can just load and go). - - List searching (zips organizes its data into trees instead of going down a list until a match is found or a large hash table). + +* Old data (zips uses 2010 US Census data from [geonames.org](http://www.geonames.org/), the most up-to-date I could find). +* Huge, raw data (zips' data is pre-processed so it can just load and go). +* List searching (zips organizes its data into trees instead of going down a list until a match is found or a large hash table). I hope you're happy too.