From 103a52cff5cc257dc6689d4872eb26bba4269aa7 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Fri, 4 Sep 2015 15:22:13 -0400 Subject: [PATCH] Calculate intersection of types requested by source and layers params --- helper/types.js | 24 ++++++++++++++++++++++-- test/unit/helper/types.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/helper/types.js b/helper/types.js index 20443ad9..a87a65b8 100644 --- a/helper/types.js +++ b/helper/types.js @@ -1,12 +1,32 @@ var valid_types = require( '../query/types' ); +/** + * Calculate the set-style intersection of two arrays + */ +var intersection = function intersection(set1, set2) { + return set2.filter(function(value) { + return set1.indexOf(value) !== -1; + }); +}; + module.exports = function calculate_types(clean_types) { if (!clean_types) { return undefined; } - if (clean_types.from_layers) { - return clean_types.from_layers; + + if (clean_types.from_layers || clean_types.from_source) { + var types = valid_types; + + if (clean_types.from_layers) { + types = intersection(types, clean_types.from_layers); + } + + if (clean_types.from_source) { + types = intersection(types, clean_types.from_source); + } + + return types; } if (clean_types.from_address_parser) { diff --git a/test/unit/helper/types.js b/test/unit/helper/types.js index d56f12d2..2398c6bb 100644 --- a/test/unit/helper/types.js +++ b/test/unit/helper/types.js @@ -55,6 +55,35 @@ module.exports.tests.layers_parameter_and_address_parser = function(test, common }); }; +module.exports.tests.source_parameter = function(test, common) { + test('source parameter specified', function(t) { + var cleaned_types = { + from_source: ['openaddresses'] + }; + + var actual = types(cleaned_types); + + var expected = ['openaddresses']; + t.deepEqual(actual, expected, 'type parameter set to types specified by source'); + t.end(); + }); +}; + +module.exports.tests.source_and_layers_parameters = function(test, common) { + test('source and layers parameter both specified', function(t) { + var cleaned_types = { + from_source: ['openaddresses'], + from_layers: ['osmaddress', 'openaddresses'] + }; + + var actual = types(cleaned_types); + + var expected = ['openaddresses']; + t.deepEqual(actual, expected, 'type set to intersection of source and layer types'); + t.end(); + }); +}; + module.exports.all = function (tape, common) { function test(name, testFunction) {