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

angel-eval

v1.0.7

Published

A conditional expression parser and interpreter.

Downloads

39

Readme

angel-eval

Logo

A conditional expression parser and interpreter. This is a safe way to evaluate boolean/logical expressions without using evil eval.

The expressions follow the JavaScript syntax. You can check the comprehensive tests for examples on functionality. Variables are evaluated at a later stage when evaluate() function is called with a context object. The context provides an object with the variables to evaluate.

Quick example: evaluate("user.firstName === firstNames[0]", { user: { firstName: 'Mark' }, firstNames: ['John'] }) would return false.

Installation

npm i angel-eval

Grammar

Supported Types

  • Integer (ex: 104, ...)
  • Float (ex: 5.5, 1.2e5, ...)
  • String (ex: 'hello' or "hello" -- no backtick (``) syntax yet)

Unary Operators

  • !expression (ex: !true, !variable, !(expression && expression), ...)

Equality Operators

  • === (ex: 5 === 5, var === 5, ...)
  • !== (ex: var !== false, ...)

== and != are not supported because they are usually bad practice.

Relational Operators

  • >
  • >=
  • <
  • <=

Logical Operators

  • &&
  • ||

Grouping

Normal paranthesis grouping can be used to group multiple conditional expressions together.

evaluate("x === 4 && y === 4 || z", { x: 3, y: 5, z: "hello" }); // false
evaluate("(x === 4 && y === 4) || (z)", { x: 3, y: 5, z: "hello" }); // true

Whitespace

Whitespace is allowed where it would usually be expected to be allowed in JavaScript.

Functions

angel-eval provides two public functions:

evaluate()

The evaluate function evaluates the expression provided. The variables used are read from the context object.

const { evaluate } = require("angel-eval");

/**
 * Evaluates the given expression using the context provided.
 *
 * @param {string} expression
 * @param {{ [key: string]: any }} [context]
 * @param {boolean} [strictBoolean] Whether to always return a boolean.
 * @returns {boolean}
 */
evaluate(expression: string, context?: { [key: string]: any }, strictBoolean?: boolean) => boolean

parse()

const { parse } = require("angel-eval");

/**
 * Parses the given expression and returns an
 * `Evaluatable` that can be evaluated with a context.
 *
 * This function is memoized for optimization.
 *
 * You can access the function's cache using `parse.cache`.
 * 
 * @param {string} expression
 * @returns {Evaluatable}
 */
parse(expression: string) => Evaluatable

Examples

const { evaluate } = require("angel-eval");

evaluate("foo.x !== y", { foo: { x: 5 }, y: 5 }); // false
evaluate('gender === "female"', { gender: "female" }); // true
evaluate("5 === potato", { potato: 5 }); // true
evaluate("x <= 76 && (y > 5 || z === 'hello world')", {
  x: 76,
  y: 5,
  z: "hello world",
}); // true
evaluate("!x || y", { x: true, y: 2 }); // true
evaluate("(x || y) === 'world'", { x: false, y: "world" }); // true

Under the hood

This uses nearley to generate a parser. Variables are evaluated from the context using lodash.get.

Expression are parsed once and are memoized so that consequent operations would just evaluate and not parse those expressions again. Memoization is done on the parse() function using lodash.memoize, so if you need to reset memoization cache you can run parse.cache.clear().

Additional Thoughts

Some can say that using Function may be safer than eval, but to me both are equally dangerous since they have access to the JavaScript interpreter and Function can still see global variables and modify app state.

angel-eval's method is safe because no eval() is actually happening. Expressions cannot access any variables not specified in the context object. The expression is either parsed correctly and interpreted as needed, or it will fail. No additional code can be run through this and no changes to your app can actually happen.

lodash does come with vulnerabilities sometimes and this package uses both _.get and _.memoize, so later on it might be worth moving away from lodash (sorry lo, I still love you).

Development

This package uses Babel to build the source.

If you change the grammar, you need to rebuild it. You can do so using:

npm run build:grammar

Test

I have a comprehensive list of tests but I still would like to add more. To run current tests you can use:

npm run test 

This will not build the grammar before running. If grammar is changed, it needs to be rebuilt using npm run build:grammar.

Coverage

This project has 100% coverage. If you find the need to add more tests, contributions are greatly appreciated. Thanks!

Contributions

Any contribution is welcome and appreciated. Please create a pull request if you have changes you would like to contribute.

If you would like to report a bug, please submit an issue.

Testimonials

Thanks to Dr. Barry Wittman for introducing me to compiler work and teaching me great things. Check out his amazing programming language: Shadow.