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

formula-interpreter

v1.0.13

Published

A lightweight JavaScript library for parsing and evaluating mathematical and logical expressions. This package acts as a formula interpreter, allowing users to input complex formulas as strings and compute their results. It supports basic arithmetic opera

Downloads

94

Readme

Node.js Interpreter

This Node.js project provides a flexible interpreter for evaluating JavaScript strings. It supports dynamic function parsing and can load external libraries based on user-specified configurations. The project is particularly useful for executing JavaScript code snippets with customized logic.

Real-Life Example: Dynamic Formula Evaluation

Consider a financial reporting application that relies on a table of formulas to calculate various metrics. These formulas are subject to change based on business needs, often every 6-7 days. Using the Node.js Interpreter, the application can easily adapt to these changes without requiring extensive reprogramming.

Scenario

  • Formula Table: | Day | Metric | Formula | |-------------|----------------|-----------------------------| | Day 1 | Total Revenue | SUM(sales) + BONUS | | Day 7 | Total Revenue | SUM(sales) * 1.1 | | Day 14 | Total Revenue | SUM(sales) - EXPENSES | | Day 21 | Total Revenue | SUM(sales) / 1.2 | | Day 28 | Total Revenue | SUM(sales) + TAX |

  • How It Works: Each time the formula changes, the financial reporting application can call the interpretor function to evaluate the new formula based on the current data.

Example Implementations

  1. Initial Formula (Day 1): For the initial formula, the application uses an add formula with sales data:

    const evalString = "return SUM(sales) + BONUS;";
    const result = interpretor(evalString, [], { sales: [100, 200, 300], BONUS: 50 });
    console.log(result); // Output: 650
    
    

Table of Contents

Features

  • Library Loader: Loads required libraries dynamically to extend the functionality.
  • Payload Support: Allows passing external arguments (payload) for evaluation within the JavaScript string.
  • Run pure Javascript: In interpreter, you can run pure js code and logical operator here.

Installation

npm i formula-interpreter

Project Overview

This project exposes functionalities to evaluate JavaScript code dynamically and allows users to manipulate data through a set of predefined libraries. It is designed to enable users who may not be proficient in JavaScript to still leverage its capabilities through simple function calls.

Usage

The project exposes two primary functions:

1. interpretor(evalString, libraries, payload)

| Parameter | Type | Description | |---------------|----------------|--------------------------------------------------------------| | evalString | string | A string containing the JavaScript code to evaluate. | | libraries | array | An array of library names to load from the libraries directory. | | EXTERNAL_VAR| object | An object containing values to pass into the evaluated code. |

Example:

const evalString = "return 1 + 1";
const result = interpretor(evalString);
console.log(result); // Output: 2

2. showLibAndProperties(library, propertyName, allAtOnce)

This function retrieves the available libraries and their properties, allowing users to inspect the functionalities that can be utilized in their JavaScript evaluations.

Parameters:

| Parameter | Type | Description | |-----------------|----------|--------------------------------------------------------------| | library | string | The name of the library to load. If null, all libraries are loaded. | | propertyName | string | The specific property or function within the library to retrieve. If null, all properties will be retrieved. | | allAtOnce | boolean| A flag indicating whether to load all libraries and their properties at once. If true, all libraries will be included in the response. |

Example Usage:

  1. Retrieving a Specific Property:

For specific response.

const libraryInfo = showLibAndProperties('myLibrary', 'myFunction', false);

For all libraries, functions and their properties at once.

const libraryInfo = showLibAndProperties(null, null, true);

LIBRARIES

LOGICAL

This library provides functions for evaluating logical conditions and expressions.

| Function | Description | Parameters | Example | |--------------------------------|---------------------------------------------------------------------------|------------------------------------------|---------------------------------------------------------| | IF(condition, trueValue, falseValue) | Evaluates a condition and returns either trueValue or falseValue based on the condition. | condition, trueValue, falseValue | return IF(1 < 2, 'Yes', 'No') → Output: Yes | | AND(...args) | Evaluates multiple boolean expressions. Returns true if all conditions are true. | ...args | return AND(1 === 1, 2 === 2) → Output: true | | OR(...args) | Evaluates multiple boolean expressions. Returns true if any condition is true. | ...args | return OR(1 === 2, 2 === 2) → Output: true | | XOR(...args) | Evaluates multiple boolean expressions. Returns true if an odd number of conditions are true. | ...args | return XOR(true, false, true) → Output: true |


This table provides a clear overview of the functions available in the LOGICAL library, along with their purpose, parameters, and usage examples.

STRING

This library provides functions for manipulating strings.

| Function | Description | Parameters | Example | |-----------------------------|-----------------------------------------------------|---------------------|--------------------------------------------------------| | CONCATSTR(...args) | Concatenates multiple strings into a single string. | ...args | return CONCATSTR('Hello, ', 'world!', ' How are you?'); → Output: Hello, world! How are you? | | STRLENGTH(str) | Returns the length of the given string. | str | return STRLENGTH('Hello, world!'); → Output: 13 |


This table provides a clear overview of the functions available in the STRING library, along with their purpose, parameters, and usage examples.

Math Functions

This library provides functions for performing mathematical operations.

| Function | Description | Parameters | Example | |-----------------------------|-----------------------------------------------------------|-----------------------------------|-------------------------------------------------------------| | SUM(...numbers) | Returns the sum of all given numbers. | ...numbers | return SUM(1, 2, 3, 4); → Output: 10 | | ROUND(number, numDigits) | Rounds the given number to the specified number of digits. | number, numDigits | return ROUND(3.14159, 2); → Output: 3.14 |