diff --git a/middleware/normalizeParentIds.js b/middleware/normalizeParentIds.js index e0e499e2..ae22a3c6 100644 --- a/middleware/normalizeParentIds.js +++ b/middleware/normalizeParentIds.js @@ -49,6 +49,10 @@ function normalizeParentIds(place) { * @return {string} */ function makeNewId(placeType, id) { + if (!id) { + return; + } + var doc = new Document('whosonfirst', placeType, id); return doc.getGid(); } diff --git a/middleware/renamePlacenames.js b/middleware/renamePlacenames.js index d89a43ad..9f63cb6e 100644 --- a/middleware/renamePlacenames.js +++ b/middleware/renamePlacenames.js @@ -1,12 +1,14 @@ -var _ = require('lodash'); +'use strict'; -var PARENT_PROPS = require('../helper/placeTypes'); +const _ = require('lodash'); -var ADDRESS_PROPS = { - 'number': 'housenumber', - 'zip': 'postalcode', - 'street': 'street' -}; +const PARENT_PROPS = require('../helper/placeTypes'); + +const ADDRESS_PROPS = [ + { name: 'number', newName: 'housenumber' }, + { name: 'zip', newName: 'postalcode', transform: (value) => { return [value]; } }, + { name: 'street', newName: 'street' } +]; function setup() { @@ -28,22 +30,39 @@ function renamePlacenames(req, res, next) { * Rename the fields in one record */ function renameOneRecord(place) { - if (place.address_parts) { - Object.keys(ADDRESS_PROPS).forEach(function (prop) { - place[ADDRESS_PROPS[prop]] = place.address_parts[prop]; - }); - } // 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(function (prop) { + PARENT_PROPS.forEach( (prop) => { place[prop] = place.parent[prop]; place[prop + '_a'] = place.parent[prop + '_a']; place[prop + '_gid'] = place.parent[prop + '_id']; }); } + // 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 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; diff --git a/test/unit/middleware/renamePlacenames.js b/test/unit/middleware/renamePlacenames.js new file mode 100644 index 00000000..15f296d2 --- /dev/null +++ b/test/unit/middleware/renamePlacenames.js @@ -0,0 +1,3 @@ +/** + * Created by diana on 2/8/17. + */