Skip to content

Commit 07c3707

Browse files
committed
Updated coordinate regex, added unit tests
1 parent c464c6c commit 07c3707

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

lib/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,9 @@ MapboxGeocoder.prototype = {
569569

570570
_requestType: function(options, search) {
571571
var type;
572-
const reverseGeocodeCoordRgx = /^[ ]*(-?\d{1,3}\.?\d{0,16})[, ]+(-?\d{1,3}\.?\d{0,16})[ ]*$/;
573572
if (options.localGeocoderOnly) {
574573
type = GEOCODE_REQUEST_TYPE.LOCAL;
575-
} else if (options.reverseGeocode && reverseGeocodeCoordRgx.test(search)) {
574+
} else if (options.reverseGeocode && utils.REVERSE_GEOCODE_COORD_RGX.test(search)) {
576575
type = GEOCODE_REQUEST_TYPE.REVERSE;
577576
} else {
578577
type = GEOCODE_REQUEST_TYPE.FORWARD;
@@ -649,7 +648,7 @@ MapboxGeocoder.prototype = {
649648
case GEOCODE_REQUEST_TYPE.FORWARD: {
650649
// Ensure that any reverse geocoding looking request is cleaned up
651650
// to be processed as only a forward geocoding request by the server.
652-
const reverseGeocodeCoordRgx = /^[ ]*(-?\d{1,3}\.?\d{0,16})[, ]+(-?\d{1,3}\.?\d{0,16})*[ ]*$/;
651+
const reverseGeocodeCoordRgx = /^[ ]*(-?\d{1,3}(\.\d{0,256})?)[, ]+(-?\d{1,3}(\.\d{0,256})?)*[ ]*$/;
653652
if (reverseGeocodeCoordRgx.test(search)) {
654653
search = search.replace(/,/g, ' ');
655654
}

lib/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ function getAddressInfo(feature) {
6060
return addrInfo;
6161
}
6262

63+
const REVERSE_GEOCODE_COORD_RGX = /^[ ]*(-?\d{1,3}(\.\d{0,256})?)[, ]+(-?\d{1,3}(\.\d{0,256})?)[ ]*$/;
64+
6365
module.exports = {
6466
transformFeatureToGeolocationText: transformFeatureToGeolocationText,
6567
getAddressInfo: getAddressInfo,
68+
REVERSE_GEOCODE_COORD_RGX: REVERSE_GEOCODE_COORD_RGX,
6669
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"scripts": {
99
"start": "budo debug/index.js --dir debug --live -- -t brfs ",
1010
"prepublish": "NODE_ENV=production && mkdir -p dist && browserify lib/index.js --transform [ babelify --global ] --standalone MapboxGeocoder | uglifyjs -c -m > dist/mapbox-gl-geocoder.min.js && cp lib/mapbox-gl-geocoder.css dist/",
11-
"test": "browserify -t envify test/index.js test/events.test.js | smokestack -b firefox | tap-status | tap-color",
11+
"test": "browserify -t envify test/index.js test/events.test.js test/utils.test.js | smokestack -b firefox | tap-status | tap-color",
1212
"docs": "documentation build lib/index.js --format=md > API.md",
1313
"pretest": "npm run lint",
1414
"lint": "eslint lib test",

test/utils.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var test = require('tape');
2+
var utils = require('../lib/utils');
3+
4+
test('REVERSE_GEOCODE_COORD_RGX', function (t) {
5+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('12, 34'), 'Reverse: "12, 34"');
6+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('1,2'), 'Reverse: "1,2"');
7+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('12.123, 34.345'), 'Reverse: "12.123, 34.345"');
8+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('12, 34.345'), 'Reverse: "12, 34.345"');
9+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('12., 34.'), 'Reverse: "12., 34."');
10+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('122, 41'), 'Reverse: "122, 41"');
11+
t.ok(utils.REVERSE_GEOCODE_COORD_RGX.test('12, 123'), 'Reverse: "12, 123"');
12+
t.notOk(utils.REVERSE_GEOCODE_COORD_RGX.test('1234, 4568'), 'Forward: "1234, 4568"');
13+
t.notOk(utils.REVERSE_GEOCODE_COORD_RGX.test('123 Main'), 'Forward: "123 Main"');
14+
})

0 commit comments

Comments
 (0)