Utilize aurelia expression parser in module

I have an existing aurelia 1 app - and I’m building some functionality in which I’d like to parse a user provided expression - execute it with a context/scope and get back a boolean/value.

Rather than import some other expression parser, I was wondering if it’d be possible to import and use the expression parser that aurelia uses in the binding syntax parser.

For example, user provides this JSON object:

    "skip": "rows.length === 0"

I’d like to (safely) execute it with some values and get back a result:

const shouldSkip = parse(jsonData.skip)({ rows: [] });
1 Like

Here’s an example you can based on Dumber Gist

From that, you can build a wrapper class that provide simpler call, rather than in the form of ast.evaluate()

There’s a couple things to improve in the Gist. For the street, the expression to evaluate should be

 street is: ${street}

and the binding on app.html regarding the street should be model.street and not address.street.

1 Like