TomFrost / Jexl

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

escape dash #58

Closed gunzip closed 5 years ago

gunzip commented 5 years ago

hi. is there any way to escape the dash (minus sign) character ?

jexl
  .eval('foo-bar == "foobar"', {
    "foo-bar": "foobar"
  })
  .then(console.log)
  .catch(console.error);

results false

TomFrost commented 5 years ago

Jexl is very similar to Javascript in that top-level variables in the context cannot contain dashes, start with a number, etc. But it's also similar to javascript in that, if it's not at the top level, you can quote it:

const context = { baz: { 'foo-bar': 'foobar' } }
const res = jexl.evalSync('baz["foo-bar"] == "foobar"', context)
console.log(res) // true

Hope this helps!

TomFrost commented 5 years ago

Closing until this need is articulated again :)