bylexus / fparse

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

Using objects as values #58

Open structinfo opened 2 months ago

structinfo commented 2 months ago

It would be useful to allow passing object values into formula to process inside custom functions.

Example:

const fObj = new Formula()
fObj.distance = (point1, point2) => Math.sqrt(Math.pow(point1.x - point2.x, 2) + Math.pow(point1.y - point2.y, 2))

fObj.setFormula('distance(x,y)')
console.log(fObj.evaluate({ x: { 1, 3 }, y: { 4, 5 } }))

The example is simplified. Points can be N-dimentional etc.