1.0.19 • Published 6 months ago

formula-interpreter v1.0.19

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

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)

ParameterTypeDescription
evalStringstringA string containing the JavaScript code to evaluate.
librariesarrayAn array of library names to load from the libraries directory.
EXTERNAL_VARobjectAn 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:

ParameterTypeDescription
librarystringThe name of the library to load. If null, all libraries are loaded.
propertyNamestringThe specific property or function within the library to retrieve. If null, all properties will be retrieved.
allAtOncebooleanA 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.

FunctionDescriptionParametersExample
IF(condition, trueValue, falseValue)Evaluates a condition and returns either trueValue or falseValue based on the condition.condition, trueValue, falseValuereturn IF(1 < 2, 'Yes', 'No') → Output: Yes
AND(...args)Evaluates multiple boolean expressions. Returns true if all conditions are true....argsreturn AND(1 === 1, 2 === 2) → Output: true
OR(...args)Evaluates multiple boolean expressions. Returns true if any condition is true....argsreturn OR(1 === 2, 2 === 2) → Output: true
XOR(...args)Evaluates multiple boolean expressions. Returns true if an odd number of conditions are true....argsreturn 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.

FunctionDescriptionParametersExample
CONCATSTR(...args)Concatenates multiple strings into a single string....argsreturn CONCATSTR('Hello, ', 'world!', ' How are you?'); → Output: Hello, world! How are you?
STRLENGTH(str)Returns the length of the given string.strreturn 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.

FunctionDescriptionParametersExample
SUM(...numbers)Returns the sum of all given numbers....numbersreturn SUM(1, 2, 3, 4); → Output: 10
ROUND(number, numDigits)Rounds the given number to the specified number of digits.number, numDigitsreturn ROUND(3.14159, 2); → Output: 3.14
1.0.19

6 months ago

1.0.18

7 months ago

1.0.17

7 months ago

1.0.16

7 months ago

1.0.15

7 months ago

1.0.14

8 months ago

1.0.13

8 months ago

1.0.12

10 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago