filtrex
filtrex copied to clipboard
String expressions fail when properties are in an object
Joe I like this tool! I ran this unit test, which is similar to your existing unit test. It always returns false/0
'filtrex: object string test': function(test) {
test.equal(1, compileExpression('obj.foo == "hello"')({obj:{foo:'hello'}}));
test.equal(0, compileExpression('obj.foo == "hello"')({obj:{foo:'bye'}}));
test.equal(0, compileExpression('obj.foo != "hello"')({obj:{foo:'hello'}}));
test.equal(1, compileExpression('obj.foo != "hello"')({obj:{foo:'bye'}}));
test.equal(true, compileExpression('obj.foo in ("aa", "bb")')({obj:{foo:'aa'}}));
test.equal(false, compileExpression('obj.foo in ("aa", "bb")')({obj:{foo:'c'}}));
test.equal(false, compileExpression('obj.foo not in ("aa", "bb")')({obj:{foo:'aa'}}));
test.equal(true, compileExpression('obj.foo not in ("aa", "bb")')({obj:{foo:'cc'}}));
}
So Filtrex fails to match strings when they are nested inside an object. Do you think this is a problem with Filtrex or with the underlying jison engine?
Filtrex does not support nested expressions. I haven't added it because there's a lot of edge cases I'd like to think through first.
To handle this, you'll have to write code that flattens your structure into a single nested object.
For example, convert this:
{
obj: {
foo:'a',
bar:'b'
}
}
into this:
{
'obj.foo': 'a',
'obj.bar': 'b'
}
For a temp solution you can use https://www.npmjs.com/package/flat package :)
Also, the new filtrex supports the of
operator:
let f = compileExpression('version of software >= 1.4')
f({ software: {name: 'Foo', version: 2} }) // true
f({ software: {name: 'bar', version: 1} }) // false
Using version of software
instead of software.version
might feel backwards for a developer, but it is closer to plain English, so we went with that.