From b04309c53351585859b125ea4a863a25f7c2824e Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Wed, 10 May 2017 19:52:11 -0400 Subject: [PATCH] added conversion to number before finite check --- controller/placeholder.js | 15 ++++++-- test/unit/controller/placeholder.js | 59 +++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/controller/placeholder.js b/controller/placeholder.js index 547c145b..56a4f771 100644 --- a/controller/placeholder.js +++ b/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: { diff --git a/test/unit/controller/placeholder.js b/test/unit/controller/placeholder.js index a4e0bcd4..d6b7e627 100644 --- a/test/unit/controller/placeholder.js +++ b/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 = [ {