Browse Source

added conversion to number before finite check

pull/850/head
Stephen Hess 8 years ago
parent
commit
b04309c533
  1. 15
      controller/placeholder.js
  2. 59
      test/unit/controller/placeholder.js

15
controller/placeholder.js

@ -6,6 +6,12 @@ const logger = require('pelias-logger').get('api');
const logging = require( '../helper/logging' );
const Document = require('pelias-model').Document;
// composition of toNumber and isFinite, useful for single call to convert a value
// to a number, then checking to see if it's finite
function isFiniteNumber() {
return _.flow(_.toNumber, _.isFinite);
}
// returns true if all 4 ,-delimited (max) substrings are parseable as numbers
// '12.12,21.21,13.13,31.31' returns true
// '12.12,21.21,13.13,blah' returns false
@ -13,8 +19,7 @@ const Document = require('pelias-model').Document;
function validBoundingBox(bbox) {
return bbox.
split(',').
map(_.toNumber).
filter(_.isFinite).length === 4;
filter(isFiniteNumber).length === 4;
}
function synthesizeDocs(result) {
@ -22,8 +27,10 @@ function synthesizeDocs(result) {
doc.setName('default', result.name);
// only assign centroid if both lat and lon are finite numbers
if (_.conformsTo(result.geom, { 'lat': _.isFinite, 'lon': _.isFinite } )) {
if (_.conformsTo(result.geom, { 'lat': isFiniteNumber, 'lon': isFiniteNumber } )) {
doc.setCentroid( { lat: result.geom.lat, lon: result.geom.lon } );
} else {
console.error('what\'s up with ' + result.id);
}
// lodash conformsTo verifies that an object has a property with a certain format
@ -31,7 +38,7 @@ function synthesizeDocs(result) {
const parsedBoundingBox = result.geom.bbox.split(',').map(_.toNumber);
doc.setBoundingBox({
upperLeft: {
lat: parsedBoundingBox[3],
lat: +parsedBoundingBox[3],
lon: parsedBoundingBox[0]
},
lowerRight: {

59
test/unit/controller/placeholder.js

@ -405,6 +405,65 @@ module.exports.tests.success = (test, common) => {
});
test('results with string geom.lat/geom.lon should convert to numbers', (t) => {
const placeholder_response = [
{
id: 1,
name: 'name 1',
placetype: 'neighbourhood',
geom: {
area: 12.34,
lat: '14.141414',
lon: '41.414141'
}
}
];
const placeholderService = (req, callback) => {
t.deepEqual(req, { param1: 'param1 value' });
callback(null, placeholder_response);
};
const should_execute = (req, res) => {
return true;
};
const controller = placeholder(placeholderService, should_execute);
const req = { param1: 'param1 value' };
const res = { };
const expected_res = {
meta: {},
data: [
{
_id: '1',
_type: 'neighbourhood',
layer: 'neighbourhood',
source: 'whosonfirst',
source_id: '1',
center_point: {
lat: 14.141414,
lon: 41.414141
},
name: {
'default': 'name 1'
},
phrase: {
'default': 'name 1'
},
parent: { }
}
]
};
controller(req, res, () => {
t.deepEquals(res, expected_res);
t.end();
});
});
test('result without geom.bbox should leave bounding_box undefined', (t) => {
const placeholder_response = [
{

Loading…
Cancel
Save