filtrex icon indicating copy to clipboard operation
filtrex copied to clipboard

Boolean and not existing propertys

Open rokdd opened this issue 1 year ago • 5 comments

Maybe I am stupid but I did not found an example with boolean. So here is the sample where the expected result is not matching the reality:

import { compileExpression} from 'filtrex';
let myfilter = compileExpression("has_bla");
for (const x of [{ "has_bla": true }, { "has_nothing": true }]) {
    console.log(x)
    if (myfilter(x)) {
        console.log("Matched!")
    }
}

Result:

{ has_bla: true }
Matched!
{ has_nothing: true }
Matched!
//should not be matched the last one?

rokdd avatar Aug 23 '24 12:08 rokdd

I faced this issue today. Filtrex seems to have a problem when a value is undefined in the object provided to the checker function (myfilter in your case), and it always resolves as true for boolean checks.

The following workaround seems to work fine in my case. I used a Proxy to make Filtrex think that the property exists, but with an empty string as value:

function createDataProxy(data)  {
  return new Proxy(data, {
    get(target, prop) {
      return prop in target ? target[prop] : "";
    },

    getOwnPropertyDescriptor(target, prop) {
      return prop in target
        ? Object.getOwnPropertyDescriptor(target, prop)
        : { enumerable: false, configurable: true, writable: true, value: "" };
    },
  });
};

const myfilter = compileExpression("has_bla");

myfilter(createDataProxy({ has_bla: true })) // true
myfilter(createDataProxy({ has_nothing: true })) // ""

I hope this helps

daveruiz avatar Aug 31 '24 16:08 daveruiz

Okay great, then I am proved now that I have not overseen something ;-). I like your solution I will try it tomorrow, but it should be also fixed :-)

Update: Thanks for your proxy, it is working now

rokdd avatar Sep 01 '24 14:09 rokdd

I agree. I don´t think the current behavior is expected or follows any logic.

daveruiz avatar Sep 10 '24 13:09 daveruiz

I needed to update it to:

function filrex_proxy(data) {
    return new Proxy(data, {
        get(target, prop) {
            return prop in target && target[prop]!=null  ? target[prop] : "";
        },

        getOwnPropertyDescriptor(target, prop) {
            return prop in target && target[prop] != null 
                ? Object.getOwnPropertyDescriptor(target, prop)
                : { enumerable: false, configurable: true, writable: true, value: "" };
        },
    });
};

rokdd avatar Oct 16 '24 07:10 rokdd

I stumble upon the same problem here. A fix would be very welcome. This is a serious bug and unfortunately breaks stuff on our end.

dannybloe avatar Jan 23 '25 14:01 dannybloe