kendo-uikendo-react-uikendo-angular-ui

@progress/kendo-data-query FilterDescriptor operator function signature


According to the docs, the FilterDescriptor's operator prop can be a function.

filterDescriptor.d.ts

/**
 * A basic filter expression. Usually a part of [`CompositeFilterDescriptor`]({% slug api_kendo-data-query_compositefilterdescriptor %}).
 *
 * For more information, refer to the [`filterBy`]({% slug api_kendo-data-query_filterby %}) method.
 */
export interface FilterDescriptor {
    /**
     * The field of the data item to which the filter operator is applied.
     */
    field?: string | Function;
    /**
     * The filter operator (comparison).
     *
     * The supported operators are:
     * * `"eq"` (equal to)
     * * `"neq"` (not equal to)
     * * `"isnull"` (is equal to null)
     * * `"isnotnull"` (is not equal to null)
     * * `"lt"` (less than)
     * * `"lte"` (less than or equal to)
     * * `"gt"` (greater than)
     * * `"gte"` (greater than or equal to)
     *
     * The following operators are supported for string fields only:
     * * `"startswith"`
     * * `"endswith"`
     * * `"contains"`
     * * `"doesnotcontain"`
     * * `"isempty"`
     * * `"isnotempty"`
     */
    operator: string | Function;
    /**
     * The value to which the field is compared. Has to be of the same type as the field.
     */
    value?: any;
    /**
     * Determines if the string comparison is case-insensitive.
     */
    ignoreCase?: boolean;
}

I can find nothing on the internet about the function signature for FilterDescriptor.operator should be. And of course because Kendo isn't open source, I can't simply examine their code.


Solution

  • With a little logging, I figured it out.

    export interface FilterDescriptor {
      // ...
      operator: string | ((fieledValue: any, filterValue: any) => boolean);
    }
    

    So, if you have a list of objects with names, and you wanted to create a filter that matched one of several names (assuming you did not want to create a filter entry for each), you could do:

    const createFilterDesc = (options) => ({
      field: 'name',
      value: new Set(options),
      operator: (val, opts) => opts.has(val),
    });