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

uxpression

v0.1.7

Published

Universa simple expression parser

Downloads

3

Readme

Simple Expression parser

the simple and fast optimizing expression parser provides absolutely safe and effective way to incorporate runtime formulae to you program. The expressions are parased into calculation trees with constant folding, allowing fast caluclation with a given set of variable values without re-parsing it.

Features

Extremely fast calculation of once created expression. Expression constructor creates and optimizes expression tree to speed up calculations. Main usage paradigm is to create an instance and then call caclulate with necessary variables.

  • regular math, brackets
  • logic operations: true, false, &&, ||, !, !!, >=, >, <=, <<, !==, ===
  • arithmetic comparisons
  • string and boolean types.
  • constant subexpressions optimization
  • lists, list equality and inclusion

Several note on operators:

|op |operation| |---|---------| | !x | convert x to boolean and negate, e.g. !1 === false | | !!x | convert x to boolean, e.g. !!1 === true | | +x | when used as unary, converts x from string to number if need | |x + y| if x or y is string, concatenates strings, otherwise sum numbers| | comparisons | see note below | | i in list | i is in the list, see below | | i !in list | i is not in the list, same as !(i in list) |

Usage

Include it from the npm module uxpression e.g.

yarn add uxpression

or using your favorite package manager. Once you have it added:

  // import Expression it the way you prefer, like
  import { Expression } from "uxpression";

  // parse (sort of compile) expression
  const exp = new Expression("foo + bar*2");
  // detected variables:
  console.log(exp.variables);
  
  // calculate with given variables:
  expect(exp.calculate({foo: 10, bar: 20 })).toEqual(50);
  // recaclulation is very fast, no need to parse:
  expect(exp.calculate({foo: 10, bar: 10 })).toEqual(30);

Expression syntax and semantic notes

Comparison operators

Equality operators == === != !=== works for all types as in javascript, except for lists.

Operations '==' and '!=' if any operand is a list, requires that other operand be list too, and check that lists are per-element equals. Nested lists are allowed and checked as expected. To check inclusion instead use operators 'x in list' or 'x !in list'.

Comparison operators < <= >= > works only if both operands are of the same type, or will throw an exception on calculation time. Use +strValue to convert string expression to a number for comparison.

Boolean expressions can be only compared for equality, we do not impose order on them.

Boolean operators are not converted to number as for now.

Using lists

List type is just an allowed data type, that can be used in vars (use javascript Array!) or in expression using [1,2,'foo'] notation. List item is just another expression, anything, including nested lists. Lists can therefore contain variable based expressions too.

Comparing lists

Allowed operators for list arguments are == and !=.

Lists comparison works as deep equality, e.g. 2 lists are equal if and only if they have exact same in the terms of regular comparison i1 == i2. It will apply to nested lists too.

See example from the tests:

expect(new Expression("[1,2,[1,3]] == [1,2,[1,3]]").calculate()).toBeTruthy();
expect(new Expression("[1,2,[1,3]] == [1,2,x]").calculate({x: [1,3]})).toBeTruthy();

Concatenating lists

It is possible to concatenate operands ib both are lists using plus operator:

expect(new Expression("([1,2,3] + [3,2, 'foo']) == [1,2,3,3,2,'foo']").calculate())
  .toBeTruthy()

You can append or prepend a single element making a list from it: [1] + [2,3].

Check item is included in the list

There are two infix operators to check inclusion of an item in a list: x in list and x !in list. Not if the x itself is a list, it checks that all the elements of x are included in the list.

expect(new Expression("['foo', 1] in [1,5,'foo']").calculate()).toBeTruthy();
expect(new Expression("['foo', 2] !in [1,5,'foo']").calculate()).toBeTruthy();
expect(new Expression("['foo', 2] in [1,5,'foo']").calculate()).toBeFalsy();
expect(new Expression("['foo', 1] !in [1,5,'foo']").calculate()).toBeFalsy();

More information

If for some reason it is not enough, see online documentation.

Contribution

You're welcome. The rules as everywhere in the github. Make a PR. Don't forget tests and comments.

License

MIT, see the repository.