Skip to content

Commit

Permalink
Merge pull request #1529 from pelias/test-autocomplete-address-exclud…
Browse files Browse the repository at this point in the history
…e-changes

Improve performance of `max_character_count_layer_filter`
  • Loading branch information
orangejulius authored May 24, 2021
2 parents df6c868 + d1ad8f4 commit fdd3ec9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
20 changes: 14 additions & 6 deletions query/view/max_character_count_layer_filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ const peliasQuery = require('pelias-query');
const allLayers = require('../../helper/type_mapping').layers;

/**
Layer terms filter view which counts the length of 'input:name' and only
applies the filter condition if the text is shorter than or equal to $maxCharCount.
This 'filter' does not actually create a new filter query, but it modifies the 'layers' query variable
so that the default 'layers' filter will be changed.
It does this if the length of'input:name'is shorter than or equal to $maxCharCount.
In that case, the provided list of excluded layers is removed from the `layers` query variable.
You must provide a list of $excludedLayers, all layers listed in the type mapping
will be targeted, minus any listed in $excludedLayers.
Expand Down Expand Up @@ -33,8 +37,7 @@ module.exports = function( excludedLayers, maxCharCount ) {
return () => null;
}

// create a new VariableStore with only the layers property
var vsWithOnlyIncludedLayers = new peliasQuery.Vars({ 'layers': includedLayers });
// remove the excludedLayers from the `layers` variable

// ensure char count is within a reasonable range
maxCharCount = _.clamp(maxCharCount, MIN_CHAR_COUNT, MAX_CHAR_COUNT);
Expand All @@ -52,7 +55,12 @@ module.exports = function( excludedLayers, maxCharCount ) {
return null;
}

// use existing 'layers' query
return peliasQuery.view.layers(vsWithOnlyIncludedLayers);
// update layers var to not include excludedLayers
const old_layers = vs.var('layers').get() || allLayers;
const new_layers = old_layers.filter(layer => !excludedLayers.includes(layer));
vs.var('layers', new_layers);

// this 'view' doesn't render anything, it merely mutates the `layers` variable
return null;
};
};
23 changes: 11 additions & 12 deletions test/unit/query/view/max_character_count_layer_filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ module.exports.tests.view_within_range = function(test, common) {
let vs = new VariableStore();
vs.var('input:name', 'example text');

let actual = view(vs);
let expected = {
terms: {
layer: { $: _.difference(allLayers, ['address']) }
}
};
// execute the view
view(vs);

let expected =_.difference(allLayers, ['address']);
let actual = vs.var('layers').get();

t.deepLooseEqual(actual, expected, 'view_within_range');
t.end();
Expand Down Expand Up @@ -102,12 +101,12 @@ module.exports.tests.view_clamp_range_low = function(test, common) {
let vs = new VariableStore();
vs.var('input:name', 'e');

let actual = view(vs);
let expected = {
terms: {
layer: { $: _.difference(allLayers, ['address']) }
}
};
// execute the view
view(vs);

let expected =_.difference(allLayers, ['address']);
let actual = vs.var('layers').get();

t.deepLooseEqual(actual, expected, 'view_clamp_range_low');
t.end();
});
Expand Down

0 comments on commit fdd3ec9

Please sign in to comment.