Skip to content

Commit

Permalink
Fix #8504 remove duplicate match exp from filter conversion (#8542)
Browse files Browse the repository at this point in the history
* Demonstrate filter conversion outputting invalid match expression

* Filter duplicate values when outputting match expressions

* Use a sorting method less likely to blow up at big O

* Tighten up sort expression
  • Loading branch information
Derek Lieu authored Jul 25, 2019
1 parent ff3210a commit e309d47
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/style-spec/feature_filter/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ function convertInOp(property: string, values: Array<any>, negate = false) {
}

if (uniformTypes && (type === 'string' || type === 'number')) {
return ['match', get, values, !negate, negate];
// Match expressions must have unique values.
const uniqueValues = values.sort().filter((v, i) => i === 0 || values[i - 1] !== v);
return ['match', get, uniqueValues, !negate, negate];
}

return [ negate ? 'all' : 'any' ].concat(
Expand Down
26 changes: 25 additions & 1 deletion test/unit/style-spec/feature_filter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ test('convert legacy filters to expressions', t => {
[
"match",
["geometry-type"],
["Polygon", "LineString", "Point"],
["LineString", "Point", "Polygon"],
true,
false
],
Expand All @@ -139,6 +139,30 @@ test('convert legacy filters to expressions', t => {
t.end();
});

t.test('removes duplicates when outputting match expressions', (t) => {
const filter = [
"in",
"$id",
1,
2,
3,
2,
1
];

const expected = [
"match",
["id"],
[1, 2, 3],
true,
false
];

const converted = convertFilter(filter);
t.same(converted, expected);
t.end();
});

t.end();
});

Expand Down

0 comments on commit e309d47

Please sign in to comment.