Home

Awesome

@mapbox/expression-jamsession

Build Status

Write Mapbox GL expressions in a more familiar, handwritable, spreadsheet-like, programming-like syntax. This library translates these handwritten formulas into valid spec-compliant Mapbox GL expressions that you can use in a Mapbox style.

Formula syntax features

// Input
2 + 2

// Output
["+", 2, 2]
// Input
max(3, log2(6))

// Output
["max", 3, ["log2", 6]]
// Input
((3 + get("num")) * 2) / 7

// Output
["/", ["*", ["+", 3, get("num")], 2], 7]
// Input
"name: " & get("name")

// Output
["concat", ["name ", ["get", "name"]]]

Usage

The module exports two functions so you can transform in both directions:

import jamsession from '@mapbox/expression-jamsession';

jamsession.formulaToExpression("3 + 4"); // ["+", 3, 4]

jamsession.expressionToFormula(["+", 3, 4]); // "3 + 4"

Browser compatibility

This library should work in IE11+. It uses a Set, so you might get it working in older browsers by adding a polyfill.

Caveats