Filter manipulations

#1

Suppose I am defining a Filter that I store in a variable: var f1 = Filter.eq(‘account.id’, ‘123’);
I now want to define a more complex Filter using f1:

var f2 = Filter.eq(‘id’, ‘abc’).and.f1

That does not work, how can I use a Filter stored in a variable?

#2

As of v7.9, Filter is a type and instances are immutable. The operators are now methods (e.g. f1.and.eq('id', 'abc') became f1.and().eq('id', 'abc')), and the new immutability actually makes it much simpler to store and re-use filters using variables.

Here’s how to do what you’re looking for:

v7.8 and older:

var f1 = Filter.eq('account.id', '123');
var f2 = Filter.eq('id', 'abc').and.lit(f1);

v7.9 and newer:

var f1 = Filter.eq('account.id', '123');
var f2 = f1.and().eq('id', 'abc'); // f1 remains unchanged

// or
var f2 = Filter.eq('id', 'abc').and().lit(f1);

lit is short for “literal” and allows you to insert any string into a Filter. In these examples that use lit, f1 is automatically converted to a string. In v7.8 and below, using lit was necessary to maintain immutability, but in v7.9 and later it’s no longer required. IMO it’s cleaner without it, so I recommend to avoid it if you already have a Filter instance available.

1 Like
#3

Thanks.

Would you use lit if you want to check a boolean like: “account.id == ‘123’ && debitFlag”?