From cd99f87c04a2593d1eb743594f9d1b26281793bd Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Tue, 12 Sep 2017 10:28:24 -0400 Subject: [PATCH] inlined and condensed doc setup --- helper/geojsonify.js | 41 +++++++++++++--------------------- test/unit/helper/geojsonify.js | 24 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/helper/geojsonify.js b/helper/geojsonify.js index 107c19c2..da7285a5 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -39,41 +39,28 @@ function geojsonifyPlaces( params, docs ){ } function geojsonifyPlace(params, place) { - const output = { + // setup the base doc + const doc = { id: place._id, gid: new Document(place.source, place.layer, place._id).getGid(), layer: place.layer, source: place.source, - source_id: place.source_id + source_id: place.source_id, + bounding_box: place.bounding_box, + lat: parseFloat(place.center_point.lat), + lng: parseFloat(place.center_point.lon) }; - if (place.hasOwnProperty('bounding_box')) { - output.bounding_box = place.bounding_box; + // assign name, logging a warning if it doesn't exist + if (_.has(place, 'name.default')) { + doc.name = place.name.default; + } else { + logger.warn(`doc ${doc.gid} does not contain name.default`); } - addName(place, output); - addDetails(params, place, output); - - // map center_point for GeoJSON to work properly - // these should not show up in the final feature properties - output.lat = parseFloat(place.center_point.lat); - output.lng = parseFloat(place.center_point.lon); + addDetails(params, place, doc); - return output; -} - -/** - * Validate and add name property - * - * @param {object} src - * @param {object} dst - */ -function addName(src, dst) { - if (_.has(src, 'name.default')) { - dst.name = src.name.default; - } else { - logger.warn(`doc ${dst.gid} does not contain name.default`); - } + return doc; } /** @@ -106,6 +93,7 @@ function addBBoxPerFeature(geojson) { */ function extractExtentPoints(geodata) { return geodata.reduce((extentPoints, place) => { + // if there's a bounding_box, use the LL/UR for the extent if (place.bounding_box) { extentPoints.push({ lng: place.bounding_box.min_lon, @@ -118,6 +106,7 @@ function extractExtentPoints(geodata) { } else { + // otherwise, use the point for the extent extentPoints.push({ lng: place.lng, lat: place.lat diff --git a/test/unit/helper/geojsonify.js b/test/unit/helper/geojsonify.js index 08571aee..8d6fc490 100644 --- a/test/unit/helper/geojsonify.js +++ b/test/unit/helper/geojsonify.js @@ -591,6 +591,30 @@ module.exports.tests.non_optimal_conditions = (test, common) => { }); + test('no points', t => { + const logger = require('pelias-mock-logger')(); + + const input = []; + + const geojsonify = proxyquire('../../../helper/geojsonify', { + './geojsonify_place_details': (params, source, dst) => { + t.fail('should not have bee called'); + }, + 'pelias-logger': logger + }); + + const actual = geojsonify({}, input); + + const expected = { + type: 'FeatureCollection', + features: [] + }; + + t.deepEquals(actual, expected); + t.end(); + + }); + }; module.exports.all = (tape, common) => {