odata-fluent-query icon indicating copy to clipboard operation
odata-fluent-query copied to clipboard

Let null or undefined into filter expressions

Open vnesterovsky opened this issue 2 years ago • 5 comments
trafficstars

Can you consider letting to pass null or undefined into FilterXXX methods, in which case filter should turn into no operation.

In this case following query.filter(t => t.date.equals(myDate)) will add date filter, provided myDate has a value.

vnesterovsky avatar Feb 22 '23 12:02 vnesterovsky

Hey, sorry for the delay!

Wouldn't it work if your model is of type Date | null?

rosostolato avatar Mar 23 '23 20:03 rosostolato

In my case myDate value is of type Date|null|undefined.

My goal was to conditionally add filters. It is not the same as to compare value against null or undefined.

With suggested feature one could compose filters like:

...
filter(t => t.x.equals(myX)).
filter(t => t.y.equals(myY))
...

At present I have to write:

if (myX != null)
{
  query = query.filter(t => t.x.equals(myX));  
}

if (myY != null)
{
  query = query.filter(t => t.y.equals(myY));  
}

vnesterovsky avatar Mar 24 '23 13:03 vnesterovsky

Sorry, it's a bit confusing. Do you want to return null inside the filter function to not include the filter? Something like:

query.filter(t => myX ? t.x.equals(myX) : null)

And if it returns null it should be ignored?

rosostolato avatar Mar 24 '23 17:03 rosostolato

Yes, something like this.

Consider, that I have some form fields, and I want to filter my results by only those I explicitly filled with values.

So, to build query I somehow need to ignore some of filters.

Ultimately: query.filter(t => t.x.equals(myX)) could beheave is if no any filter applied if myX === undefined

vnesterovsky avatar Mar 24 '23 17:03 vnesterovsky

@vnesterovsky I want to take a deeper look into this, do you think you can share a stackblitz or github project reproducing this?

rosostolato avatar Jul 11 '23 16:07 rosostolato