From 42097fafb2a2e1c61a38fc936ffa921d9367684c Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Tue, 5 Sep 2017 14:52:57 -0400 Subject: [PATCH] treat undefined text-analyzer response as no house number available --- sanitizer/_synthesize_analysis.js | 8 ++---- test/unit/sanitizer/_synthesize_analysis.js | 30 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/sanitizer/_synthesize_analysis.js b/sanitizer/_synthesize_analysis.js index a4764549..3822d9d4 100644 --- a/sanitizer/_synthesize_analysis.js +++ b/sanitizer/_synthesize_analysis.js @@ -27,12 +27,8 @@ function isPostalCodeOnly(parsed_text) { // so because we're treating the entire field as a street address, it's safe // to assume that an identified postcode is actually a house number. function getHouseNumberField(analyzed_address) { - for (var field of ['number', 'postalcode']) { - if (analyzed_address.hasOwnProperty(field)) { - return field; - } - } - + // return the first field available in the libpostal response, undefined if none + return _.find(['number', 'postalcode'], _.partial(_.has, analyzed_address)); } function _sanitize( raw, clean ){ diff --git a/test/unit/sanitizer/_synthesize_analysis.js b/test/unit/sanitizer/_synthesize_analysis.js index 287b0e0d..64243dc9 100644 --- a/test/unit/sanitizer/_synthesize_analysis.js +++ b/test/unit/sanitizer/_synthesize_analysis.js @@ -233,6 +233,36 @@ module.exports.tests.text_parser = function(test, common) { }); + test('text_analyzer returning undefined on address resolution should treat as if no house number field was found', t => { + var sanitizer = proxyquire('../../../sanitizer/_synthesize_analysis', { + 'pelias-text-analyzer': { parse: function(query) { + t.equals(query, 'Street Value'); + + return undefined; + } + }}); + + const raw = { + address: 'Street Value' + }; + + const clean = {}; + + const expected_clean = { + parsed_text: { + street: 'Street Value' + } + }; + + const messages = sanitizer().sanitize(raw, clean); + + t.deepEquals(clean, expected_clean); + t.deepEquals(messages.errors, [], 'no errors'); + t.deepEquals(messages.warnings, [], 'no warnings'); + t.end(); + + }); + test('return an array of expected parameters in object form for validation', function (t) { const sanitizer = require('../../../sanitizer/_synthesize_analysis'); const expected = [