Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean and not existing propertys #35

Open
rokdd opened this issue Aug 23, 2024 · 3 comments
Open

Boolean and not existing propertys #35

rokdd opened this issue Aug 23, 2024 · 3 comments

Comments

@rokdd
Copy link

rokdd commented Aug 23, 2024

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?
@daveruiz
Copy link

daveruiz commented Aug 31, 2024

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

@rokdd
Copy link
Author

rokdd commented Sep 1, 2024

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

@daveruiz
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants