-
Notifications
You must be signed in to change notification settings - Fork 0
Range filter
The AbstractRangeFilter class is an abstract base class for filters that apply range operations. It is typically used for numeric or date-based filtering, allowing the user to set conditions that represent a range of values. The filter supports comparison operations like greater than and less than.
This class is generic and uses one type parameter:
-
T: The data type of the values being filtered, which can be eithernumberorDate.
constructor(column: string, label: string, conditions?: Condition[])-
column: string: The name of the column or field that the filter applies to. -
label: string: The label or display name for the filter. -
conditions?: Condition[]: An optional array of conditions to initialize the filter with. This array can contain one or two conditions.
If more than two conditions are provided, the constructor throws a RangeError because range filters only support up to two conditions.
apply(value1: T, operation1: ComparisonOperation, value2: T, operation2: ComparisonOperation): void;-
Type:
void - Description: Applies a range of conditions to the filter by specifying two values and their respective comparison operations.
-
Parameters:
-
value1: T: The first value in the range, representing the lower or upper bound. -
operation1: ComparisonOperation: The comparison operation to apply tovalue1(e.g., greater than, less than). -
value2: T: The second value in the range, representing the opposite bound (upper or lower bound). -
operation2: ComparisonOperation: The comparison operation to apply tovalue2.
-
-
Remarks:
- This method replaces any previously set conditions with the new range conditions.
- The filter supports exactly two conditions, typically representing both the upper and lower bounds of the range.
The AbstractRangeFilter class is designed to handle filtering logic for values that can be represented as ranges. It allows for applying two conditions that represent the bounds of the range and emits an event when the filter is applied.
Suppose you want to create a filter to handle numeric ranges, like filtering a dataset based on a range of prices (see here):
class PriceRangeFilter extends AbstractRangeFilter<number> {
// Additional custom methods can be added here
}
const priceFilter = new PriceRangeFilter("price", "Price Filter");
priceFilter.apply(
10,
ComparisonOperation.GreaterThan,
50,
ComparisonOperation.LessThanOrEqual
); // Sets the range of 10 to 50