Browse Source

Merge pull request #453 from vesameskanen/config-localnaming

Make street/number flipping configurable
pull/462/head
Julian Simioni 9 years ago
parent
commit
a578d063ee
  1. 21
      middleware/localNamingConventions.js
  2. 43
      test/unit/middleware/localNamingConventions.js

21
middleware/localNamingConventions.js

@ -1,5 +1,16 @@
var check = require('check-types');
var _ = require('lodash');
var flipNumberAndStreetCountries = ['DEU', 'FIN', 'SWE', 'NOR', 'DNK', 'ISL'];
function setup() {
var api = require('pelias-config').generate().api;
var settings = api.localization;
if (settings && settings.flipNumberAndStreetCountries) {
var countries = settings.flipNumberAndStreetCountries;
flipNumberAndStreetCountries = _.uniq(flipNumberAndStreetCountries.concat(countries));
}
return applyLocalNamingConventions;
}
@ -12,11 +23,15 @@ function applyLocalNamingConventions(req, res, next) {
// loop through data items and flip relevant number/street
res.data.filter(function(place){
// only relevant for German addresses
if( place.parent.country_a.indexOf('DEU') === -1 ){ return false; }
// relevant for some countries
var flip = place.parent.country_a.some(function(country) {
return _.includes(flipNumberAndStreetCountries, country);
});
if (!flip){ return false; }
if( !place.hasOwnProperty('address_parts') ){ return false; }
if( !place.address_parts.hasOwnProperty('number') ){ return false; }
if( !place.address_parts.hasOwnProperty('street') ){ return false; }
return true;
})
.forEach( flipNumberAndStreet );
@ -24,7 +39,7 @@ function applyLocalNamingConventions(req, res, next) {
next();
}
// DE address should have the housenumber and street name flipped
// flip the housenumber and street name
// eg. '101 Grolmanstraße' -> 'Grolmanstraße 101'
function flipNumberAndStreet(place) {
var standard = ( place.address_parts.number + ' ' + place.address_parts.street ),

43
test/unit/middleware/localNamingConventions.js

@ -1,4 +1,19 @@
var localNamingConventions = require('../../../middleware/localNamingConventions');
var proxyquire = require('proxyquire');
var customConfig = {
generate: function generate() {
return {
api : {
localization : { // expand the set of flipped countries
flipNumberAndStreetCountries : ['NLD'] // Netherlands
}
}
};
}
};
var localNamingConventions = proxyquire('../../../middleware/localNamingConventions', { 'pelias-config': customConfig });
module.exports.tests = {};
@ -42,8 +57,28 @@ module.exports.tests.flipNumberAndStreet = function(test, common) {
}
};
var nlAddress = {
'_id': 'test3',
'_type': 'test',
'name': { 'default': '117 Keizersgracht' },
'center_point': { 'lon': 4.887545, 'lat': 52.376795 },
'address_parts': {
'zip': '1015',
'number': '117',
'street': 'Keizersgracht'
},
'parent': {
'region': ['Amsterdam'],
'locality': ['Amsterdam'],
'country_a': ['NLD'],
'county': ['Noord-Holland'],
'country': ['Netherlands'],
'neighbourhood': ['Grachtengordel-West']
}
};
var req = {},
res = { data: [ ukAddress, deAddress ] },
res = { data: [ ukAddress, deAddress, nlAddress ] },
middleware = localNamingConventions();
test('flipNumberAndStreet', function(t) {
@ -57,6 +92,10 @@ module.exports.tests.flipNumberAndStreet = function(test, common) {
// eg. '101 Grolmanstraße' -> 'Grolmanstraße 101'
t.equal( res.data[1].name.default, 'Grolmanstraße 23', 'flipped name' );
// NLD address should have the housenumber and street name flipped, too
// this definition comes from pelias configuration
t.equal( res.data[2].name.default, 'Keizersgracht 117', 'flipped name' );
t.end();
});
});

Loading…
Cancel
Save