Boolean and not existing propertys
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?
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
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
I agree. I don´t think the current behavior is expected or follows any logic.
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: "" };
},
});
};
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.