diff --git a/helper/layers.js b/helper/layers.js new file mode 100644 index 00000000..8efce727 --- /dev/null +++ b/helper/layers.js @@ -0,0 +1,25 @@ +module.exports = function(alias_layers) { + // make a copy of the array so, you are not modifying original ref + var layers = alias_layers.slice(0); + + // expand aliases + var expand_aliases = function(alias, layers, layer_indeces) { + var alias_index = layers.indexOf(alias); + if (alias_index !== -1 ) { + layers.splice(alias_index, 1); + layers = layers.concat(layer_indeces); + } + return layers; + }; + + layers = expand_aliases('poi', layers, ['geoname','osmnode','osmway']); + layers = expand_aliases('admin', layers, ['admin0','admin1','admin2','neighborhood']); + layers = expand_aliases('address', layers, ['osmaddress','openaddresses']); + + // de-dupe + layers = layers.filter(function(item, pos) { + return layers.indexOf(item) === pos; + }); + + return layers; +}; diff --git a/helper/queryMixer.json b/helper/queryMixer.json index c22670dd..4e2eb696 100644 --- a/helper/queryMixer.json +++ b/helper/queryMixer.json @@ -1,37 +1,37 @@ { "suggest": [ { - "layers": ["geoname","osmnode","osmway","admin0","admin1","admin2","neighborhood"], + "layers": ["poi", "admin"], "precision": [5, 3, 1] }, { - "layers": ["admin0","admin1","admin2","neighborhood"], + "layers": ["admin"], "precision": [] }, { - "layers": ["geoname","osmnode","osmway","admin0","admin1","admin2","neighborhood"], + "layers": ["poi", "admin"], "precision": [3], "fuzzy": "AUTO" } ], "suggest_nearby": [ { - "layers": ["geoname","osmnode","osmway","admin0","admin1","admin2","neighborhood"], + "layers": ["poi", "admin"], "precision": [] }, { - "layers": ["geoname","osmnode","osmway","admin0","admin1","admin2","neighborhood"], + "layers": ["poi", "admin"], "precision": [], "fuzzy": "AUTO" } ], "coarse": [ { - "layers": ["admin0","admin1","admin2","neighborhood"], + "layers": ["admin"], "precision": [5, 3, 1] }, { - "layers": ["admin0","admin1","admin2","neighborhood"], + "layers": ["admin"], "precision": [3], "fuzzy": "AUTO" } diff --git a/query/suggest.js b/query/suggest.js index 4e9d30b6..9d81da05 100644 --- a/query/suggest.js +++ b/query/suggest.js @@ -1,5 +1,6 @@ -var logger = require('../src/logger'); +var logger = require('../src/logger'), + get_layers = require('../helper/layers'); // Build pelias suggest query function generate( params, query_mixer, fuzziness ){ @@ -49,12 +50,13 @@ function generate( params, query_mixer, fuzziness ){ var cmd = new CmdGenerator(params); if (query_mixer && query_mixer.length) { query_mixer.forEach(function(item, index){ + var expanded_layers = get_layers(item.layers); if (item.precision && Array.isArray( item.precision ) && item.precision.length ) { item.precision.forEach(function(precision) { - cmd.add_suggester(index, precision, item.layers, item.fuzzy); + cmd.add_suggester(index, precision, expanded_layers, item.fuzzy); }); } else { - cmd.add_suggester(index, undefined, item.layers, item.fuzzy); + cmd.add_suggester(index, undefined, expanded_layers, item.fuzzy); } }); } else { diff --git a/sanitiser/_layers.js b/sanitiser/_layers.js index 96739146..87fd6b07 100644 --- a/sanitiser/_layers.js +++ b/sanitiser/_layers.js @@ -1,6 +1,7 @@ var isObject = require('is-object'), - indeces = require('../query/indeces'); + indeces = require('../query/indeces'), + get_layers = require('../helper/layers'); // validate inputs, convert types and apply defaults function sanitize( req ){ @@ -38,27 +39,8 @@ function sanitize( req ){ } } - // expand aliases - var expand_aliases = function(alias, layers, layer_indeces) { - var alias_index = layers.indexOf(alias); - if (alias_index !== -1 ) { - layers.splice(alias_index, 1); - layers = layers.concat(layer_indeces); - } - return layers; - }; - - layers = expand_aliases('poi', layers, ['geoname','osmnode','osmway']); - layers = expand_aliases('admin', layers, ['admin0','admin1','admin2','neighborhood']); - layers = expand_aliases('address', layers, ['osmaddress','openaddresses']); - - // de-dupe - layers = layers.filter(function(item, pos) { - return layers.indexOf(item) === pos; - }); - // pass validated params to next middleware - clean.layers = layers; + clean.layers = get_layers(layers); req.clean = clean; return { 'error': false }; diff --git a/test/unit/helper/queryMixer.js b/test/unit/helper/queryMixer.js index 8b8ac3ad..7e202ef4 100644 --- a/test/unit/helper/queryMixer.js +++ b/test/unit/helper/queryMixer.js @@ -1,6 +1,7 @@ var query_mixer = require('../../../helper/queryMixer.json'); var indeces = require('../../../query/indeces'); +var alias_layers = ['poi', 'admin', 'address']; module.exports.tests = {}; @@ -16,7 +17,7 @@ module.exports.tests.interface = function(test, common) { module.exports.tests.valid = function(test, common) { var valid_keys = ['layers', 'precision', 'fuzzy']; var valid_fuzzy_vals = ['AUTO', 0, 1, 2]; - var valid_layer_vals = indeces; + var valid_layer_vals = indeces.concat(alias_layers); var isValidPrecision = function(t, precisionArr) { precisionArr.forEach(function(precision) {