npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mapbox/expression-jamsession

v0.5.0

Published

Write Mapbox GL expressions in a more familiar, handwritable, spreadsheet-like, programming-like syntax.

Downloads

3,254

Readme

@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

  • Most expressions are represented like function invocations in programming, e.g. get("population"), log2(get("population")).
  • Symbolic math operators (+ - * / %) and parentheses work like in high school math, e.g. ((3 + 4) * 2) / 7. That is, the formula should contain 3 + 4 instead of +(3, 4).
  • Symbolic decision operators also work with operands on both sides, instead of like function invocations. So get("foo") != 4 instead of !=(get("foo"), 4).
  • Strings must always be wrapped in quotation marks, e.g. concat("egg", "s") not concat(egg, s).
  • & operator concatenates strings, as in spreadsheet programs.
// 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:

  • formulaToExpression transforms (string) formulas to (array) expressions.
  • expressionToFormula transforms expressions to formulas.
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

  • You can use this library to create expressions that are syntactically acceptable but invalid as Mapbox GL expressions, e.g. get(true) outputs ["get", true], which fails.
  • You cannot use JSON object literal arguments to the literal expression. This is allowed in the spec; but objects are not supported by jsep and the use case for this type of expression is kind of an edge case — so it's probably not worth trying to adjust the parser to support this edge case. If you disagree, please consider filing an issue and/or PR.