JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 103180
  • Score
    100M100P100Q187634F
  • License MIT

Safely evaluate JavaScript (estree) expressions, sync and async.

Package Exports

  • eval-estree-expression

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (eval-estree-expression) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

eval-estree-expression NPM version NPM monthly downloads NPM total downloads

Safely evaluate JavaScript (estree) expressions, sync and async.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

(TOC generated by verb using markdown-toc)

Install

Install with npm :

$ npm install --save eval-estree-expression

Usage

Evaluates an estree expression from [@babel/parser][], esprima, acorn, or any other library that parses and returns a valid estree expression.

const { evaluate } = require('eval-estree-expression');

evaluate(expressionTree[, context]); // async
evaluate.sync(expressionTree[, context]); // sync

See the unit tests for hundreds of additional usage examples.

Params

The evaluate function takes the following arguments:

  • expressionTree {object} - a valid estree expression AST.
  • context {object} - a data object with values to replace variables in expressions

Usage with Babel

Most of the examples in this document assume the following setup code is used:

const { evaluate } = require('eval-estree-expression');
const { parseExpression } = require('@babel/parser');

// parse your JavaScript expression
const ast = parseExpression('1 + 2);

// evaluate synchronously
console.log(evaluate.sync(ast)); //=> 3

// or asynchronously
console.log(await evaluate(ast)); //=> 3

Usage with Esprima

[Esprima][esprimar] doesn't have a "parseExpression" method like @babel/parser, so you'll need to return the expression from the AST, like so:

const { parse } = require('esprima');
const { evaluate } = require('eval-estree-expression');
const ast = parse('[1, 2, 3].map(n => n * x);').body[0].expression;

// evaluate synchronously
console.log(evaluate.sync(ast)); // =>, [2, 4, 6]

// or asynchronously
console.log(await evaluate(ast)); // =>, [2, 4, 6]

async usage

Evaluate expresssions asynchronously.

console.log(await evaluate(parse('1 + 2'))); //=> 3
console.log(await evaluate(parse('5 * 2'))); //=> 10
console.log(await evaluate(parse('1 > 2'))); //=> false
console.log(await evaluate(parse('1 < 2'))); //=> true

// with context object
console.log(await evaluate(parse('page.title === "home"'), { page: { title: 'home' } })); //=> true

sync usage

Evaluate expresssions synchronously.

console.log(evaluate.sync(parse('1 + 2'))); //=> 3
console.log(evaluate.sync(parse('5 * 2'))); //=> 10
console.log(evaluate.sync(parse('1 > 2'))); //=> false
console.log(evaluate.sync(parse('1 < 2'))); //=> true

// with context object
console.log(evaluate.sync(parse('page.title === "home"'), { page: { title: 'home' } })); //=> true

Options

booleanLogicalOperators

Type: boolean Default: undefined

Force logical operators to return a boolean result.

console.log(await evaluate(parse('a && b'), { a: undefined, b: true })); //=> undefined
console.log(await evaluate(parse('a && b'), { a: undefined, b: false })); //=> undefined
console.log(await evaluate(parse('a || b'), { a: false, b: null })); //=> null
console.log(await evaluate(parse('a || b'), { a: false, b: undefined })); //=> undefined

//
// With booleanLogicalOperators enabled
//

const options = {
  booleanLogicalOperators: true
};

console.log(await evaluate(parse('a || b'), { a: false, b: null }, options)); //=> false
console.log(await evaluate(parse('a && b'), { a: undefined, b: true }, options)); //=> false
console.log(await evaluate(parse('a && b'), { a: undefined, b: false }, options)); //=> false
console.log(await evaluate(parse('a || b'), { a: false, b: undefined }, options)); //=> false

functions

Type: boolean Default: false

Allow function calls to be evaluated. This is unsafe, please enable this option at your own risk. As an extra precaution, to evaluate functions you must also enable the generate option.

Example

See the generate docs for an example.

generate

Type: boolean Default: undefined

Pass the .generate() function from the escodegen library to enable function evalution. For functions to be evaluated you must also set the functions option to true.

Example

const { parse } = require('esprima');
const { generate } = require('escodegen');
const { evaluate } = require('eval-estree-expression');

const options = {
  functions: true,
  generate
};

console.log(await evaluate(('[1, 2, 3].map(n => n * x);'), { x: 2 }, options))); // =>, [2, 4, 6]

regexOperator

Type: boolean Default: true

In expressions, if you wish to test a value using a regular expression, you have two options:

  1. Enable function support so that you can use methods like .test() and .match(), or
  2. Use this option, which enables support for a special syntax for matching against regular expressions, without the need to enable support for evaluating functions.

In other words, instead of having to do this:

console.log(evaluate.sync(parse('/^ab+c$/ig.test("abbbbbc")'), {}, { functions: true }));

You can do this:

console.log(evaluate.sync(parse('name =~ /^a.*c$/'), { name: 'abc' }));
console.log(evaluate.sync(parse('name =~ regex'), { name: 'abc', regex: /^a.*c$/ }));

strict

Type: boolean Default: false

Throw an error when variables are undefined.

withMembers

Type: boolean Default: undefined

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

You might also be interested in these projects:

Author

Jon Schlinkert

License

Copyright © 2021, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on September 05, 2021.