Browse Source

fix postalcodes not showing up in results

pull/796/head
Diana Shkolnikov 8 years ago
parent
commit
6dd4cc9bd8
  1. 4
      middleware/normalizeParentIds.js
  2. 45
      middleware/renamePlacenames.js
  3. 3
      test/unit/middleware/renamePlacenames.js

4
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();
}

45
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;

3
test/unit/middleware/renamePlacenames.js

@ -0,0 +1,3 @@
/**
* Created by diana on 2/8/17.
*/
Loading…
Cancel
Save