From 7ddbfa2d374d1bab63abbb6ec405ba751895164e Mon Sep 17 00:00:00 2001 From: Eli Fitch Date: Thu, 6 Dec 2018 13:36:13 -0500 Subject: [PATCH] Flattens nested single element all expressions when converting to expressions (#7679) --- src/style-spec/feature_filter/convert.js | 2 +- test/unit/style-spec/feature_filter.test.js | 39 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/style-spec/feature_filter/convert.js b/src/style-spec/feature_filter/convert.js index 574c64779d1..1998eabc87a 100644 --- a/src/style-spec/feature_filter/convert.js +++ b/src/style-spec/feature_filter/convert.js @@ -92,7 +92,7 @@ function _convertFilter(filter: FilterSpecification, expectedTypes: ExpectedType return ['any'].concat(children); } else if (op === 'all') { const children = (filter: any).slice(1).map(f => _convertFilter(f, expectedTypes)); - return ['all'].concat(children); + return children.length > 1 ? ['all'].concat(children) : [].concat(...children); } else if (op === 'none') { return ['!', _convertFilter(['any'].concat(filter.slice(1)), {})]; } else if (op === 'in') { diff --git a/test/unit/style-spec/feature_filter.test.js b/test/unit/style-spec/feature_filter.test.js index d1624ca4487..6621b2dd713 100644 --- a/test/unit/style-spec/feature_filter.test.js +++ b/test/unit/style-spec/feature_filter.test.js @@ -100,6 +100,45 @@ test('convert legacy filters to expressions', t => { t.end(); }); + t.test('flattens nested, single child all expressions', (t) => { + const filter = [ + "all", + [ + "in", + "$type", + "Polygon", + "LineString", + "Point" + ], + [ + "all", + ["in", "type", "island"] + ] + ]; + + const expected = [ + "all", + [ + "match", + ["geometry-type"], + ["Polygon", "LineString", "Point"], + true, + false + ], + [ + "match", + ["get", "type"], + ["island"], + true, + false + ] + ]; + + const converted = convertFilter(filter); + t.same(converted, expected); + t.end(); + }); + t.end(); });