Skip to content

Latest commit

 

History

History
46 lines (36 loc) · 989 Bytes

README.md

File metadata and controls

46 lines (36 loc) · 989 Bytes

fastexpr.js

A blazingly fast, single-file, zero dependency JavaScript expression parser.

Features

  • Fast. Hand-coded lexer and top-down operator precedence parser. Twice as fast as esprima.
  • Small. Less than 10KB minified and gzipped.
  • Compatible. Parses all valid JavaScript expressions. Produces an esprima style AST.

Usage:

import {parse} from '@anpete/fastexpr';

const expr = (s) => `hello from ${s}!`;

const ast = parse(expr.toString());

console.log(ast);

which produces:

{
  type: "ArrowFunctionExpression",
  params: [ { type: "IdentifierExpression", name: "s" } ],
  body: {
    type: "TemplateLiteralExpression",
    quasis: [
      {
        type: "TemplateExpression",
        value: { cooked: "hello from " },
        tail: false
      },
      {
        type: "TemplateExpression",
        value: { cooked: "!" },
        tail: true
      }
    ],
    expressions: [ { type: "IdentifierExpression", name: "s" } ]
  }
}