-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathlocalNamingConventions.js
102 lines (89 loc) · 2.82 KB
/
localNamingConventions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const _ = require('lodash');
const field = require('../helper/fieldValue');
var flipNumberAndStreetCountries = [
/* from the ID Editor config */
/* https://github.com/openstreetmap/iD/blob/master/data/address-formats.json */
'AUT' /* Austria */,
'CHE' /* Switzerland */,
'DEU' /* Germany */,
'SVN' /* Slovenia */ ,
'POL' /* Poland */,
'AND' /* Andorra */,
'BIH' /* Bosnia and Herzegovina */,
'BEL' /* Belgium */,
'CZE' /* Czechia */,
'DNK' /* Denmark */,
'ESP' /* Spain */,
'FIN' /* Finland */,
'GRC' /* Greece */,
'HRV' /* Croatia */,
'ISL' /* Iceland */,
'ITA' /* Italy */,
'LIE' /* Liechtenstein */,
'NLD' /* Netherlands */,
'NOR' /* Norway */,
'PRT' /* Portugal */,
'SWE' /* Sweden */,
'SVK' /* Slovakia */,
'SMR' /* San Marino */,
'VAT' /* Holy See */,
'BRA' /* Brazil */,
'TWN' /* Taiwan */,
'TUR' /* Turkey */,
/* Additional country codes not provided by ID Editor config */
'ROU' /* Romania */,
'MDA' /* Moldova */,
'COL' /* Colombia */,
'HUN' /* Hungary */,
'UKR' /* Ukraine */,
'RUS' /* Russia */,
'UZB' /* Uzbekistan */,
'TJK' /* Tajikistan */,
'BLR' /* Belarus */,
'AZE' /* Azerbaijan */,
'GEO' /* Georgia */,
'KGZ' /* Kyrgyzstan */,
'KAZ' /* Kazakhstan */,
];
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;
}
function applyLocalNamingConventions(req, res, next) {
// do nothing if no result data set
if (!res || !res.data) {
return next();
}
// loop through data items and flip relevant number/street
res.data.filter(function(place){
// do nothing for records with no admin info
if (!_.has(place, 'parent.country_a')) { return false; }
// relevant for some countries
const flip = place.parent.country_a.some(country => {
return _.includes(flipNumberAndStreetCountries, country);
});
if (!flip){ return false; }
if (!_.has(place, 'address_parts.number')) { return false; }
if (!_.has(place, 'address_parts.street')) { return false; }
return true;
})
.forEach( flipNumberAndStreet );
next();
}
// flip the housenumber and street name
// eg. '101 Grolmanstraße' -> 'Grolmanstraße 101'
function flipNumberAndStreet(place) {
const number = field.getStringValue(_.get(place, 'address_parts.number'));
const street = field.getStringValue(_.get(place, 'address_parts.street'));
const name = field.getStringValue(_.get(place, 'name.default'));
// flip street name and housenumber
if (name === `${number} ${street}`) {
place.name.default = `${street} ${number}`;
}
}
module.exports = setup;