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/6ad0dad591858d9c15c3ec98e2857084 to your computer and use it in GitHub Desktop.
Save tmcw/6ad0dad591858d9c15c3ec98e2857084 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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment