From 817981228272abe518ecb562f07e7141ccbfbf8b Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 10 Oct 2016 11:21:18 -0400 Subject: [PATCH] added sanitizer for synthesizing an analysis from parameters --- sanitizer/_synthesize_analysis.js | 34 +++++++ test/unit/run.js | 1 + test/unit/sanitizer/_synthesize_analysis.js | 103 ++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 sanitizer/_synthesize_analysis.js create mode 100644 test/unit/sanitizer/_synthesize_analysis.js diff --git a/sanitizer/_synthesize_analysis.js b/sanitizer/_synthesize_analysis.js new file mode 100644 index 00000000..ac1ad4c8 --- /dev/null +++ b/sanitizer/_synthesize_analysis.js @@ -0,0 +1,34 @@ +var _ = require('lodash'); + +var fields = ['query', 'address', 'neighbourhood', 'city', + 'county', 'state', 'postalcode', 'country']; + +function normalizeWhitespaceToSingleSpace(val) { + return _.replace(_.trim(val), /\s+/g, ' '); +} + +function sanitize( raw, clean ){ + + // error & warning messages + var messages = { errors: [], warnings: [] }; + + // collect all the valid values into a single object + clean.parsed_text = fields.reduce(function(o, f) { + if (_.isString(raw[f]) && !_.isEmpty(_.trim(raw[f]))) { + o[f] = normalizeWhitespaceToSingleSpace(raw[f]); + } + + return o; + + }, {}); + + if (_.isEmpty(Object.keys(clean.parsed_text))) { + messages.errors.push('at least one of the following fields is required: ' + + 'query, address, neighbourhood, city, county, state, postalcode, country'); + } + + return messages; +} + +// export function +module.exports = sanitize; diff --git a/test/unit/run.js b/test/unit/run.js index 3a8114f0..63c2dd4b 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -49,6 +49,7 @@ var tests = [ require('./sanitizer/_size'), require('./sanitizer/_sources'), require('./sanitizer/_sources_and_layers'), + require('./sanitizer/_synthesize_analysis'), require('./sanitizer/_text'), require('./sanitizer/_text_addressit'), require('./sanitizer/_tokenizer'), diff --git a/test/unit/sanitizer/_synthesize_analysis.js b/test/unit/sanitizer/_synthesize_analysis.js new file mode 100644 index 00000000..7bfc52dd --- /dev/null +++ b/test/unit/sanitizer/_synthesize_analysis.js @@ -0,0 +1,103 @@ +var sanitizer = require('../../../sanitizer/_synthesize_analysis'); +var _ = require('lodash'); + +module.exports.tests = {}; + +module.exports.tests.text_parser = function(test, common) { + test('all variables should be parsed', function(t) { + var raw = { + query: ' \t query \t value \t ', + address: ' \t address \t value \t ', + neighbourhood: ' \t neighbourhood \t value \t ', + city: ' \t city \t value \t ', + county: ' \t county \t value \t ', + state: ' \t state \t value \t ', + postalcode: ' \t postalcode \t value \t ', + country: ' \t country \t value \t ' + }; + + var clean = {}; + + var expected_clean = { + parsed_text: { + query: 'query value', + address: 'address value', + neighbourhood: 'neighbourhood value', + city: 'city value', + county: 'county value', + state: 'state value', + postalcode: 'postalcode value', + country: 'country value' + } + }; + + var messages = sanitizer(raw, clean); + + t.deepEquals(clean, expected_clean); + t.deepEquals(messages.errors, [], 'no errors'); + t.deepEquals(messages.warnings, [], 'no warnings'); + t.end(); + + }); + + test('non-string and blank string values should be treated as not supplied', function(t) { + // helper to return a random value that's considered invalid + function getInvalidValue() { + return _.sample([{}, [], false, '', ' \t ', 17, undefined]); + } + + var raw = { + query: getInvalidValue(), + address: getInvalidValue(), + neighbourhood: getInvalidValue(), + city: getInvalidValue(), + county: getInvalidValue(), + state: getInvalidValue(), + postalcode: getInvalidValue(), + country: getInvalidValue() + }; + + var clean = {}; + + var expected_clean = { + parsed_text: {} + }; + + var messages = sanitizer(raw, clean); + + t.deepEquals(clean, expected_clean); + t.deepEquals(messages.errors, ['at least one of the following fields is required: ' + + 'query, address, neighbourhood, city, county, state, postalcode, country'], 'no errors'); + t.deepEquals(messages.warnings, [], 'no warnings'); + t.end(); + + }); + + test('no supplied fields should return error', function(t) { + var raw = {}; + + var clean = {}; + + var expected_clean = { parsed_text: {} }; + + var messages = sanitizer(raw, clean); + + t.deepEquals(clean, expected_clean); + t.deepEquals(messages.errors, ['at least one of the following fields is required: ' + + 'query, address, neighbourhood, city, county, state, postalcode, country'], 'no errors'); + t.deepEquals(messages.warnings, [], 'no warnings'); + t.end(); + + }); + +}; + +module.exports.all = function (tape, common) { + function test(name, testFunction) { + return tape('sanitizer _synthesize_analysis: ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +};