aurelia / templating-binding

An implementation of the templating engine's Binding Language abstraction which uses a pluggable command syntax.
MIT License
32 stars 26 forks source link

Support binding RegExp literals #77

Closed davismj closed 6 years ago

davismj commented 8 years ago

I have a filter list value converter and I want to pass in a regexp literal:

/[^anythingbutthis]/i

This is currently a syntax error.

ERROR [app-router] Error: Parser Error: Unexpected token / at column 39 in [submission.samples | filter: { value: /[^anythingbutthis]/i }](…)
EisenbergEffect commented 8 years ago

Perhaps put it in a string and then convert it to a RegEx inside your converter? I have a feeling this would be a major task to implement. @jdanyow Any thoughts?

davismj commented 8 years ago

I did that. It would just be a really nice to have, I think.

For the record here's my filter code:

const REMATCH = /\/(.*)\/(\w*)/;

export class FilterValueConverter {
  toView(array, filter) {
    let keys = Object.keys(filter).filter((key) => filter[key]);
    if (!keys.length || keys.every((key) => filter[key] == null)) {
      return array;
    }
    return array.filter((item) => {
      return keys.some((key) => {

        let re = filter[key] || '';
        let match = REMATCH.exec(re);
        if (match) {
          re = new RegExp(match[1], match[2]);
        } else {
          re = new RegExp(re, 'i');
        }

        let value = item[key] || '';
        return re.test(value);
      });
    });
  }
}
jdanyow commented 8 years ago

It might be possible to add logic to the parser for this. I'm not sure how much trouble that will be. This section in the grammar worries me a little bit, but may be a non-issue:

grammar http://www.ecma-international.org/ecma-262/5.1/#sec-7

It may be as easy as adding another condition here and a RegExLiteral class to the AST. Will experiment when I get a chance. @davismj if you want to play around with it, I can try and answer any questions.

EisenbergEffect commented 8 years ago

I'd say skip it for now. We can consider it again after 1.0. I think there are more important things to tackle in the mean time, esp. since this isn't a show stopper.

Alexander-Taran commented 6 years ago

Stale since 2016 interesting use case though

EisenbergEffect commented 6 years ago

Going to close this. I don't want to expand the expression grammar if we can help it. A workaround for the above would be to register RegEx by name and then have the filter lookup the RegEx.

davismj commented 6 years ago

Get this out of the box with http://aurelia.plus