|
|
@ -5,43 +5,55 @@ var get_layers = require('../helper/layers'); |
|
|
|
var delim = ','; |
|
|
|
var delim = ','; |
|
|
|
|
|
|
|
|
|
|
|
module.exports = function(query) { |
|
|
|
module.exports = function(query) { |
|
|
|
var parsedAddress0 = {}; |
|
|
|
|
|
|
|
var parsedAddress1 = {}; |
|
|
|
|
|
|
|
var parsedAddress2 = {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// naive approach
|
|
|
|
|
|
|
|
// for admin matching during query time
|
|
|
|
|
|
|
|
// split 'flatiron, new york, ny' into 'flatiron' and 'new york, ny'
|
|
|
|
|
|
|
|
var delimIndex = query.indexOf(delim); |
|
|
|
|
|
|
|
if ( delimIndex !== -1 ) { |
|
|
|
|
|
|
|
parsedAddress0.name = query.substring(0, delimIndex); |
|
|
|
|
|
|
|
parsedAddress0.admin_parts = query.substring(delimIndex + 1).trim(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var tokenized = query.split(/[ ,]+/); |
|
|
|
var tokenized = query.split(/[ ,]+/); |
|
|
|
var hasNumber = /\d/.test(query); |
|
|
|
var hasNumber = /\d/.test(query); |
|
|
|
|
|
|
|
|
|
|
|
// set target_layer if input length <= 3 characters
|
|
|
|
var getAdminPartsBySplittingOnDelim = function(query) { |
|
|
|
if (query.length <= 3 ) { |
|
|
|
// naive approach - for admin matching during query time
|
|
|
|
// no address parsing required
|
|
|
|
// split 'flatiron, new york, ny' into 'flatiron' and 'new york, ny'
|
|
|
|
parsedAddress2.target_layer = get_layers(['admin']); |
|
|
|
var delimIndex = query.indexOf(delim); |
|
|
|
} else if (tokenized.length === 1 || (tokenized.length < 3 && !hasNumber)) { |
|
|
|
var address = {}; |
|
|
|
// no need to hit address layers if there's only one (or two) token(s)
|
|
|
|
if ( delimIndex !== -1 ) { |
|
|
|
parsedAddress2.target_layer = get_layers(['admin', 'poi']); |
|
|
|
address.name = query.substring(0, delimIndex); |
|
|
|
} else { |
|
|
|
address.admin_parts = query.substring(delimIndex + 1).trim(); |
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return address; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var getTargetLayersWhenAddressParsingIsNotNecessary = function(query) { |
|
|
|
|
|
|
|
var address = {}; |
|
|
|
|
|
|
|
// set target_layer if input length <= 3 characters
|
|
|
|
|
|
|
|
if (query.length <= 3 ) { |
|
|
|
|
|
|
|
// no address parsing required
|
|
|
|
|
|
|
|
address.target_layer = get_layers(['admin']); |
|
|
|
|
|
|
|
} else if (tokenized.length === 1 || (tokenized.length < 3 && !hasNumber)) { |
|
|
|
|
|
|
|
// no need to hit address layers if there's only one (or two) token(s)
|
|
|
|
|
|
|
|
address.target_layer = get_layers(['admin', 'poi']); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return address.target_layer ? address : null; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var getAddressParts = function(query) { |
|
|
|
// address parsing
|
|
|
|
// address parsing
|
|
|
|
parsedAddress1 = parser( query ); |
|
|
|
var address = parser( query ); |
|
|
|
// set target_layer if input suggests no address
|
|
|
|
// set target_layer if input suggests no address
|
|
|
|
if (parsedAddress1.text === parsedAddress1.regions.join(' ') && !hasNumber) { |
|
|
|
if (address.text === address.regions.join(' ') && !hasNumber) { |
|
|
|
parsedAddress2.target_layer = get_layers(['admin', 'poi']); |
|
|
|
address.target_layer = get_layers(['admin', 'poi']); |
|
|
|
} // else {
|
|
|
|
} |
|
|
|
// this might be an overkill - you'd want to search for poi and admin
|
|
|
|
|
|
|
|
// even if an address is being queried. TBD
|
|
|
|
return address; |
|
|
|
// parsedAddress2.target_layer = get_layers(['address']);
|
|
|
|
}; |
|
|
|
// }
|
|
|
|
|
|
|
|
} |
|
|
|
var addressWithAdminParts = getAdminPartsBySplittingOnDelim(query); |
|
|
|
|
|
|
|
var addressWithTargetLayers= getTargetLayersWhenAddressParsingIsNotNecessary(query); |
|
|
|
|
|
|
|
var addressWithAddressParts= !addressWithTargetLayers ? getAddressParts(query) : {};
|
|
|
|
|
|
|
|
|
|
|
|
var parsedAddress = extend(parsedAddress0, parsedAddress1, parsedAddress2); |
|
|
|
var parsedAddress = extend(addressWithAdminParts,
|
|
|
|
|
|
|
|
addressWithTargetLayers,
|
|
|
|
|
|
|
|
addressWithAddressParts); |
|
|
|
|
|
|
|
|
|
|
|
var address_parts = [ 'name', |
|
|
|
var address_parts = [ 'name', |
|
|
|
'number', |
|
|
|
'number', |
|
|
|