bylexus / fparse

A JavaScript Formula Parser
http://fparser.alexi.ch/
MIT License
89 stars 15 forks source link

Support for binary operations ? #27

Closed Necmttn closed 2 years ago

Necmttn commented 2 years ago

I would like to use fparse to create binary cases to execute like

x + y < z
x > y
x >= z
x + y + z = 10
etc 

is there a way to extend this functionality?

bylexus commented 2 years ago

Those are boolean expressions, which do not evaluate in a number result. You can however register your own functions to achieve this, e.g. like this:

const fObj = new Formula('lessThan(x+y, z)'); // representing x + y < z
fObj.lessThan = (a, b) => a < b ? 1 : 0;
let result = fObj.evaluate({x: 1, y: 2, z: 4}); // => 1
bylexus commented 2 years ago

Docu Pointer: https://fparse.alexi.ch/#using-user-defined-functions

Necmttn commented 2 years ago

gotcha, thank you