UNPKG

@equantic/linq

Version:
94 lines (93 loc) 3.33 kB
/** * Represents a single filtering condition for a specific column/property. * Provides type-safe filtering with strongly-typed column paths and values. * * @template TData - The type of data being filtered * @template TColumn - The specific column/field path being filtered */ export class Filtering { /** The column or field path being filtered */ column; /** The filter operator to apply */ operator; /** The value to filter by, typed according to the column type */ value; constructor(...args) { this.column = args?.length > 0 ? args[0] : null; this.value = args?.length > 1 ? args[1] : null; this.operator = args?.length > 2 ? args[2] : 'eq'; } /** * Converts the filtering instance to its string representation. * @returns A string representation in the format 'column:operator(value)' */ toString() { return `${this.column}:${this.operator}(${this.value})`; } /** * Creates a new filtering instance with equality operator. * @template TData - The type of data being filtered * @template TColumn - The specific column/field path being filtered * @param column - The column or field path to filter * @param value - The value to test for equality * @returns A new filtering instance with equality operation */ static eq(column, value) { return new Filtering(column, value, 'eq'); } /** * Creates a new filtering instance with inequality operator. * @template TData - The type of data being filtered * @template TColumn - The specific column/field path being filtered * @param column - The column or field path to filter * @param value - The value to test for inequality * @returns A new filtering instance with inequality operation */ static neq(column, value) { return new Filtering(column, value, 'neq'); } static gt(column, value) { return new Filtering(column, value, 'gt'); } static lt(column, value) { return new Filtering(column, value, 'lt'); } static gte(column, value) { return new Filtering(column, value, 'gte'); } static lte(column, value) { return new Filtering(column, value, 'lte'); } /** * Creates a new filtering instance with contains operator (for string properties). * @template TData - The type of data being filtered * @template TColumn - The specific column/field path being filtered * @param column - The column or field path to filter * @param value - The substring to search for within the property value * @returns A new filtering instance with contains operation */ static ct(column, value) { return new Filtering(column, value, 'ct'); } static in(column, ...value) { return new Filtering(column, value, 'in'); } static sw(column, value) { return new Filtering(column, value, 'sw'); } static ew(column, value) { return new Filtering(column, value, 'ew'); } } /** * Alias for Filtering class for more concise usage. * @example * ```typescript * // Using full class name * const filter1 = Filtering.eq('name', 'John'); * * // Using alias * const filter2 = F.eq('name', 'John'); * ``` */ export const F = Filtering;