From 79a9f0ecf7a8d03c5dd59b4009848bb470aaa849 Mon Sep 17 00:00:00 2001 From: Greenkeeper Date: Fri, 23 Sep 2016 20:29:23 +0200 Subject: [PATCH 1/4] chore(package): update mastarm to version 0.10.0 (#8) https://greenkeeper.io/ --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e626a38..142430b 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "qs": "^6.2.0" }, "devDependencies": { - "mastarm": "^0.9.2", + "mastarm": "^0.10.0", "semantic-release": "^4.3.5", "tape": "^4.6.0" } From c05040a23565e67fdc4045a27780b2cacc576288 Mon Sep 17 00:00:00 2001 From: greenkeeperio-bot Date: Fri, 18 Nov 2016 04:48:21 +0800 Subject: [PATCH 2/4] chore(package): update semantic-release to version 6.3.2 https://greenkeeper.io/ --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 142430b..4d44ff4 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ }, "devDependencies": { "mastarm": "^0.10.0", - "semantic-release": "^4.3.5", + "semantic-release": "^6.3.2", "tape": "^4.6.0" } } From 8882c29b81d818c4b1440445a8768e2e78849b94 Mon Sep 17 00:00:00 2001 From: Evan Siroky Date: Mon, 23 Jan 2017 15:35:42 -0800 Subject: [PATCH 3/4] test(jest): add tests and update lonlng to @conveyal/lonlat --- .gitignore | 3 ++ .npmignore | 3 +- .travis.yml | 6 ++- __tests__/__snapshots__/index.js.snap | 66 +++++++++++++++++++++++ __tests__/index.js | 54 +++++++++++++++++++ __tests__/mock-reverse-result.json | 71 +++++++++++++++++++++++++ __tests__/mock-search-result.json | 75 +++++++++++++++++++++++++++ index.js | 16 +++--- package.json | 16 +++--- 9 files changed, 293 insertions(+), 17 deletions(-) create mode 100644 __tests__/__snapshots__/index.js.snap create mode 100644 __tests__/index.js create mode 100644 __tests__/mock-reverse-result.json create mode 100644 __tests__/mock-search-result.json diff --git a/.gitignore b/.gitignore index e2b75d6..e5c4b28 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ lib-cov # Coverage directory used by tools like istanbul coverage +tmp # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt @@ -25,6 +26,8 @@ build/Release # Dependency directory node_modules + +# built files build/* # Optional npm cache directory diff --git a/.npmignore b/.npmignore index 496ee2c..d4eb940 100644 --- a/.npmignore +++ b/.npmignore @@ -1 +1,2 @@ -.DS_Store \ No newline at end of file +.DS_Store +tmp diff --git a/.travis.yml b/.travis.yml index 0808e65..bdcbd17 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,9 +8,13 @@ notifications: node_js: - '6' before_install: - - npm i -g npm@^3.0.0 + - npm i -g codecov before_script: - npm prune +script: + - npm run lint + - npm run cover + - codecov after_success: - npm run semantic-release branches: diff --git a/__tests__/__snapshots__/index.js.snap b/__tests__/__snapshots__/index.js.snap new file mode 100644 index 0000000..a0dd58a --- /dev/null +++ b/__tests__/__snapshots__/index.js.snap @@ -0,0 +1,66 @@ +exports[`reverse should successfully reverse geocode and format 1`] = ` +Array [ + Object { + "accuracy": "centroid", + "address": "Takoma, Takoma Park, MD, USA", + "confidence": 1, + "country": "United States", + "country_a": "USA", + "country_gid": "whosonfirst:country:85633793", + "county": "Montgomery County", + "county_gid": "whosonfirst:county:102082719", + "distance": 0, + "gid": "whosonfirst:neighbourhood:85851759", + "id": "85851759", + "label": "Takoma, Takoma Park, MD, USA", + "latlng": Object { + "lat": 38.976745, + "lon": -77.023104, + }, + "layer": "neighbourhood", + "locality": "Takoma Park", + "locality_gid": "whosonfirst:locality:85949501", + "name": "Takoma", + "neighbourhood": "Takoma", + "neighbourhood_gid": "whosonfirst:neighbourhood:85851759", + "region": "Maryland", + "region_a": "MD", + "region_gid": "whosonfirst:region:85688501", + "source": "whosonfirst", + "source_id": "85851759", + }, +] +`; + +exports[`search should successfully geocode and format 1`] = ` +Array [ + Object { + "accuracy": "centroid", + "address": "Takoma, Takoma Park, MD, USA", + "confidence": 0.965, + "country": "United States", + "country_a": "USA", + "country_gid": "whosonfirst:country:85633793", + "county": "Montgomery County", + "county_gid": "whosonfirst:county:102082719", + "gid": "whosonfirst:neighbourhood:85851759", + "id": "85851759", + "label": "Takoma, Takoma Park, MD, USA", + "latlng": Object { + "lat": 38.976745, + "lon": -77.023104, + }, + "layer": "neighbourhood", + "locality": "Takoma Park", + "locality_gid": "whosonfirst:locality:85949501", + "name": "Takoma", + "neighbourhood": "Takoma", + "neighbourhood_gid": "whosonfirst:neighbourhood:85851759", + "region": "Maryland", + "region_a": "MD", + "region_gid": "whosonfirst:region:85688501", + "source": "whosonfirst", + "source_id": "85851759", + }, +] +`; diff --git a/__tests__/index.js b/__tests__/index.js new file mode 100644 index 0000000..4ba40f4 --- /dev/null +++ b/__tests__/index.js @@ -0,0 +1,54 @@ +/* globals describe, expect, it */ + +const nock = require('nock') + +const geocoder = require('../index.js') +const mockReverseResult = require('./mock-reverse-result.json') +const mockSearchResult = require('./mock-search-result.json') +const mockKey = 'test-key' + +describe('search', () => { + const searchQuery = '123 abc st' + + it('should successfully geocode', async () => { + nock('https://search.mapzen.com/') + .get(/v1\/search/) + .reply(200, mockSearchResult) + + const result = await geocoder.search(mockKey, searchQuery) + expect(result.features[0].geometry.coordinates[0]).toEqual(-77.023104) + }) + + it('should successfully geocode and format', async () => { + nock('https://search.mapzen.com/') + .get(/v1\/search/) + .reply(200, mockSearchResult) + + const result = await geocoder.search(mockKey, searchQuery, { format: true }) + expect(result).toMatchSnapshot() + expect(result[0].address).toEqual('Takoma, Takoma Park, MD, USA') + }) +}) + +describe('reverse', () => { + const reverseQuery = { lat: 38.976745, lng: -77.023104 } + + it('should successfully reverse geocode', async () => { + nock('https://search.mapzen.com/') + .get(/v1\/reverse/) + .reply(200, mockReverseResult) + + const result = await geocoder.reverse(mockKey, reverseQuery) + expect(result.features[0].geometry.coordinates[0]).toEqual(-77.023104) + }) + + it('should successfully reverse geocode and format', async () => { + nock('https://search.mapzen.com/') + .get(/v1\/reverse/) + .reply(200, mockReverseResult) + + const result = await geocoder.reverse(mockKey, reverseQuery, { format: true }) + expect(result).toMatchSnapshot() + expect(result[0].address).toEqual('Takoma, Takoma Park, MD, USA') + }) +}) diff --git a/__tests__/mock-reverse-result.json b/__tests__/mock-reverse-result.json new file mode 100644 index 0000000..16a0969 --- /dev/null +++ b/__tests__/mock-reverse-result.json @@ -0,0 +1,71 @@ +{ + "geocoding": { + "version": "0.2", + "attribution": "https://search.mapzen.com/v1/attribution", + "query": { + "size": 10, + "private": false, + "point.lat": 38.976745, + "point.lon": -77.023104, + "boundary.circle.radius": 1, + "boundary.circle.lat": 38.976745, + "boundary.circle.lon": -77.023104, + "querySize": 20 + }, + "engine": { + "name": "Pelias", + "author": "Mapzen", + "version": "1.0" + }, + "timestamp": 1481582055662 + }, + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -77.023104, + 38.976745 + ] + }, + "properties": { + "id": "85851759", + "gid": "whosonfirst:neighbourhood:85851759", + "layer": "neighbourhood", + "source": "whosonfirst", + "source_id": "85851759", + "name": "Takoma", + "confidence": 1, + "distance": 0, + "accuracy": "centroid", + "country": "United States", + "country_gid": "whosonfirst:country:85633793", + "country_a": "USA", + "region": "Maryland", + "region_gid": "whosonfirst:region:85688501", + "region_a": "MD", + "county": "Montgomery County", + "county_gid": "whosonfirst:county:102082719", + "locality": "Takoma Park", + "locality_gid": "whosonfirst:locality:85949501", + "neighbourhood": "Takoma", + "neighbourhood_gid": "whosonfirst:neighbourhood:85851759", + "label": "Takoma, Takoma Park, MD, USA" + }, + "bbox": [ + -77.017242, + 38.973896, + -77.012062, + 38.980594 + ] + } + ], + "bbox": [ + -77.023234, + 38.973896, + -77.012062, + 38.980594 + ] +} diff --git a/__tests__/mock-search-result.json b/__tests__/mock-search-result.json new file mode 100644 index 0000000..8336e7e --- /dev/null +++ b/__tests__/mock-search-result.json @@ -0,0 +1,75 @@ +{ + "geocoding": { + "version": "0.2", + "attribution": "https://search.mapzen.com/v1/attribution", + "query": { + "text": "takoma", + "size": 10, + "sources": [ + "geonames", + "openaddresses", + "openstreetmap", + "whosonfirst" + ], + "private": false, + "boundary.circle.radius": 25, + "boundary.circle.lat": 38.8886, + "boundary.circle.lon": -77.043, + "querySize": 20 + }, + "engine": { + "name": "Pelias", + "author": "Mapzen", + "version": "1.0" + }, + "timestamp": 1479272483487 + }, + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -77.023104, + 38.976745 + ] + }, + "properties": { + "id": "85851759", + "gid": "whosonfirst:neighbourhood:85851759", + "layer": "neighbourhood", + "source": "whosonfirst", + "source_id": "85851759", + "name": "Takoma", + "confidence": 0.965, + "accuracy": "centroid", + "country": "United States", + "country_gid": "whosonfirst:country:85633793", + "country_a": "USA", + "region": "Maryland", + "region_gid": "whosonfirst:region:85688501", + "region_a": "MD", + "county": "Montgomery County", + "county_gid": "whosonfirst:county:102082719", + "locality": "Takoma Park", + "locality_gid": "whosonfirst:locality:85949501", + "neighbourhood": "Takoma", + "neighbourhood_gid": "whosonfirst:neighbourhood:85851759", + "label": "Takoma, Takoma Park, MD, USA" + }, + "bbox": [ + -77.017242, + 38.973896, + -77.012062, + 38.980594 + ] + } + ], + "bbox": [ + -77.017897, + 38.904, + -76.978642, + 39.001084 + ] +} diff --git a/index.js b/index.js index e868352..ecda6c4 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ +import lonlat from '@conveyal/lonlat' import fetch from 'isomorphic-fetch' -import lonlng from 'lonlng' import qs from 'qs' const mapzenUrl = 'https://search.mapzen.com/v1' @@ -16,7 +16,7 @@ export function search (apiKey, text, {boundary, focusLatlng, format} = {}) { } if (focusLatlng) { - const {lat, lng} = lonlng(focusLatlng) + const {lat, lng} = lonlat(focusLatlng) query['focus.point.lat'] = lat query['focus.point.lon'] = lng } @@ -24,15 +24,15 @@ export function search (apiKey, text, {boundary, focusLatlng, format} = {}) { if (boundary) { if (boundary.country) query['boundary.country'] = boundary.country if (boundary.rect) { - const min = lonlng(boundary.rect.minLatlng) - const max = lonlng(boundary.rect.maxLatlng) + const min = lonlat(boundary.rect.minLatlng) + const max = lonlat(boundary.rect.maxLatlng) query['boundary.rect.min_lat'] = min.lat query['boundary.rect.min_lon'] = min.lng query['boundary.rect.max_lat'] = max.lat query['boundary.rect.max_lon'] = max.lng } if (boundary.circle) { - const {lat, lng} = lonlng(boundary.circle.latlng) + const {lat, lng} = lonlat(boundary.circle.latlng) query['boundary.circle.lat'] = lat query['boundary.circle.lon'] = lng query['boundary.circle.radius'] = boundary.circle.radius @@ -43,7 +43,7 @@ export function search (apiKey, text, {boundary, focusLatlng, format} = {}) { } export function reverse (apiKey, latlng, {format} = {}) { - const {lng, lat} = lonlng(latlng) + const {lng, lat} = lonlat(latlng) return run(reverseUrl, { api_key: apiKey, 'point.lat': lat, @@ -59,7 +59,7 @@ function run (url, query, format) { function split ({geometry, properties}) { return Object.assign({}, properties, { - address: `${properties.label} ${properties.postalcode}`, - latlng: lonlng.fromCoordinates(geometry.coordinates) + address: `${properties.label}${properties.postalcode ? ' ' + properties.postalcode : ''}`, + latlng: lonlat.fromCoordinates(geometry.coordinates) }) } diff --git a/package.json b/package.json index 4d44ff4..a26a0b1 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,11 @@ "description": "Isomorphic Mapzen search for reuse across our libraries.", "main": "./build/index", "scripts": { - "prepublish": "mastarm prepublish index.js", - "test": "mastarm lint", - "semantic-release": "semantic-release pre && npm publish && semantic-release post" + "cover": "mastarm test --coverage --coverage-paths index.js", + "lint": "mastarm lint", + "prepublish": "mastarm prepublish index.js:build/index.js", + "semantic-release": "semantic-release pre && npm publish && semantic-release post", + "test": "mastarm test" }, "repository": { "type": "git", @@ -18,13 +20,13 @@ }, "homepage": "https://github.com/conveyal/isomorphic-mapzen-search", "dependencies": { + "@conveyal/lonlat": "^1.1.0", "isomorphic-fetch": "^2.2.1", - "lonlng": "0.1.0", "qs": "^6.2.0" }, "devDependencies": { - "mastarm": "^0.10.0", - "semantic-release": "^6.3.2", - "tape": "^4.6.0" + "mastarm": "^3.1.1", + "nock": "^9.0.2", + "semantic-release": "^6.3.2" } } From 91698b1690852ba37d143c940877ee06f7d910be Mon Sep 17 00:00:00 2001 From: Evan Siroky Date: Tue, 24 Jan 2017 21:37:02 -0800 Subject: [PATCH 4/4] refactor(lon): update all lng variables to lon --- index.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index ecda6c4..a24edcd 100644 --- a/index.js +++ b/index.js @@ -16,9 +16,9 @@ export function search (apiKey, text, {boundary, focusLatlng, format} = {}) { } if (focusLatlng) { - const {lat, lng} = lonlat(focusLatlng) + const {lat, lon} = lonlat(focusLatlng) query['focus.point.lat'] = lat - query['focus.point.lon'] = lng + query['focus.point.lon'] = lon } if (boundary) { @@ -27,14 +27,14 @@ export function search (apiKey, text, {boundary, focusLatlng, format} = {}) { const min = lonlat(boundary.rect.minLatlng) const max = lonlat(boundary.rect.maxLatlng) query['boundary.rect.min_lat'] = min.lat - query['boundary.rect.min_lon'] = min.lng + query['boundary.rect.min_lon'] = min.lon query['boundary.rect.max_lat'] = max.lat - query['boundary.rect.max_lon'] = max.lng + query['boundary.rect.max_lon'] = max.lon } if (boundary.circle) { - const {lat, lng} = lonlat(boundary.circle.latlng) + const {lat, lon} = lonlat(boundary.circle.latlng) query['boundary.circle.lat'] = lat - query['boundary.circle.lon'] = lng + query['boundary.circle.lon'] = lon query['boundary.circle.radius'] = boundary.circle.radius } } @@ -42,12 +42,12 @@ export function search (apiKey, text, {boundary, focusLatlng, format} = {}) { return run(searchUrl, query, format) } -export function reverse (apiKey, latlng, {format} = {}) { - const {lng, lat} = lonlat(latlng) +export function reverse (apiKey, latlon, {format} = {}) { + const {lon, lat} = lonlat(latlon) return run(reverseUrl, { api_key: apiKey, 'point.lat': lat, - 'point.lon': lng + 'point.lon': lon }, format) }