Browse Source

Add type property to allow proper handling of arrays

pull/546/head
Diana Shkolnikov 9 years ago
parent
commit
269733b70c
  1. 69
      helper/geojsonify.js
  2. 15
      test/unit/helper/geojsonify.js

69
helper/geojsonify.js

@ -8,11 +8,13 @@ var GeoJSON = require('geojson'),
_ = require('lodash'); _ = require('lodash');
// Properties to be copied // Properties to be copied
// If a property is identified as a single string, assume it should be presented as a string in response
// If something other than string is desired, use the following structure: { name: 'category', type: 'array' }
var DETAILS_PROPS = [ var DETAILS_PROPS = [
'housenumber', 'housenumber',
'street', 'street',
'postalcode', 'postalcode',
'confidence', { name: 'confidence', type: 'default' },
'distance', 'distance',
'country', 'country',
'country_gid', 'country_gid',
@ -40,10 +42,10 @@ var DETAILS_PROPS = [
'borough_a', 'borough_a',
'neighbourhood', 'neighbourhood',
'neighbourhood_gid', 'neighbourhood_gid',
'bounding_box' { name: 'bounding_box', type: 'default' },
{ name: 'category', type: 'array' }
]; ];
function lookupSource(src) { function lookupSource(src) {
return src.source; return src.source;
} }
@ -217,24 +219,63 @@ function computeBBox(geojson, geojsonExtentPoints) {
function copyProperties( source, props, dst ) { function copyProperties( source, props, dst ) {
props.forEach( function ( prop ) { props.forEach( function ( prop ) {
if ( source.hasOwnProperty( prop ) ) { var property = {
name: prop.name || prop,
type: prop.type || 'string'
};
var value = null;
if ( source.hasOwnProperty( property.name ) ) {
switch (property.type) {
case 'string':
value = getStringValue(source[property.name]);
break;
case 'array':
value = getArrayValue(source[property.name]);
break;
// default behavior is to copy property exactly as is
default:
value = source[property.name];
}
// array value, take first item in array (at this time only used for admin values) if (_.isNumber(value) || (value && !_.isEmpty(value))) {
if (source[prop] instanceof Array) { dst[property.name] = value;
if (source[prop].length === 0) {
return;
} }
if (source[prop][0]) {
dst[prop] = source[prop][0];
} }
});
}
function getStringValue(property) {
// isEmpty check works for all types of values: strings, arrays, objects
if (_.isEmpty(property)) {
return '';
} }
// simple value if (_.isString(property)) {
else { return property;
dst[prop] = source[prop];
} }
// array value, take first item in array (at this time only used for admin values)
if (_.isArray(property)) {
return property[0];
} }
});
return _.toString(property);
}
function getArrayValue(property) {
// isEmpty check works for all types of values: strings, arrays, objects
if (_.isEmpty(property)) {
return '';
}
if (_.isArray(property)) {
return property;
}
return [property];
} }
/** /**

15
test/unit/helper/geojsonify.js

@ -153,7 +153,11 @@ module.exports.tests.search = function(test, common) {
'neighbourhood': 'test3', 'neighbourhood': 'test3',
'housenumber': '13', 'housenumber': '13',
'street': 'Liverpool Road', 'street': 'Liverpool Road',
'postalcode': 'N1 0RW' 'postalcode': 'N1 0RW',
'category': [
'food',
'nightlife'
]
} }
}, },
{ {
@ -208,7 +212,11 @@ module.exports.tests.search = function(test, common) {
'county': 'New York', 'county': 'New York',
'borough': 'Manhattan', 'borough': 'Manhattan',
'locality': 'New York', 'locality': 'New York',
'neighbourhood': 'Koreatown' 'neighbourhood': 'Koreatown',
'category': [
'tourism',
'transport'
]
} }
} }
] ]
@ -245,7 +253,7 @@ module.exports.tests.search = function(test, common) {
'default': 'East New York' 'default': 'East New York'
}, },
'source_id': '85816607', 'source_id': '85816607',
'category': [], 'category': ['government'],
'_id': '85816607', '_id': '85816607',
'_type': 'neighbourhood', '_type': 'neighbourhood',
'_score': 21.434, '_score': 21.434,
@ -328,6 +336,7 @@ module.exports.tests.search = function(test, common) {
'source': 'whosonfirst', 'source': 'whosonfirst',
'source_id': '85816607', 'source_id': '85816607',
'name': 'East New York', 'name': 'East New York',
'category': ['government'],
'confidence': 0.888, 'confidence': 0.888,
'country': 'United States', 'country': 'United States',
'country_gid': '85633793', 'country_gid': '85633793',

Loading…
Cancel
Save