Browse Source

adding 'aliases' - 'poi' & 'admin' for the layers param.

pull/30/head
Harish Krishna 10 years ago
parent
commit
6b29c5bbfe
  1. 25
      sanitiser/_layers.js
  2. 46
      test/unit/sanitiser/sanitise.js

25
sanitiser/_layers.js

@ -13,17 +13,38 @@ function sanitize( req ){
// which layers to query // which layers to query
if('string' === typeof params.layers && params.layers.length){ if('string' === typeof params.layers && params.layers.length){
var alias_layers = ['poi', 'admin'];
var alias_indeces = indeces.concat(alias_layers);
var layers = params.layers.split(',').map( function( layer ){ var layers = params.layers.split(',').map( function( layer ){
return layer.toLowerCase(); // lowercase inputs return layer.toLowerCase(); // lowercase inputs
}); });
for( var x=0; x<layers.length; x++ ){ for( var x=0; x<layers.length; x++ ){
if( -1 === indeces.indexOf( layers[x] ) ){ if( -1 === alias_indeces.indexOf( layers[x] ) ){
return { return {
'error': true, 'error': true,
'message': 'invalid param \'layer\': must be one or more of ' + indeces.join(',') 'message': 'invalid param \'layer\': must be one or more of ' + alias_indeces.join(',')
} }
} }
} }
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']);
// de-dup
layers = layers.filter(function(item, pos) {
return layers.indexOf(item) == pos;
});
clean.layers = layers; clean.layers = layers;
} }
else { else {

46
test/unit/sanitiser/sanitise.js

@ -153,11 +153,55 @@ module.exports.tests.sanitize_layers = function(test, common) {
}); });
test('invalid layer', function(t) { test('invalid layer', function(t) {
sanitize({ layers: 'test_layer', input: 'test', lat: 0, lon: 0 }, function( err, clean ){ sanitize({ layers: 'test_layer', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
var msg = 'invalid param \'layer\': must be one or more of geoname,osmnode,osmway,admin0,admin1,admin2,neighborhood'; var msg = 'invalid param \'layer\': must be one or more of geoname,osmnode,osmway,admin0,admin1,admin2,neighborhood,poi,admin';
t.equal(err, msg, 'invalid layer requested'); t.equal(err, msg, 'invalid layer requested');
t.end(); t.end();
}); });
}); });
test('poi (alias) layer', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
sanitize({ layers: 'poi', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, poi_layers, 'poi layers set');
t.end();
});
});
test('admin (alias) layer', function(t) {
var admin_layers = ['admin0','admin1','admin2','neighborhood'];
sanitize({ layers: 'admin', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, admin_layers, 'admin layers set');
t.end();
});
});
test('poi alias layer plus regular layers', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
var reg_layers = ['admin0', 'admin1'];
sanitize({ layers: 'poi,admin0,admin1', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, reg_layers.concat(poi_layers), 'poi + regular layers');
t.end();
});
});
test('admin alias layer plus regular layers', function(t) {
var admin_layers = ['admin0','admin1','admin2','neighborhood'];
var reg_layers = ['geoname', 'osmway'];
sanitize({ layers: 'admin,geoname,osmway', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, reg_layers.concat(admin_layers), 'admin + regular layers set');
t.end();
});
});
test('alias layer plus regular layers (no duplicates)', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
sanitize({ layers: 'poi,geoname,osmnode', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, poi_layers, 'poi layers found (no duplicates)');
t.end();
});
});
test('multiple alias layers (no duplicates)', function(t) {
var alias_layers = ['geoname','osmnode','osmway','admin0','admin1','admin2','neighborhood'];
sanitize({ layers: 'poi,admin', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, alias_layers, 'all layers found (no duplicates)');
t.end();
});
});
}; };
module.exports.tests.invalid_params = function(test, common) { module.exports.tests.invalid_params = function(test, common) {

Loading…
Cancel
Save