A small language that compiles to Mapbox GL JS style expressions.
Playground • Guide • Operators • Grammar • Contributing
Write readable code, get back the JSON expression Mapbox expects.
var speed = get("speed")
var type = get("type")
if speed > 80 and type == "highway" then
"red"
else
"gray"
compiles to:
[
"let",
"speed",
["get", "speed"],
"type",
["get", "type"],
["case", ["all", [">", ["var", "speed"], 80], ["==", ["var", "type"], "highway"]], "red", "gray"]
]npm install mapbox-expr-langimport { compile } from "mapbox-expr-lang";
compile('get("speed") > 80');
// [">", ["get", "speed"], 80]That's the whole API: one function. It throws on invalid source, with a
message that says exactly what's wrong ('and' requires a boolean operand, but this is a number, Unknown function "typof", Variable "x" is not defined, ...) — never an internal detail you can't act on.
- Playground — type source, see the compiled expression live
- Language guide — a short tour of the language
- Operator reference — every operator, linked to its page on docs.mapbox.com
- Full grammar — the formal EBNF grammar
Contributions of any size are welcome — a typo fix, a missing operator, a clearer error message.
git clone https://github.com/amine-a11/mapbox-expr-lang.git
cd mapbox-expr-lang
npm install
npm run testSee CONTRIBUTING.md for the full guide (project layout, available scripts, and what a PR should look like before it's opened).
