TomFrost / Jexl

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

call functions with namespace #96

Open seocochan opened 3 years ago

seocochan commented 3 years ago

Hi Tom

I'm currently working on implementing simple rules engines and considering jexl as a DSL to adopt.

I found out that some implementations like this provides calling methods with namespace(class names)

So I tried some expressions like FooService.doSomething() or FooService#doSomething()on jexl but it fails.

here is the code I tried on runkit

const jexl = require("jexl")

const input = {
  order: {
    total: 100000,
  },
  coupon: {
    status: 'ACTIVE'
  },
}

jexl.addFunction('OrderService.discountOrder', (order, rate) => order.total = order.total * (1.0 - rate))

const rules = [
  `OrderService.discountOrder(order, 0.2)`, // mutate passed object
]

const results = await Promise.all(rules.map(rule => jexl.eval(rule, input)))

console.log(results)
console.log(input.order)

I guess there're some reserved tokens like defined on grammar.js but is there any way to avoid these behavior or any suggestions to achieve my point?

Thanks.