Browse Source

Calculate intersection of types requested by source and layers params

pull/221/head
Julian Simioni 9 years ago
parent
commit
103a52cff5
  1. 24
      helper/types.js
  2. 29
      test/unit/helper/types.js

24
helper/types.js

@ -1,12 +1,32 @@
var valid_types = require( '../query/types' ); 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) { module.exports = function calculate_types(clean_types) {
if (!clean_types) { if (!clean_types) {
return undefined; 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) { if (clean_types.from_address_parser) {

29
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) { module.exports.all = function (tape, common) {
function test(name, testFunction) { function test(name, testFunction) {

Loading…
Cancel
Save