From 6d32328fcaa36e3e90eeca0931e11a68af045bb8 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 9 Oct 2017 11:27:14 -0400 Subject: [PATCH] exposed any/all functions --- .../predicates/has_results_at_layers.js | 28 +++++-- .../predicates/has_results_at_layers.js | 78 +++++++++++++++---- 2 files changed, 83 insertions(+), 23 deletions(-) diff --git a/controller/predicates/has_results_at_layers.js b/controller/predicates/has_results_at_layers.js index e7a389d0..f5ec794d 100644 --- a/controller/predicates/has_results_at_layers.js +++ b/controller/predicates/has_results_at_layers.js @@ -7,9 +7,9 @@ const stackTraceLine = require('../../helper/stackTraceLine'); // example usage: determining if the response contains only admin results -module.exports = (layers) => { - return (request, response) => { - const has_results_at_layers = !_.isEmpty( +module.exports = { + any: (layers) => (request, response) => { + const has_results_at_any_layer = !_.isEmpty( _.intersection( // convert layers to an array if it isn't already one _.castArray(layers), @@ -18,10 +18,26 @@ module.exports = (layers) => { )); debugLog.push(request, () => ({ - reply: {[layers]: has_results_at_layers}, + reply: {[layers]: has_results_at_any_layer}, stack_trace: stackTraceLine() })); - return has_results_at_layers; - }; + return has_results_at_any_layer; + + }, + all: (layers) => (request, response) => { + const has_results_at_all_layers = _.isEmpty( + _.difference( + _.map(response.data, _.property('layer')), + _.castArray(layers) + ) + ); + + debugLog.push(request, () => ({ + reply: {[layers]: has_results_at_all_layers}, + stack_trace: stackTraceLine() + })); + return has_results_at_all_layers; + + } }; diff --git a/test/unit/controller/predicates/has_results_at_layers.js b/test/unit/controller/predicates/has_results_at_layers.js index f81e9045..3e36ccb1 100644 --- a/test/unit/controller/predicates/has_results_at_layers.js +++ b/test/unit/controller/predicates/has_results_at_layers.js @@ -6,15 +6,17 @@ const has_results_at_layers = require('../../../../controller/predicates/has_res module.exports.tests = {}; module.exports.tests.interface = (test, common) => { - test('valid interface', (t) => { - t.equal(typeof has_results_at_layers, 'function', 'has_results_at_layers is a function'); - t.equal(has_results_at_layers.length, 1); + test('valid interface', t => { + t.equal(typeof has_results_at_layers.any(), 'function', 'has_results_at_layers.any is a function'); + t.equal(has_results_at_layers.any.length, 1); + t.equal(typeof has_results_at_layers.all(), 'function', 'has_results_at_layers.all is a function'); + t.equal(has_results_at_layers.all.length, 1); t.end(); }); }; -module.exports.tests.true_conditions = (test, common) => { - test('should return true when any result.layer matches any layer in array', (t) => { +module.exports.tests.any_true_conditions = (test, common) => { + test('any: should return true when any result.layer matches any layer in array', t => { const req = {}; const res = { data: [ @@ -30,12 +32,12 @@ module.exports.tests.true_conditions = (test, common) => { ] }; - t.ok(has_results_at_layers(['layer 2', 'layer 4'])(req, res)); + t.ok(has_results_at_layers.any(['layer 2', 'layer 4'])(req, res)); t.end(); }); - test('should return true when any result.layer matches layer string', (t) => { + test('any: should return true when any result.layer matches layer string', t => { const req = {}; const res = { data: [ @@ -51,35 +53,35 @@ module.exports.tests.true_conditions = (test, common) => { ] }; - t.ok(has_results_at_layers('layer 2')(req, res)); + t.ok(has_results_at_layers.any('layer 2')(req, res)); t.end(); }); }; -module.exports.tests.false_conditions = (test, common) => { - test('should return false when response has undefined data', (t) => { +module.exports.tests.any_false_conditions = (test, common) => { + test('any: should return false when response has undefined data', t => { const req = {}; const res = {}; - t.notOk(has_results_at_layers('layer')(req, res)); + t.notOk(has_results_at_layers.any('layer')(req, res)); t.end(); }); - test('should return false when response has empty data array', (t) => { + test('any: should return false when response has empty data array', t => { const req = {}; const res = { data: [] }; - t.notOk(has_results_at_layers('layer')(req, res)); + t.notOk(has_results_at_layers.any('layer')(req, res)); t.end(); }); - test('should return false when layer is a substring of non-array string layers parameter', (t) => { + test('any: should return false when layer is a substring of non-array string layers parameter', t => { const req = {}; const res = { data: [ @@ -89,12 +91,12 @@ module.exports.tests.false_conditions = (test, common) => { ] }; - t.notOk(has_results_at_layers('layer')(req, res)); + t.notOk(has_results_at_layers.any('layer')(req, res)); t.end(); }); - test('should return false when no results have layer in supplied layers', (t) => { + test('any: should return false when no results have layer in supplied layers', t => { const req = {}; const res = { data: [ @@ -104,7 +106,49 @@ module.exports.tests.false_conditions = (test, common) => { ] }; - t.notOk(has_results_at_layers(['layer 2', 'layer 3'])(req, res)); + t.notOk(has_results_at_layers.any(['layer 2', 'layer 3'])(req, res)); + t.end(); + + }); + +}; + +module.exports.tests.all_true_conditions = (test, common) => { + test('all: should return true when all result.layer values are in supplied layers', t => { + const req = {}; + const res = { + data: [ + { + layer: 'layer 1' + }, + { + layer: 'layer 2' + } + ] + }; + + t.ok(has_results_at_layers.all(['layer 1', 'layer 2', 'layer 4'])(req, res)); + t.end(); + + }); + +}; + +module.exports.tests.all_false_conditions = (test, common) => { + test('all: should return true when all result.layer values are in supplied layers', t => { + const req = {}; + const res = { + data: [ + { + layer: 'layer 1' + }, + { + layer: 'layer 3' + } + ] + }; + + t.notOk(has_results_at_layers.all(['layer 1', 'layer 2'])(req, res)); t.end(); });