Skip to content

Commit 404d3de

Browse files
committed
fix(layers): Handle sources and layers param with address filter
As reported in #1653, there is an issue when negative sources are specified in combination with negative layers. The end result is that the negative layers are ignored. This issue was likely introduced in #1604 or #1525, the pull requests where we added the concept of negative sources and layers and subsequently improved them. Fixes #1525
1 parent bfbb98e commit 404d3de

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

sanitizer/_address_layer_filter.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ function _setup(tm) {
103103

104104
// handle the case where 'sources' were explicitly specified
105105
else if (_.isArray(clean.sources)) {
106-
107106
// we need to create a list of layers for the specified sources
108107
let sourceLayers = clean.sources.reduce((l, key) => l.concat(tm.layers_by_source[key] || []), []);
109108
sourceLayers = _.uniq(sourceLayers); // dedupe
@@ -114,8 +113,18 @@ function _setup(tm) {
114113
return messages;
115114
}
116115

117-
// target all layers for the sources specified except 'address'
118-
clean.layers = sourceLayers.filter(item => item !== 'address'); // exclude 'address'
116+
// create a list of all "possible layers": layers from the specified sources, minus address layer
117+
const possibleLayers = sourceLayers.filter(item => item !== 'address');
118+
119+
// intersect the possible layers with any already specified layer preferences
120+
if (_.isArray(clean.layers) && clean.layers.length > 1) {
121+
// layers already exist, intersect
122+
clean.layers = _.intersection(clean.layers, possibleLayers);
123+
} else {
124+
// no layers already, use all possible layers
125+
clean.layers = possibleLayers;
126+
}
127+
119128
messages.warnings.push(ADDRESS_FILTER_WARNING);
120129
}
121130
}

test/unit/sanitizer/_address_layer_filter.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,26 @@ module.exports.tests.sanitize = function (test, common) {
117117
t.end();
118118
});
119119

120+
test('sanitize - exclude addresses when negative layers and sources are specified', (t) => {
121+
// select all layers except venue to simulate value of clean.layers from targets sanitizer
122+
const clean_layers = real_type_mapping.getCanonicalLayers().filter(layer => layer !== 'venue').sort();
123+
124+
let clean = { text: 'foo',
125+
layers: clean_layers,
126+
negative_layers: ['venue'],
127+
positive_layers: [],
128+
sources: ['openstreetmap', 'openaddresses','whosonfirst'],
129+
negative_sources: ['geonames'],
130+
positive_sources: []
131+
};
132+
133+
const expected_layers = clean_layers.filter(layer => layer !== 'address').sort();
134+
135+
t.deepEqual(real_sanitizer.sanitize(null, clean), STD_MESSAGES);
136+
t.deepEqual(clean.layers.sort(), expected_layers, 'layer list is reduced to exclude addresses');
137+
t.end();
138+
});
139+
120140
test('sanitize - exclude addresses when negative layers other than address are specified', (t) => {
121141
// select all layers except venue to simulate value of clean.layers from targets sanitizer
122142
const clean_layers = real_type_mapping.getCanonicalLayers().filter(layer => layer !== 'venue').sort();

0 commit comments

Comments
 (0)