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

expr-tree

v0.1.6

Published

An expression tree system for node.js. This library provides methods to parse Reverse Polish Notation (RPN) and useful functions to analyse and work with expression trees.

Downloads

2

Readme

expr-tree

expr-tree is an expression tree system for node.js. This library provides methods to parse reverse polish notation (RPN) and useful functions to work with expression trees. This module is available on npm as expr-tree.

install

If you're using node.js and npm, type into a terminal :

$ npm install expr-tree --save

If you're using the browser, add to the beginning of your file:

<script src="expr-tree.min.js"></script>

example

var expr = require('expr-tree')

expr.compute('5 7 * 7 +') // == 42
expr.toFunction('x y +')(10, 10) // == 20

custom operators

To add custom operators to expr-tree, just add an 'operator object' to expr.operators :

var expr = require('expr-tree')

// adding the operator
expr.operators['myCustomOperator'] = {
  // the function called to compute the value of this operator.
  // the nth argument is written as '{n}'
  js: 'myCustomOperator({0}, {1}, ..., {n})',
  //  the number of arguments taken by the operator
  n: number_arguments
}

api

The tree structure allow for the use of t-js. An expression tree node has the following structure :

var tree = {
  label: '...',                // string (contains the token used in RPN)
  type: 'func|var|number',     // string (depends on the type of the node/leaf)
  parent: parent,              // tree (can be undefined if tree is root)
  children: [child1, ...]      // array of trees
}

All the methods dealing with expressions contained in this library can take interchangeably a tree or the reverse polish notation (RPN) of an expression tree. They always output a tree : if you want a method to output RPN, wrap your calls inside expr.toRPN(tree).

The following methods are available:

fromRPN

var tree = expr.fromRPN(rpn)

Returns the expression tree described by the given reverse polish notation (RPN).

The complexity is O(n), with n the number of tokens in the RPN.

toRPN

var rpn = expr.toRPN(tree)

Returns the RPN describing the given expression tree

The complexity is O(n), with n the number of nodes/leaves in the tree.

toFunction

var func = expr.toFunction(expression, args)

Returns the function represented by the given expression. args must be an array of expression variables names. If args is null or unspecified, the function will only take the minimal number of arguments it needs to compute the expression, sorted by lexicographical order. If args is not null, the function will take the minimal number of arguments it needs to compute the expression, plus the ones specified in args

The complexity is O(n), with n the number of nodes/leaves in the tree, or the number of tokens in the RPN.

isExpressionTree

expr.isExpressionTree(expression)

Returns true if the expression is a valid RPN or a valid tree.

The complexity is O(n), with n the number of nodes/leaves in the tree, or the number of tokens in the RPN.

simplify

var tree = expr.simplify(expression, vars)

Simplify an expression by replacing some of the variables inside by their value. The value of the variable varname in the expression is given by vars[varname].

The complexity is O(n), with n the number of nodes/leaves in the tree, or the number of tokens in the RPN

reduce

var tree = expr.reduce(expression)

Simplify an expression by computing all the constants. For instance, '7 6 * x +' will be reduced to '42 x +'.

This function will always return a tree. To compute the result of an expression given all its variables, see expr.compute(expression, vars)

The complexity is O(n), with n the number of nodes/leaves in the tree, or the number of tokens in the RPN

compute

var tree = expr.compute(expression, vars)

Simplify an expression by computing all the constants. For instance, '7 5 * 7 +' will be reduced to '42'.

Warning : This function will always return a tree. To compute the result of an expression given all its variables, see expr.compute(expression, vars)

The complexity is O(n), with n the number of nodes/leaves in the tree, or the number of tokens in the RPN

copy

var tree = expr.copy(expression)

Returns a deep copy of the expression.

The complexity is O(n), with n the number of nodes/leaves in the tree, or the number of tokens in the RPN

mutate

var tree = expr.mutate(expression, pick)

Mutate an expression by randomly changing one node/leaf label. This function is mainly used in genetic algorithms. You must provide as an argument a pick function, according to the following :

  • pick must take a String as its first argument
  • pick must return a number of your choosing if its first argument equals 'number'
  • pick must return a variable name of your choosing if its first argument equals 'var'

The complexity is O(n), with n the number of nodes/leaves in the tree, or the number of tokens in the RPN

reproduce

var tree = expr.reproduce(sup, sub) // sup and sub can be trees or RPN

Returns a duplicate of sup, with one of its subtrees randomly swapped with a subtree from sub. This function is mainly used in genetic algorithms.

The complexity is O(n+m), with n (respectively m) the number of nodes/leaves in sup (resp. sub)

crossover

var tree = expr.crossover(expression)

Returns a duplicate of the expression, with two of its subtrees swapped

The complexity is O(n), with n the number of nodes/leaves in the tree, or the number of tokens in the RPN

release History

  • 0.1.0 Initial release
    • 0.1.1 Minor changes (removed eval)
    • 0.1.2 Added browser support

license

MIT