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

@jaakkohurtta/parsajs

v2.0.0

Published

Library to parse and evaluate arithmetic expressions without eval()

Downloads

2

Readme

ParsaJS

https://parsajs-demo.netlify.app/

Library to parse and evaluate arithmetic expressions without eval()

Supports basic operators [ ** * / % + - ( ) ] and variables.

Order of operations: BEMDAS

Usage

$ npm i @jaakkohurtta/parsajs
import Parsa from "@jaakkohurtta/parsajs"
const myParser = new Parsa()

let parse = myParser.parse('a*(20/8)*b,{"a":3,"b":2}')
let evaluation = myParser.evaluateAll()

console.log(evaluation)
	
// Log:
//	{
//	  complete: true,
//	  eval: 15, 
//	  items: [{ "15", "number" }]
//	  blocks: [{ id: 1, prio: 0, startIndex: 0, endIndex: 0 }],
//	}

Methods

parse(string)

Parses an input string for evaluation

Params: a string
Returns an object: 
	{ 
  success: boolean,
	  msg: string, 
	}

evaluateAll()

Evaluates the expression string

Returns an object: 
	{ 
	  blocks: [{ ... }], 
	  complete: boolean (true), 
	  eval: number, 
	  items: [{ ... }] 
	}

evaluateNext()

Evaluates the next block according to order of operations

Returns an object: 
	{ 
	  blocks: [{ ... }], 
	  complete: boolean (false), 
	  eval: number, 
	  items: [{ ... }] 
	}

Getters

myParser.getSourceString

Returns the original input string

myParser.getItems

Returns an array of parsed expression items

myParser.getNextBlockId

Returns the id of the next block to be evaluated

myParser.getBlocks

Returns an array of parsed items organized in blocks

Tests

$ npm run test

PASS  tests/parsa.test.ts
  Parser tests
    ✓ Simple expression parsed correctly (3 ms)
    ✓ Intermediate expression parsed correctly (1 ms)
    ✓ Complex expression parsed correctly (1 ms)
    ✓ Expressiong with variables is parsed correctly (3 ms)
    ✓ Parsing fails if expression string containains NaNs
    ✓ Parsing fails with if expression starts with operator
    ✓ Parsing fails with if expression ends with operator
    ✓ Parsing fails with if an operator is misplaced (double operator)
    ✓ Parsing fails with odd number of parenthesis (1 ms)
  Eval tests
    ✓ Simple expression is evaluated correctly (1 ms)
    ✓ evaluateNext() works correctly (1 ms)
    ✓ Complex expression with variables is evaluated correctly (1 ms)
  Getters
    ✓ getSourceString returns correct value
    ✓ getItems returns correct values (1 ms)
    ✓ getBlocks returns correct values
    ✓ getNextBlockId returns correct value (1 ms)

Test Suites: 1 passed, 1 total
Tests:       16 passed, 16 total
Snapshots:   0 total
Time:        1.572 s, estimated 2 s

License: MIT