formula

Math and string formula parser.

Latest Version: 3.0.0
hapi-family
Installation:

npm: npm install @hapi/formula

yarn: yarn add @hapi/formula

Module Status:
Version License Node Dependencies Travis End of Life
3.0.0
hapi helmet github logo
BSD 12 Dependency Status Build Status
1.2.0
hapi helmet github logo
BSD 12, 10, 8 Dependency Status Build Status

Example

const Formula = require('@hapi/formula');


const functions = {
    x: (value) => value + 10
};

const constants = {
    Z: 100
};

const reference = function (name) {

    return (context) => context[name];
};


const formula = new Formula.Parser('1 + a.b.c.2.4.x + [b] + x([y + 4] + Z)', { functions, constants, reference });

formula.evaluate({ 'a.b.c.2.4.x': 2, b: 3, 'y + 4': 5 });       // 1 + 2 + 3 + 5 + 10 + 100
formula.evaluate({ 'a.b.c.2.4.x': '2', b: 3, 'y + 4': '5' });   // '123510010'