filtrex icon indicating copy to clipboard operation
filtrex copied to clipboard

String expressions fail when properties are in an object

Open dpdonohue opened this issue 9 years ago • 4 comments

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'}}));
    }

dpdonohue avatar Nov 14 '15 20:11 dpdonohue

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?

dpdonohue avatar Nov 19 '15 15:11 dpdonohue

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'
}

joewalnes avatar Nov 20 '15 03:11 joewalnes

For a temp solution you can use https://www.npmjs.com/package/flat package :)

serhiisol avatar Dec 21 '18 13:12 serhiisol

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.

cshaa avatar Mar 01 '20 14:03 cshaa