Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created May 2, 2016 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/810cf787bb47b33e5606c1b49f64ce98 to your computer and use it in GitHub Desktop.
Save tmcw/810cf787bb47b33e5606c1b49f64ce98 to your computer and use it in GitHub Desktop.
var filter = [
"all",
[ "in", "$type", "Polygon", "LineString", "Point"],
[ "all",
["in", "type", "town", "city"],
["==", "class", "street_limited"],
[ "<=", "scalerank", 2]
]
]
var filter2 = [
"all",
[ "in", "$type", "Polygon", "Point", "LineString"],
[ "all",
["in", "type", "city", "town"],
["==", "class", "street_limited"],
[ "<=", "scalerank", 2]
]
]
var filter3 = [
"all",
[ "all",
["in", "type", "city", "town"],
["==", "class", "street_limited"],
[ "<=", "scalerank", 2]
],
[ "in", "$type", "Polygon", "Point", "LineString"]
]
var filter4 = [
"all",
[ "all",
["==", "class", "street_limited"],
["in", "type", "city", "town"],
[ "<=", "scalerank", 2]
],
[ "in", "$type", "Polygon", "Point", "LineString"]
]
var filter5 = [
"all",
[ "all",
["==", "class", "street_limited"],
["in", "type", "city", "town"],
[ "<=", "scalerank", 2]
],
[ "in", "$type", "Polygon", "Point", "LineString"]
]
function sortLevel(list) {
if (!Array.isArray(list)) {
return list;
} else if (list[0] == 'any' || list[0] == 'all' || list[0] == 'none') {
return list.slice(0, 1).concat(list.slice(1).map(sortLevel).sort());
} else {
return list.slice(0, 2).concat(list.slice(2).map(sortLevel).sort());
}
}
console.log(sortLevel(filter));
console.log(sortLevel(filter2));
console.log(sortLevel(filter3));
console.log(sortLevel(filter4));
@scothis
Copy link

scothis commented May 3, 2016

Is there a reliable source of metadata for which filter expressions are comparing against a property vs a set? It would be nice to not hard code 'any', 'all' and 'none'. (I don't think this metadata exists currently, we should create it)

Properties values inside set operations are required to be operations themselves (arrays). While non-set operations are individual values (non-arrays). The .map(sortLevel) on line 57 is unnessesary.

It would be nice to have this method inside the style spec so it can grow and evolve with the spec.

@tmcw
Copy link
Author

tmcw commented May 3, 2016

The filter operator list is just a list, doesn't divide into set vs property https://github.com/mapbox/mapbox-gl-style-spec/blob/mb-pages/reference/v8.json#L790

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment