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

@momsfriendlydevco/eval

v1.0.0

Published

Safe evaluation of JavaScript(ish) expressions

Downloads

3

Readme

@momsfriendlydevco/eval

Safe expression evaluator.

This function is an implementation of the Shunting-Yard Alorithm which parses a regular JavaScript expression, runs it though a Reverse Polish Notation parser and returns a result.

The output of this function is the evaluated result of the line, along with any optionally supplied context.

Examples

var eval = require('@momsfriendlydevco/eval');

// Simple evaluations
eval('1'); //= 1
eval('1 + 1'); //= 2
eval('"a" + "b" + "c"'); //= "abc"


// Using an optional context
eval('foo + bar', {foo: 1, bar: 2}); //= 3
eval('foo(bar)', {foo: a => a, bar: 10}); //= 10
eval('add(10, 20)', {add: (a, b) => a + b}); //= 30

API

eval(expression, context, options)

Evaluate a JavaScript style expression and return the result. Context is an object of optional variables to allow access to or functions to support. Default options can also be set in eval.defaults.

Options are:

| Option | Type | Default | Description | |--------------|-------------------|----------|--------------------------------------------------------------------------------------------------------------------------| | re | Object | See code | List of Regular expressions used to parse the expression | | re.operand | Boolean or RegExp | false | The regular expression used to parse operands, if specifying your own operands this needs to be falsy to force recompile | | operands | Object | See code | List of Operands to support |

eval.defaults

Object of default values to use when calling eval().

eval.get(target, path)

Internal function to retrieve a dotted notation path in an expression. Similar to the Lodash Get function.

eval.compileOperands(operands)

Compiler to return the optimized Regular Expression of the operands structure. Called automatically if settings.re.operands is falsy or on initalization of the module. Invoke this function manually and stash the result in defaults if defining a custom operand chain.

eval.defaults.re.operand = eval.compileOperands(eval.defaults.operands);

Why?

There are a number of reasons this module is necessary instead of using plain eval():

  1. Its safe - Its eval, have you used it? Its not meant to be used for anything. Eval is massivly unsafe and unless used exactly right can cause major harm
  2. Babel safe - This module does not use any JS parsing, it uses its own internal lexing + parsing engine
  3. Context is sandboxed - Only exposed variables and functions are accessible within an expression, nothing else is accepted
  4. Implements only a subset of the JS stanard - Advanced JS concepts like Classes and Assignments are configurable (eventually - see the Todo list)

Todo

Lexical features of JavaScript and their support:

| Precedence | Type | Syntax | Supported | |------------|------------------------|------------------------------------------|-----------| | 21 | Parenthesis | (, ) | Yes | | 20 | Member access | a.b.c | Yes | | 20 | New with arg list | new A() | | | 20 | Functions | a(b, c) | Yes | | 18 | Postfixes | a--, b++ | | | 17 | Logical NOT | !a | | | 17 | Bitwise NOT | ~a | | | 17 | Unary plus | +a | | | 17 | Unary negation | -a | | | 17 | Typeof | typeof a | | | 17 | Void | void | | | 17 | Delete | delete a | | | 17 | Await | await a() | | | 16 | Exponentials | a ** b | | | 15 | Multiplication | a * b | Yes | | 15 | Division | a / b | Yes | | 15 | Remainder | a % b | Yes | | 14 | Addition | a + b | Yes | | 14 | Subtraction | a - b | Yes | | 13 | Bitwise shift | a << b, a >> b, a >>> b | | | 12 | Lower than (or equal) | a < b, a <= b | Yes | | 12 | Higher than (or equal) | a > b, a >= b | Yes | | 12 | In | a in b | | | 12 | Instance of | a instanceof b | | | 11 | (In)Equality | a == b, a != b, a === b, a !== b | Yes | | 10 | Bitwise AND | a & b | | | 9 | Bitwise XOR | a ^ b | | | 8 | Bitwise OR | a \| b | | | 7 | Nullish coalescing | a ?? b | Yes | | 6 | Logical AND | a && b | Yes | | 5 | Logical OR | a || b | Yes | | 4 | Ternary | a ? b : c | | | 3 | Assignment | a = b | | | 2 | Yield | yield a or yield* a | | | 1 | Comma lists | a, b, c | |