forked from pelias/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenamePlacenames.js
67 lines (57 loc) · 1.79 KB
/
renamePlacenames.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
const _ = require('lodash');
const PARENT_PROPS = require('../helper/placeTypes');
const ADDRESS_PROPS = [
{ name: 'unit', newName: 'unit' },
{ name: 'number', newName: 'housenumber' },
{ name: 'zip', newName: 'postalcode', transform: (value) => { return [value]; } },
{ name: 'street', newName: 'street' },
{ name: 'city', newName: 'city' }
];
function setup(optimiseThroughput) {
/*
* Rename the fields in one record
*/
function renameOneRecord(place) {
// merge the parent block into the top level object to flatten the structure
// only copy the properties if they have values
if (place.parent) {
PARENT_PROPS.forEach((prop) => {
place[prop] = place.parent[prop];
if (!optimiseThroughput) {
place[prop + '_a'] = place.parent[prop + '_a'];
place[prop + '_gid'] = place.parent[prop + '_id'];
place[prop + '_source'] = place.parent[prop + '_source'];
}
});
}
// copy the address parts after parent hierarchy in order to prefer
// the postalcode specified by the original source data
if (place.address_parts) {
ADDRESS_PROPS.forEach( (prop) => {
renameAddressProperty(place, prop);
});
}
return place;
}
function renamePlacenames(req, res, next) {
// do nothing if no result data set
if (!res || !res.data) {
return next();
}
res.data = res.data.map(renameOneRecord);
next();
}
return renamePlacenames;
}
function renameAddressProperty(place, prop) {
if (!place.address_parts.hasOwnProperty(prop.name)) {
return;
}
if (prop.hasOwnProperty('transform')) {
place[prop.newName] = prop.transform(place.address_parts[prop.name]);
}
else {
place[prop.newName] = place.address_parts[prop.name];
}
}
module.exports = setup;