Skip to the content.

mapbox-expr-lang — write expressions naturally, compile them into Mapbox expressions

mapbox-expr-lang

A small language that compiles to Mapbox GL JS style expressions.

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"]
]

Install

npm install mapbox-expr-lang
import { compile } from "mapbox-expr-lang";

compile('get("speed") > 80');
// [">", ["get", "speed"], 80]

That’s the whole API: one function, compile(source). It throws on invalid source with a message that says exactly what’s wrong, never an internal detail you can’t act on.

Where to go next