From 09c45b5bed2de28099df6f2b0ebd28b62b021cf4 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Fri, 4 Nov 2016 14:56:19 -0400 Subject: [PATCH] Support island input from libpostal This includes an edge case for Hawaii to handle islands which are mostly stored as counties in our data currently. See https://github.com/pelias/whosonfirst/issues/94. --- query/text_parser.js | 18 ++++++++++++++++++ test/unit/query/text_parser.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/query/text_parser.js b/query/text_parser.js index c5b8da44..0722f2a3 100644 --- a/query/text_parser.js +++ b/query/text_parser.js @@ -39,6 +39,12 @@ function addParsedVariablesToQueryVariables( parsed_text, vs ){ vs.var( 'input:postcode', parsed_text.postalcode ); } + + // island + if( parsed_text.hasOwnProperty('island') ){ + vs.var( 'input:island', parsed_text.island ); + } + // ==== add parsed matches [admin components] ==== // city @@ -83,6 +89,11 @@ function addParsedVariablesToQueryVariables( parsed_text, vs ){ vs.unset( 'input:query' ); } + + if (shouldTreatIslandAsCounty(vs)) { + vs.var( 'input:county', vs.var('input:island').toString()); + vs.unset('input:island'); + } } function shouldSetQueryIntoHouseNumber(vs) { @@ -91,4 +102,11 @@ function shouldSetQueryIntoHouseNumber(vs) { /^[0-9]+$/.test(vs.var('input:query').toString()); } +function shouldTreatIslandAsCounty(vs) { + return vs.isset('input:island') && + vs.isset('input:region') && + !vs.isset('input:county') && + vs.var('input:region').toString() === 'hawaii'; +} + module.exports = addParsedVariablesToQueryVariables; diff --git a/test/unit/query/text_parser.js b/test/unit/query/text_parser.js index 34830c7f..0fdd52cd 100644 --- a/test/unit/query/text_parser.js +++ b/test/unit/query/text_parser.js @@ -154,6 +154,37 @@ module.exports.tests.housenumber_special_cases = function(test, common) { }; +module.exports.tests.islands = function(test, common) { + test('island normally left untouched', function(t) { + var parsed_text = { + island: 'an island', + state: 'somewhere' + }; + var vs = new VariableStore(); + + text_parser(parsed_text, vs); + + t.equals(vs.var('input:island').toString(), 'an island'); + t.equals(vs.var('input:region').toString(), 'somewhere'); + t.end(); + }); + + test('islands in hawaii should have island changed to county', function(t) { + var parsed_text = { + island: 'maui', + state: 'hawaii' + }; + var vs = new VariableStore(); + + text_parser(parsed_text, vs); + + t.equals(vs.var('input:county').toString(), 'maui'); + t.false(vs.isset('input:island')); + t.equals(vs.var('input:region').toString(), 'hawaii'); + t.end(); + }); +}; + module.exports.all = function (tape, common) { function test(name, testFunction) { return tape('text_parser ' + name, testFunction);