Browse Source

introducing target_layer that gets set part of input_parsing

- if input is less than 3 characters -> only hit admin layers
- if input suggests that its not a street address -> avoid hitting address layers
pull/169/head
Harish Krishna 9 years ago
parent
commit
1d24890b27
  1. 5
      controller/search.js
  2. 52
      sanitiser/_input.js

5
controller/search.js

@ -21,6 +21,11 @@ function setup( backend, query ){
cmd.type = req.clean.layers; cmd.type = req.clean.layers;
} }
// set type if input suggests targeting a layer(s)
if (req.clean.parsed_input) {
cmd.type = req.clean.parsed_input.target_layer;
}
// query backend // query backend
service.search( backend, cmd, function( err, docs ){ service.search( backend, cmd, function( err, docs ){

52
sanitiser/_input.js

@ -1,7 +1,7 @@
var isObject = require('is-object'); var isObject = require('is-object');
// var parser1 = require('parse-address'); // works well with US addresses var parser = require('addressit');
var parser2 = require('addressit'); // freeform address parser (backup)
var extend = require('extend'); var extend = require('extend');
var get_layers = require('../helper/layers');
// validate inputs, convert types and apply defaults // validate inputs, convert types and apply defaults
function sanitize( req ){ function sanitize( req ){
@ -36,30 +36,20 @@ function sanitize( req ){
} }
// address parsing // address parsing
// var parsedAddress1 = parser1.parseAddress(params.input); var parsedAddress1 = parser( params.input );
// postcodes (should be its own file. Contribute back to addressIt)
// {
// "US":/^\d{5}([\-]?\d{4})?$/,
// "UK":/^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$/,
// "DE":/\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b/,
// "CA":/^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$/,
// "FR":/^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$/,
// "IT":/^(V-|I-)?[0-9]{5}$/,
// "AU":/^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$/,
// "NL":/^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$/,
// "ES":/^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$/,
// "DK":/^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$/,
// "SE":/^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$/,
// "BE":/^[1-9]{1}[0-9]{3}$/,
// "IN":/^\d{6}$/
// }
// using US PostCode for now // input parsing
var parsedAddress2 = parser2(params.input, { rePostalCode: /^\d{5}([\-]?\d{4})?$/ }); var parsedAddress2 = {};
// set target_layer if input length < 3 characters
if (params.input.length <= 3) {
parsedAddress2.target_layer = get_layers(['admin']);
}
// set target_layer if input suggests no address
if (parsedAddress1.text === parsedAddress1.regions.join(' ')) {
parsedAddress2.target_layer = get_layers(['admin', 'poi']);
}
// var parsedAddress = extend(parsedAddress0, parsedAddress1, parsedAddress2); var parsedAddress = extend(parsedAddress0, parsedAddress1, parsedAddress2);
var parsedAddress = extend(parsedAddress0, parsedAddress2);
var address_parts = [ 'name', var address_parts = [ 'name',
'number', 'number',
@ -69,7 +59,8 @@ function sanitize( req ){
'country', 'country',
'postalcode', 'postalcode',
'regions', 'regions',
'admin_parts' 'admin_parts',
'target_layer'
]; ];
req.clean.parsed_input = {}; req.clean.parsed_input = {};
@ -84,12 +75,13 @@ function sanitize( req ){
// name : parsedAddress.name, // name : parsedAddress.name,
// number : parsedAddress.number, // number : parsedAddress.number,
// street : parsedAddress.street, // street : parsedAddress.street,
// admin2 : parsedAddress.city, // city : parsedAddress.city,
// admin1 : parsedAddress.state, // state : parsedAddress.state,
// admin0 : parsedAddress.country, // country: parsedAddress.country,
// zip : parsedAddress.zip, // postalcode : parsedAddress.postalcode,
// regions: parsedAddress.regions, // regions: parsedAddress.regions,
// admin_parts: parsedAddress.admin_parts // admin_parts: parsedAddress.admin_parts,
// target_layer: parsedAddress.target_layer
// } // }

Loading…
Cancel
Save