From a1efb0c6d0dbad2054a35065070d189dd8a57b88 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 14 Sep 2015 19:53:54 +0200 Subject: [PATCH 1/2] lodash-ify some array functions; remove 'yes' and 'y' from thruthy --- helper/adminFields.js | 8 +++++--- helper/geojsonify.js | 9 ++++----- sanitiser/_details.js | 6 ++++-- sanitiser/_id.js | 12 ++++-------- sanitiser/_source.js | 5 +++-- test/unit/sanitiser/reverse.js | 4 ++-- test/unit/sanitiser/search.js | 4 ++-- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/helper/adminFields.js b/helper/adminFields.js index de1dd009..275e03c1 100644 --- a/helper/adminFields.js +++ b/helper/adminFields.js @@ -1,5 +1,7 @@ -var peliasSchema = require('pelias-schema'); -var peliasLogger = require( 'pelias-logger' ).get( 'api' ); + +var _ = require('lodash'), + peliasSchema = require('pelias-schema'), + peliasLogger = require( 'pelias-logger' ).get( 'api' ); var ADMIN_FIELDS = [ 'admin0', @@ -29,7 +31,7 @@ function getAvailableAdminFields(schema, expectedFields, logger) { // check if expected fields are actually in current schema var available = expectedFields.filter(function (field) { - return (actualFields.indexOf(field) !== -1); + return _.contains( actualFields, field ); }); if (available.length === 0) { diff --git a/helper/geojsonify.js b/helper/geojsonify.js index cf1f8744..ae054dbc 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -1,9 +1,8 @@ -var GeoJSON = require('geojson'); -var extent = require('geojson-extent'); -var outputGenerator = require('./outputGenerator'); -var logger = require('pelias-logger').get('api'); - +var GeoJSON = require('geojson'), + extent = require('geojson-extent'), + outputGenerator = require('./outputGenerator'), + logger = require('pelias-logger').get('api'); // Properties to be copied when details=true var DETAILS_PROPS = [ diff --git a/sanitiser/_details.js b/sanitiser/_details.js index e17c656c..f0e2bade 100644 --- a/sanitiser/_details.js +++ b/sanitiser/_details.js @@ -1,5 +1,7 @@ -var check = require('check-types'); +var _ = require('lodash'), + check = require('check-types'); + var DEFAULT_DETAILS_BOOL = true; // validate inputs, convert types and apply defaults @@ -20,7 +22,7 @@ function sanitize( raw, clean ){ // be lenient with 'truthy' values function isTruthy(val) { if( check.string( val ) ){ - return ['true', '1', 'yes', 'y'].indexOf(val) !== -1; + return _.contains( ['true', '1'], val ); } return val === 1 || val === true; diff --git a/sanitiser/_id.js b/sanitiser/_id.js index 77d21fa8..ff6834ca 100644 --- a/sanitiser/_id.js +++ b/sanitiser/_id.js @@ -1,5 +1,6 @@ -var check = require('check-types'), +var _ = require('lodash'), + check = require('check-types'), types = require('../query/types'); var ID_DELIM = ':'; @@ -18,12 +19,7 @@ function sanitize( raw, clean ){ var messages = { errors: [], warnings: [] }; // 'raw.id' can be an array!? - var rawIds = check.array( raw.id ) ? raw.id : [ raw.id ]; - - // de-dupe ids - rawIds = rawIds.filter(function(item, pos) { - return rawIds.indexOf( item ) === pos; - }); + var rawIds = check.array( raw.id ) ? _.unique( raw.id ) : [ raw.id ]; // ensure all elements are valid non-empty strings rawIds = rawIds.filter( function( uc ){ @@ -60,7 +56,7 @@ function sanitize( raw, clean ){ messages.errors.push( errorMessage( rawId ) ); } // type text must be one of the types - if( types.indexOf( type ) === -1 ){ + if( !_.contains( types, type ) ){ messages.errors.push( errorMessage('type', type + ' is invalid. It must be one of these values - [' + types.join(', ') + ']') ); diff --git a/sanitiser/_source.js b/sanitiser/_source.js index 88d620fc..fae33d1d 100644 --- a/sanitiser/_source.js +++ b/sanitiser/_source.js @@ -1,5 +1,6 @@ -var check = require('check-types'), +var _ = require('lodash'), + check = require('check-types'), sources_map = require( '../query/sources' ); var ALL_SOURCES = Object.keys(sources_map), @@ -20,7 +21,7 @@ function sanitize( raw, clean ) { var sources = raw.source.split(','); var invalid_sources = sources.filter(function(source) { - return ALL_SOURCES.indexOf(source) === -1; + return !_.contains( ALL_SOURCES, source ); }); if( invalid_sources.length > 0 ){ diff --git a/test/unit/sanitiser/reverse.js b/test/unit/sanitiser/reverse.js index 5c2a3a9f..f0d2e3af 100644 --- a/test/unit/sanitiser/reverse.js +++ b/test/unit/sanitiser/reverse.js @@ -133,7 +133,7 @@ module.exports.tests.sanitize_details = function(test, common) { }); }); - var valid_values = [true, 'true', 1, '1', 'yes', 'y']; + var valid_values = [true, 'true', 1, '1']; valid_values.forEach(function(details) { test('valid details param ' + details, function(t) { sanitize({ 'point.lat': 0, 'point.lon': 0, details: details }, function( err, clean ){ @@ -150,7 +150,7 @@ module.exports.tests.sanitize_details = function(test, common) { }); }); - var valid_false_values = ['false', false, 0, '0', 'no', 'n']; + var valid_false_values = ['false', false, 0, '0']; valid_false_values.forEach(function(details) { test('test setting false explicitly ' + details, function(t) { sanitize({ 'point.lat': 0, 'point.lon': 0, details: details }, function( err, clean ){ diff --git a/test/unit/sanitiser/search.js b/test/unit/sanitiser/search.js index e6c39284..c0f901b8 100644 --- a/test/unit/sanitiser/search.js +++ b/test/unit/sanitiser/search.js @@ -252,7 +252,7 @@ module.exports.tests.sanitize_details = function(test, common) { }); }); - var valid_values = ['true', true, 1, '1', 'yes', 'y']; + var valid_values = ['true', true, 1, '1']; valid_values.forEach(function(details) { test('valid details param ' + details, function(t) { sanitize({ text: 'test', details: details }, function( err, clean ){ @@ -262,7 +262,7 @@ module.exports.tests.sanitize_details = function(test, common) { }); }); - var valid_false_values = ['false', false, 0, '0', 'no', 'n']; + var valid_false_values = ['false', false, 0, '0']; valid_false_values.forEach(function(details) { test('test setting false explicitly ' + details, function(t) { sanitize({ text: 'test', details: details }, function( err, clean ){ From 509a6a7401de604787147f80140215138a8e3896 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Mon, 14 Sep 2015 17:11:39 -0400 Subject: [PATCH 2/2] Remove geocoding block from properties in GeoJSON responses All the attributes that were in this block are now put directly in the GeoJSON properties object --- helper/geojsonify.js | 12 ++-- test/unit/helper/geojsonify.js | 126 +++++++++++++++------------------ 2 files changed, 62 insertions(+), 76 deletions(-) diff --git a/helper/geojsonify.js b/helper/geojsonify.js index cf1f8744..13aef255 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -107,15 +107,13 @@ function geojsonifyPlace(details, place) { return warning('No doc or center_point property'); } - var geocoding = {}; + var output = {}; - addMetaData(place, geocoding); - addDetails(details, place, geocoding); - addLabel(place, geocoding); + addMetaData(place, output); + addDetails(details, place, output); + addLabel(place, output); - var output = {}; - output.geocoding = geocoding; // 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); @@ -210,4 +208,4 @@ function warning( doc ) { } -module.exports.search = geojsonifyPlaces; \ No newline at end of file +module.exports.search = geojsonifyPlaces; diff --git a/test/unit/helper/geojsonify.js b/test/unit/helper/geojsonify.js index 141c2783..0565bf0e 100644 --- a/test/unit/helper/geojsonify.js +++ b/test/unit/helper/geojsonify.js @@ -140,25 +140,23 @@ module.exports.tests.search = function(test, common) { ] }, 'properties': { - 'geocoding': { - 'id': 'id1', - 'layer': 'type1', - 'source': 'type1', - 'label': '\'Round Midnight Jazz and Blues Bar, test3, Angel', - 'name': '\'Round Midnight Jazz and Blues Bar', - 'country_a': 'GBR', - 'country': 'United Kingdom', - 'region': 'Islington', - 'region_a': 'ISL', - 'county': 'Angel', - 'localadmin': 'test1', - 'locality': 'test2', - 'neighbourhood': 'test3', - 'category': ['food', 'nightlife'], - 'housenumber': '13', - 'street': 'Liverpool Road', - 'postalcode': 'N1 0RW' - } + 'id': 'id1', + 'layer': 'type1', + 'source': 'type1', + 'label': '\'Round Midnight Jazz and Blues Bar, test3, Angel', + 'name': '\'Round Midnight Jazz and Blues Bar', + 'country_a': 'GBR', + 'country': 'United Kingdom', + 'region': 'Islington', + 'region_a': 'ISL', + 'county': 'Angel', + 'localadmin': 'test1', + 'locality': 'test2', + 'neighbourhood': 'test3', + 'category': ['food', 'nightlife'], + 'housenumber': '13', + 'street': 'Liverpool Road', + 'postalcode': 'N1 0RW' } }, { @@ -171,21 +169,19 @@ module.exports.tests.search = function(test, common) { ] }, 'properties': { - 'geocoding': { - 'id': 'id2', - 'layer': 'type2', - 'source': 'type2', - 'label': 'Blues Cafe, test3, Smithfield', - 'name': 'Blues Cafe', - 'country_a': 'GBR', - 'country': 'United Kingdom', - 'region': 'City And County Of The City Of London', - 'region_a': 'COL', - 'county': 'Smithfield', - 'localadmin': 'test1', - 'locality': 'test2', - 'neighbourhood': 'test3' - } + 'id': 'id2', + 'layer': 'type2', + 'source': 'type2', + 'label': 'Blues Cafe, test3, Smithfield', + 'name': 'Blues Cafe', + 'country_a': 'GBR', + 'country': 'United Kingdom', + 'region': 'City And County Of The City Of London', + 'region_a': 'COL', + 'county': 'Smithfield', + 'localadmin': 'test1', + 'locality': 'test2', + 'neighbourhood': 'test3' } }, { @@ -198,22 +194,20 @@ module.exports.tests.search = function(test, common) { ] }, 'properties': { - 'geocoding': { - 'id': '34633854', - 'layer': 'venue', - 'source': 'osm', - 'label': 'Empire State Building, Manhattan, NY', - 'name': 'Empire State Building', - 'country_a': 'USA', - 'country': 'United States', - 'region': 'New York', - 'region_a': 'NY', - 'county': 'New York', - 'localadmin': 'Manhattan', - 'locality': 'New York', - 'neighbourhood': 'Koreatown', - 'category': ['tourism', 'transport'] - } + 'id': '34633854', + 'layer': 'venue', + 'source': 'osm', + 'label': 'Empire State Building, Manhattan, NY', + 'name': 'Empire State Building', + 'country_a': 'USA', + 'country': 'United States', + 'region': 'New York', + 'region_a': 'NY', + 'county': 'New York', + 'localadmin': 'Manhattan', + 'locality': 'New York', + 'neighbourhood': 'Koreatown', + 'category': ['tourism', 'transport'] } } ] @@ -249,12 +243,10 @@ module.exports.tests.search = function(test, common) { ] }, 'properties': { - 'geocoding': { - 'id': 'id1', - 'layer': 'type1', - 'source': 'type1', - 'label': '\'Round Midnight Jazz and Blues Bar, test3, Angel' - } + 'id': 'id1', + 'layer': 'type1', + 'source': 'type1', + 'label': '\'Round Midnight Jazz and Blues Bar, test3, Angel' } }, { @@ -267,12 +259,10 @@ module.exports.tests.search = function(test, common) { ] }, 'properties': { - 'geocoding': { - 'id': 'id2', - 'layer': 'type2', - 'source': 'type2', - 'label': 'Blues Cafe, test3, Smithfield' - } + 'id': 'id2', + 'layer': 'type2', + 'source': 'type2', + 'label': 'Blues Cafe, test3, Smithfield' } }, { @@ -285,12 +275,10 @@ module.exports.tests.search = function(test, common) { ] }, 'properties': { - 'geocoding': { - 'id': '34633854', - 'layer': 'venue', - 'source': 'osm', - 'label': 'Empire State Building, Manhattan, NY' - } + 'id': '34633854', + 'layer': 'venue', + 'source': 'osm', + 'label': 'Empire State Building, Manhattan, NY' } } ] @@ -322,4 +310,4 @@ module.exports.all = function (tape, common) { for( var testCase in module.exports.tests ){ module.exports.tests[testCase](test, common); } -}; \ No newline at end of file +};