TomFrost / Jexl

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

Keys with hyphens evaluate to null #132

Closed TylerMizuyabu closed 1 year ago

TylerMizuyabu commented 1 year ago

Trying to use jexl to evaluate boolean expressions where the keys used in the expression can have hyphens in them Example: task-a && task-b, however when I pass it a context object like this {"task-a": true, "task-b": true} the expression evaluates to null. Even the simple expression of task-a evaluates to null. It seems to be an issue with the hyphen in the key because the expressions taskA && taskB and taskA evaluates properly. Currently I'm doing a work around where I nest these values one level deeper in the context object like this { tasks: { "task-a" : true, "task-b": true } } and then I'm performing a replace() call that changes the expression to tasks["task-a"] && tasks["task-b"]. But this seems like a bug. If it's intended or my use of the expression is just wrong please let me know

TomFrost commented 1 year ago

Hey Tyler! I'm afraid that's as-designed. If your top level keys have a hyphen in them, Jexl interprets the hyphen as a minus sign just like most programming languages. You hit on both the workarounds I'd recommend: Nesting those keys a layer deep so you can reference them in quotes, or converting them to camelcase.

Best of luck!