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');
// 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 = [
'housenumber',
'street',
'postalcode',
'confidence',
{ name: 'confidence', type: 'default' },
'distance',
'country',
'country_gid',
@ -40,10 +42,10 @@ var DETAILS_PROPS = [
'borough_a',
'neighbourhood',
'neighbourhood_gid',
'bounding_box'
{ name: 'bounding_box', type: 'default' },
{ name: 'category', type: 'array' }
];
function lookupSource(src) {
return src.source;
}
@ -217,24 +219,63 @@ function computeBBox(geojson, geojsonExtentPoints) {
function copyProperties( source, props, dst ) {
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 (source[prop] instanceof Array) {
if (source[prop].length === 0) {
return;
if (_.isNumber(value) || (value && !_.isEmpty(value))) {
dst[property.name] = value;
}
if (source[prop][0]) {
dst[prop] = source[prop][0];
}
});
}
// simple value
else {
dst[prop] = source[prop];
function getStringValue(property) {
// isEmpty check works for all types of values: strings, arrays, objects
if (_.isEmpty(property)) {
return '';
}
if (_.isString(property)) {
return property;
}
});
// 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',
'housenumber': '13',
'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',
'borough': 'Manhattan',
'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'
},
'source_id': '85816607',
'category': [],
'category': ['government'],
'_id': '85816607',
'_type': 'neighbourhood',
'_score': 21.434,
@ -328,6 +336,7 @@ module.exports.tests.search = function(test, common) {
'source': 'whosonfirst',
'source_id': '85816607',
'name': 'East New York',
'category': ['government'],
'confidence': 0.888,
'country': 'United States',
'country_gid': '85633793',

Loading…
Cancel
Save