/* * This file belongs to Hoist, an application development toolkit * developed by Extremely Heavy Industries (www.xh.io | info@xh.io) * * Copyright © 2026 Extremely Heavy Industries Inc. */ import {XH} from '@xh/hoist/core'; import {throwIf} from '@xh/hoist/utils/js'; import {isFunction} from 'lodash'; import {Store} from '../Store'; import {Filter} from './Filter'; import {FunctionFilterSpec, FilterTestFn} from './Types'; /** * Filters via a custom function specified by the developer or generated by a component such as * StoreFilterField. * * Immutable. */ export class FunctionFilter extends Filter { static isFunctionFilter(obj: unknown): obj is FunctionFilter { return obj instanceof FunctionFilter; } readonly key: string; readonly testFn: FilterTestFn; /** * Constructor - not typically called by apps - create via {@link parseFilter} instead. * @internal */ constructor({key, testFn}: FunctionFilterSpec) { super(); throwIf(!key, 'FunctionFilter requires a `key`'); throwIf(!isFunction(testFn), 'FunctionFilter requires a `testFn`'); this.key = key; this.testFn = testFn; Object.freeze(this); } //----------------- // Overrides //----------------- override getTestFn(store?: Store): FilterTestFn { return this.testFn; } override equals(other: Filter) { if (other === this) return true; return other instanceof FunctionFilter && this.testFn === other.testFn; } override toJSON(): FunctionFilterSpec { throw XH.exception('FunctionFilter.toJSON() not supported.'); } override removeFieldFilters(field: string = null): Filter { return this; } override removeFunctionFilters(key: string = null): Filter { return !key || this.key === key ? null : this; } }