Browse Source

Whitespace

pull/229/head
Julian Simioni 9 years ago
parent
commit
fb0cf514e9
  1. 1
      controller/search.js
  2. 2
      helper/layers.js
  3. 18
      helper/query_parser.js
  4. 10
      sanitiser/_id.js
  5. 11
      sanitiser/_input.js
  6. 3
      sanitiser/_layers.js
  7. 32
      test/unit/helper/query_parser.js

1
controller/search.js

@ -1,4 +1,3 @@
var service = { search: require('../service/search') }; var service = { search: require('../service/search') };
function setup( backend, query ){ function setup( backend, query ){

2
helper/layers.js

@ -1,6 +1,6 @@
module.exports = function(alias_layers) { module.exports = function(alias_layers) {
// make a copy of the array so, you are not modifying original ref // make a copy of the array so, you are not modifying original ref
var layers = alias_layers.slice(0); var layers = alias_layers.slice(0);
// expand aliases // expand aliases
var expand_aliases = function(alias, layers, layer_indeces) { var expand_aliases = function(alias, layers, layer_indeces) {

18
helper/query_parser.js

@ -5,7 +5,7 @@ var get_layers = require('../helper/layers');
var delim = ','; var delim = ',';
module.exports = function(query) { module.exports = function(query) {
var tokenized = query.split(/[ ,]+/); var tokenized = query.split(/[ ,]+/);
var hasNumber = /\d/.test(query); var hasNumber = /\d/.test(query);
@ -17,11 +17,11 @@ module.exports = function(query) {
if ( delimIndex !== -1 ) { if ( delimIndex !== -1 ) {
address.name = query.substring(0, delimIndex); address.name = query.substring(0, delimIndex);
address.admin_parts = query.substring(delimIndex + 1).trim(); address.admin_parts = query.substring(delimIndex + 1).trim();
} }
return address; return address;
}; };
var getTargetLayersWhenAddressParsingIsNotNecessary = function(query) { var getTargetLayersWhenAddressParsingIsNotNecessary = function(query) {
var address = {}; var address = {};
// set target_layer if input length <= 3 characters // set target_layer if input length <= 3 characters
@ -32,7 +32,7 @@ module.exports = function(query) {
// no need to hit address layers if there's only one (or two) token(s) // no need to hit address layers if there's only one (or two) token(s)
address.target_layer = get_layers(['admin', 'poi']); address.target_layer = get_layers(['admin', 'poi']);
} }
return address.target_layer ? address : null; return address.target_layer ? address : null;
}; };
@ -49,10 +49,10 @@ module.exports = function(query) {
var addressWithAdminParts = getAdminPartsBySplittingOnDelim(query); var addressWithAdminParts = getAdminPartsBySplittingOnDelim(query);
var addressWithTargetLayers= getTargetLayersWhenAddressParsingIsNotNecessary(query); var addressWithTargetLayers= getTargetLayersWhenAddressParsingIsNotNecessary(query);
var addressWithAddressParts= !addressWithTargetLayers ? getAddressParts(query) : {}; var addressWithAddressParts= !addressWithTargetLayers ? getAddressParts(query) : {};
var parsedAddress = extend(addressWithAdminParts, var parsedAddress = extend(addressWithAdminParts,
addressWithTargetLayers, addressWithTargetLayers,
addressWithAddressParts); addressWithAddressParts);
var address_parts = [ 'name', var address_parts = [ 'name',
@ -69,7 +69,7 @@ module.exports = function(query) {
var parsed_input = {}; var parsed_input = {};
address_parts.forEach(function(part){ address_parts.forEach(function(part){
if (parsedAddress[part]) { if (parsedAddress[part]) {
parsed_input[part] = parsedAddress[part]; parsed_input[part] = parsedAddress[part];
} }
@ -90,4 +90,4 @@ module.exports = function(query) {
// regions: parsedAddress.regions, // regions: parsedAddress.regions,
// admin_parts: parsedAddress.admin_parts, // admin_parts: parsedAddress.admin_parts,
// target_layer: parsedAddress.target_layer // target_layer: parsedAddress.target_layer
// } // }

10
sanitiser/_id.js

@ -5,7 +5,6 @@ var isObject = require('is-object');
// so, both type and id are required fields. // so, both type and id are required fields.
function sanitize( req ){ function sanitize( req ){
req.clean = req.clean || {}; req.clean = req.clean || {};
var params = req.query; var params = req.query;
var indeces = require('../query/indeces'); var indeces = require('../query/indeces');
@ -30,7 +29,7 @@ function sanitize( req ){
if( params && params.id && params.id.length ){ if( params && params.id && params.id.length ){
req.clean.ids = []; req.clean.ids = [];
params.ids = Array.isArray(params.id) ? params.id : [params.id]; params.ids = Array.isArray(params.id) ? params.id : [params.id];
// de-dupe // de-dupe
params.ids = params.ids.filter(function(item, pos) { params.ids = params.ids.filter(function(item, pos) {
return params.ids.indexOf(item) === pos; return params.ids.indexOf(item) === pos;
@ -38,7 +37,7 @@ function sanitize( req ){
for (var i=0; i<params.ids.length; i++) { for (var i=0; i<params.ids.length; i++) {
var thisparam = params.ids[i]; var thisparam = params.ids[i];
// basic format/ presence of ':' // basic format/ presence of ':'
if(thisparam.indexOf(delim) === -1) { if(thisparam.indexOf(delim) === -1) {
return errormessage(null, 'invalid: must be of the format type:id for ex: \'geoname:4163334\''); return errormessage(null, 'invalid: must be of the format type:id for ex: \'geoname:4163334\'');
@ -66,10 +65,9 @@ function sanitize( req ){
}); });
} }
} }
return { 'error': false };
return { 'error': false };
} }
// export function // export function
module.exports = sanitize; module.exports = sanitize;

11
sanitiser/_input.js

@ -3,10 +3,9 @@ var query_parse= require('../helper/query_parser');
// validate inputs, convert types and apply defaults // validate inputs, convert types and apply defaults
function sanitize( req ){ function sanitize( req ){
req.clean = req.clean || {}; req.clean = req.clean || {};
var params= req.query; var params= req.query;
// ensure the input params are a valid object // ensure the input params are a valid object
if( !isObject( params ) ){ if( !isObject( params ) ){
params = {}; params = {};
@ -14,20 +13,18 @@ function sanitize( req ){
// input text // input text
if('string' !== typeof params.input || !params.input.length){ if('string' !== typeof params.input || !params.input.length){
return { return {
'error': true, 'error': true,
'message': 'invalid param \'input\': text length, must be >0' 'message': 'invalid param \'input\': text length, must be >0'
}; };
} }
req.clean.input = params.input; req.clean.input = params.input;
req.clean.parsed_input = query_parse(params.input); req.clean.parsed_input = query_parse(params.input);
return { 'error': false }; return { 'error': false };
} }
// export function // export function
module.exports = sanitize; module.exports = sanitize;

3
sanitiser/_layers.js

@ -5,7 +5,6 @@ var isObject = require('is-object'),
// validate inputs, convert types and apply defaults // validate inputs, convert types and apply defaults
function sanitize( req ){ function sanitize( req ){
var clean = req.clean || {}; var clean = req.clean || {};
var params= req.query; var params= req.query;
@ -42,7 +41,7 @@ function sanitize( req ){
// pass validated params to next middleware // pass validated params to next middleware
clean.layers = get_layers(layers); clean.layers = get_layers(layers);
req.clean = clean; req.clean = clean;
return { 'error': false }; return { 'error': false };
} }

32
test/unit/helper/query_parser.js

@ -67,13 +67,13 @@ module.exports.tests.parse_one_or_more_tokens = function(test, common) {
var target_layer = get_layers(['admin', 'poi']); var target_layer = get_layers(['admin', 'poi']);
t.equal(typeof address, 'object', 'valid object'); t.equal(typeof address, 'object', 'valid object');
if (parse_address) { if (parse_address) {
t.deepEqual(address.regions.join(''), query, 'since query contained a number, it went through address parsing'); t.deepEqual(address.regions.join(''), query, 'since query contained a number, it went through address parsing');
} else { } else {
t.deepEqual(address.target_layer, target_layer, 'admin_parts set correctly to ' + target_layer.join(', ')); t.deepEqual(address.target_layer, target_layer, 'admin_parts set correctly to ' + target_layer.join(', '));
} }
t.end(); t.end();
}); });
}; };
@ -88,32 +88,32 @@ module.exports.tests.parse_one_or_more_tokens = function(test, common) {
}; };
module.exports.tests.parse_address = function(test, common) { module.exports.tests.parse_address = function(test, common) {
var addresses_nonum = [{ non_street: 'main particle', city: 'new york'}, var addresses_nonum = [{ non_street: 'main particle', city: 'new york'},
{ non_street: 'biggg city block' }, { non_street: 'biggg city block' },
{ non_street: 'the empire state building' } { non_street: 'the empire state building' }
]; ];
var address_with_num = [{ number: 123, street: 'main st', city: 'new york', state: 'ny'}, var address_with_num = [{ number: 123, street: 'main st', city: 'new york', state: 'ny'},
{ number: 456, street: 'pine ave', city: 'san francisco', state: 'CA'}, { number: 456, street: 'pine ave', city: 'san francisco', state: 'CA'},
{ number: 1980, street: 'house st', city: 'hoboken', state: 'NY'} { number: 1980, street: 'house st', city: 'hoboken', state: 'NY'}
]; ];
var address_with_zip = [{ number: 1, street: 'main st', city: 'new york', state: 'ny', zip: 10010}, var address_with_zip = [{ number: 1, street: 'main st', city: 'new york', state: 'ny', zip: 10010},
{ number: 4, street: 'ape ave', city: 'san diego', state: 'CA', zip: 98970}, { number: 4, street: 'ape ave', city: 'san diego', state: 'CA', zip: 98970},
{ number: 19, street: 'house dr', city: 'houston', state: 'TX', zip: 79089} { number: 19, street: 'house dr', city: 'houston', state: 'TX', zip: 79089}
]; ];
var testParse = function(query, hasNumber, hasZip) { var testParse = function(query, hasNumber, hasZip) {
var testcase = 'parse query with ' + (hasNumber ? 'a house number ': 'no house number '); var testcase = 'parse query with ' + (hasNumber ? 'a house number ': 'no house number ');
testcase += 'and ' + (hasZip ? 'a zip ' : 'no zip '); testcase += 'and ' + (hasZip ? 'a zip ' : 'no zip ');
test(testcase, function(t) { test(testcase, function(t) {
var query_string = ''; var query_string = '';
for (var k in query) { for (var k in query) {
query_string += ' ' + query[k]; query_string += ' ' + query[k];
} }
// remove leading whitespace // remove leading whitespace
query_string = query_string.substring(1); query_string = query_string.substring(1);
var address = parser(query_string); var address = parser(query_string);
var non_address_layer = get_layers(['admin', 'poi']); var non_address_layer = get_layers(['admin', 'poi']);
@ -126,14 +126,14 @@ module.exports.tests.parse_address = function(test, common) {
} }
if ((hasNumber || hasZip) && query.street) { if ((hasNumber || hasZip) && query.street) {
t.equal(typeof address.number, 'number', 'valid house number format (' + address.number + ')'); t.equal(typeof address.number, 'number', 'valid house number format (' + address.number + ')');
t.equal(address.number, query.number, 'correct house number (' + query.number + ')'); t.equal(address.number, query.number, 'correct house number (' + query.number + ')');
t.equal(typeof address.street, 'string', 'valid street name format (' + address.street + ')'); t.equal(typeof address.street, 'string', 'valid street name format (' + address.street + ')');
t.equal(address.street, query.street, 'correct street name (' + query.street + ')'); t.equal(address.street, query.street, 'correct street name (' + query.street + ')');
} }
if (hasZip) { if (hasZip) {
t.equal(typeof address.postalcode, 'number', 'valid zip (' + address.postalcode + ')'); t.equal(typeof address.postalcode, 'number', 'valid zip (' + address.postalcode + ')');
t.equal(address.postalcode, query.zip, 'correct postal code (' + query.zip + ')'); t.equal(address.postalcode, query.zip, 'correct postal code (' + query.zip + ')');
} }

Loading…
Cancel
Save