TomFrost / Jexl

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

Best way for my transformer functions to access context #9

Closed khowling closed 8 years ago

khowling commented 8 years ago

I need to access req.user in my transformers, what is the best way to do this? I love this project by the way, thank you!

mythmon commented 8 years ago

In my project where I'm using jexl, we require content like that to be passed into the transform, either as the subject or one of the arguments.

TomFrost commented 8 years ago

To allow for this in a non-breaking change, I could send the expression's context object as either the final argument to the transform, or set it to this inside of the transform function.

I'm torn, as I don't particularly love either of these solutions. The argument would have inconsistent placement depending on how many arguments the user sends to the transform, and setting the this execution context would preclude access to the local scope when using an arrow function.

Will chew on this a bit more-- in the meantime, I'm very open to implementation ideas.

TomFrost commented 8 years ago

Thought about this for a bit, and I'm curious: why not send the arguments your transform needs to the transform when you call it?

jexl.eval("someVar|myTransform(req.user)", {someVar, req})
  .then(...)
TomFrost commented 8 years ago

Closing in favor of using arguments to support this use case, but if anyone can make a strong case for setting this in the transform function to the context object, I'd love to hear it. My only reason for not doing that at the moment is to avoid potential ugly misuse of transform functions.