From 9ae838903a8d7eb69c8bde0b3b62f4c7228ec438 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Wed, 5 Jul 2017 10:02:05 -0400 Subject: [PATCH] converted to 'any' property for brevity --- .../has_any_parsed_text_property.js | 17 +++++++++ .../predicates/has_parsed_text_property.js | 13 ------- routes/v1.js | 17 ++++----- ...rty.js => has_any_parsed_text_property.js} | 36 ++++++++++++------- test/unit/run.js | 2 +- 5 files changed, 48 insertions(+), 37 deletions(-) create mode 100644 controller/predicates/has_any_parsed_text_property.js delete mode 100644 controller/predicates/has_parsed_text_property.js rename test/unit/controller/predicates/{has_parsed_text_property.js => has_any_parsed_text_property.js} (54%) diff --git a/controller/predicates/has_any_parsed_text_property.js b/controller/predicates/has_any_parsed_text_property.js new file mode 100644 index 00000000..8f1be8b2 --- /dev/null +++ b/controller/predicates/has_any_parsed_text_property.js @@ -0,0 +1,17 @@ +const _ = require('lodash'); + +// return true if any setup parameter is a key of request.clean.parsed_text +// "arguments" is only available in long-form function declarations, cannot be shortened to fat arrow syntax +// potential improvement: inject set operator to allow for any/all functionality +module.exports = function() { + // save off requested properties since arguments can't be referenced later + const properties = _.values(arguments); + + return (request, response) => !_.isEmpty( + _.intersection( + properties, + _.keys(_.get(request, ['clean', 'parsed_text'], {})) + ) + ); + +}; diff --git a/controller/predicates/has_parsed_text_property.js b/controller/predicates/has_parsed_text_property.js deleted file mode 100644 index f6a51371..00000000 --- a/controller/predicates/has_parsed_text_property.js +++ /dev/null @@ -1,13 +0,0 @@ -const _ = require('lodash'); - -// returns a function that returns true if any result.layer is in any of the -// supplied layers using array intersection - -// example usage: determining if the response contains only admin results - -module.exports = (property) => { - return (request, response) => { - return _.has(request, ['clean', 'parsed_text', property]); - }; - -}; diff --git a/routes/v1.js b/routes/v1.js index a8cff01e..18cc39b7 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -68,7 +68,7 @@ var postProc = { }; // predicates that drive whether controller/search runs -const hasParsedTextProperty = require('../controller/predicates/has_parsed_text_property'); +const hasAnyParsedTextProperty = require('../controller/predicates/has_any_parsed_text_property'); const hasResponseData = require('../controller/predicates/has_response_data'); const hasRequestErrors = require('../controller/predicates/has_request_errors'); const isCoarseReverse = require('../controller/predicates/is_coarse_reverse'); @@ -80,13 +80,8 @@ const hasResponseDataOrRequestErrors = any(hasResponseData, hasRequestErrors); const hasAdminOnlyResults = not(hasResultsAtLayers(['venue', 'address', 'street'])); const hasNumberButNotStreet = all( - hasParsedTextProperty('number'), - not(hasParsedTextProperty('street')) -); - -const hasQueryOrCategory = any( - hasParsedTextProperty('query'), - hasParsedTextProperty('category') + hasAnyParsedTextProperty('number'), + not(hasAnyParsedTextProperty('street')) ); const serviceWrapper = require('pelias-microservice-wrapper').service; @@ -121,15 +116,15 @@ function addRoutes(app, peliasConfig) { // don't run placeholder if there's a number but no street not(hasNumberButNotStreet), // don't run placeholder if there's a query or category - not(hasQueryOrCategory) + not(hasAnyParsedTextProperty('query', 'category')) ); const searchWithIdsShouldExecute = all( not(hasRequestErrors), // don't search-with-ids if there's a query or category - not(hasQueryOrCategory), + not(hasAnyParsedTextProperty('query', 'category')), // there must be a street - hasParsedTextProperty('street') + hasAnyParsedTextProperty('street') ); // execute under the following conditions: diff --git a/test/unit/controller/predicates/has_parsed_text_property.js b/test/unit/controller/predicates/has_any_parsed_text_property.js similarity index 54% rename from test/unit/controller/predicates/has_parsed_text_property.js rename to test/unit/controller/predicates/has_any_parsed_text_property.js index a6b06be1..4a985bbe 100644 --- a/test/unit/controller/predicates/has_parsed_text_property.js +++ b/test/unit/controller/predicates/has_any_parsed_text_property.js @@ -1,13 +1,13 @@ 'use strict'; const _ = require('lodash'); -const has_parsed_text_property = require('../../../../controller/predicates/has_parsed_text_property'); +const has_any_parsed_text_property = require('../../../../controller/predicates/has_any_parsed_text_property'); module.exports.tests = {}; module.exports.tests.interface = (test, common) => { test('valid interface', (t) => { - t.equal(typeof has_parsed_text_property, 'function', 'has_parsed_text_property is a function'); + t.ok(_.isFunction(has_any_parsed_text_property), 'has_any_parsed_text_property is a function'); t.end(); }); }; @@ -21,9 +21,23 @@ module.exports.tests.true_conditions = (test, common) => { } } }; - const res = {}; - t.ok(has_parsed_text_property('property')(req, res)); + t.ok(has_any_parsed_text_property('property')(req)); + t.end(); + + }); + + test('clean.parsed_text with any property should return true ', (t) => { + const req = { + clean: { + parsed_text: { + property2: 'value2', + property3: 'value3' + } + } + }; + + t.ok(has_any_parsed_text_property('property1', 'property3')(req)); t.end(); }); @@ -32,9 +46,7 @@ module.exports.tests.true_conditions = (test, common) => { module.exports.tests.false_conditions = (test, common) => { test('undefined request should return false', (t) => { - const req = {}; - - t.notOk(has_parsed_text_property('property')(req, undefined)); + t.notOk(has_any_parsed_text_property('property')()); t.end(); }); @@ -42,7 +54,7 @@ module.exports.tests.false_conditions = (test, common) => { test('undefined request.clean should return false', (t) => { const req = {}; - t.notOk(has_parsed_text_property('property')(req, undefined)); + t.notOk(has_any_parsed_text_property('property')(req)); t.end(); }); @@ -52,19 +64,19 @@ module.exports.tests.false_conditions = (test, common) => { clean: {} }; - t.notOk(has_parsed_text_property('property')(req, undefined)); + t.notOk(has_any_parsed_text_property('property')(req)); t.end(); }); - test('undefined request.clean.parsed_text.property should return false', (t) => { + test('request.clean.parsed_text with none of the supplied properties should return false', (t) => { const req = { clean: { parsed_text: {} } }; - t.notOk(has_parsed_text_property('property')(req, undefined)); + t.notOk(has_any_parsed_text_property('property1', 'property2')(req)); t.end(); }); @@ -73,7 +85,7 @@ module.exports.tests.false_conditions = (test, common) => { module.exports.all = (tape, common) => { function test(name, testFunction) { - return tape(`GET /has_parsed_text_property ${name}`, testFunction); + return tape(`GET /has_any_parsed_text_property ${name}`, testFunction); } for( const testCase in module.exports.tests ){ diff --git a/test/unit/run.js b/test/unit/run.js index 70a2a449..66ed53d8 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -17,7 +17,7 @@ var tests = [ require('./controller/placeholder'), require('./controller/search'), require('./controller/search_with_ids'), - require('./controller/predicates/has_parsed_text_property'), + require('./controller/predicates/has_any_parsed_text_property'), require('./controller/predicates/has_response_data'), require('./controller/predicates/has_results_at_layers'), require('./controller/predicates/has_request_errors'),