TomFrost / Jexl

Javascript Expression Language: Powerful context-based expression parser and evaluator
MIT License
561 stars 92 forks source link

how to make a function call in expression #76

Closed wasimshariff closed 4 years ago

wasimshariff commented 4 years ago

H Tom,

Thanks a ton for such a great library!!!

I am trying to do something similar to one given below.

const context = { name: { first: 'Sterling', last: 'Archer' } }

jexl.eval('name.first.toUpperCase()', context)

My use case is , i get the expression from some other source dynamically. I have few prototype methods that i need to execute as part of expression. Is there a way to do this ?

Right now, above snippet returns an error while trying to call toUpperCase on string .

Thank you.

TomFrost commented 4 years ago

Hi there, glad you're enjoying Jexl!

You can absolutely do that -- you just need to change to the transform syntax. First, create the "toUpperCase" transform like this (you only need to do this once):

jexl.addTransform('toUpperCase', (str) => str.toUpperCase())

And now you can use that transform like this:

jexl.evalSync('name.first|toUpperCase', context)

Done! Let me know how that works out.

wasimshariff commented 4 years ago

It works...Thank you Tom!!!!