Browse Source

using alias layer names in queryMixer and expanding them in helper/layers.js (keeping things DRY)

pull/64/head
Harish Krishna 10 years ago
parent
commit
103ed5f006
  1. 25
      helper/layers.js
  2. 14
      helper/queryMixer.json
  3. 8
      query/suggest.js
  4. 24
      sanitiser/_layers.js
  5. 3
      test/unit/helper/queryMixer.js

25
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;
};

14
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"
}

8
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 {

24
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 };

3
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) {

Loading…
Cancel
Save