Skip to content

Commit

Permalink
add new filterFns
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Nov 12, 2024
1 parent ea6bc49 commit 2056b09
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface FilterFn<
row: Row<TFeatures, TData>,
columnId: string,
filterValue: any,
addMeta: (meta: FilterMeta) => void,
addMeta?: (meta: FilterMeta) => void,
): boolean
autoRemove?: ColumnFilterAutoRemoveTestFn<TFeatures, TData>
resolveFilterValue?: TransformFilterValueFn<TFeatures, TData>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ function _createFilteredRowModel<
const resolvedGlobalFilters: Array<ResolvedColumnFilter<TFeatures, TData>> =
[]

columnFilters?.forEach((d) => {
const column = table_getColumn(table, d.id)
columnFilters?.forEach((columnFilter) => {
const column = table_getColumn(table, columnFilter.id)

if (!column) {
return
Expand All @@ -78,9 +78,10 @@ function _createFilteredRowModel<
}

resolvedColumnFilters.push({
id: d.id,
id: columnFilter.id,
filterFn,
resolvedValue: filterFn.resolveFilterValue?.(d.value) ?? d.value,
resolvedValue:
filterFn.resolveFilterValue?.(columnFilter.value) ?? columnFilter.value,
})
})

Expand All @@ -105,7 +106,7 @@ function _createFilteredRowModel<
})
}

// Flag the prefiltered row model with each filter state
// Flag the pre-filtered row model with each filter state
for (const row of rowModel.flatRows as Array<
Row<TFeatures, TData> & Partial<Row_ColumnFiltering<TFeatures, TData>>
>) {
Expand Down Expand Up @@ -169,5 +170,5 @@ function _createFilteredRowModel<
}

// Filter final rows using all of the active filters
return filterRows(rowModel.rows as any, filterRowsImpl as any, table)
return filterRows(rowModel.rows, filterRowsImpl as any, table)
}
27 changes: 27 additions & 0 deletions packages/table-core/src/fns/aggregationFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import type { TableFeatures } from '../types/TableFeatures'
import type { Row } from '../types/Row'
import type { AggregationFn } from '../features/column-grouping/ColumnGrouping.types'

/**
* Aggregation function for summing up the values of a column.
*/
export const aggregationFn_sum: AggregationFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
Expand All @@ -20,6 +23,9 @@ export const aggregationFn_sum: AggregationFn<any, any> = <
}, 0)
}

/**
* Aggregation function for finding the minimum value of a column.
*/
export const aggregationFn_min: AggregationFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
Expand All @@ -44,6 +50,9 @@ export const aggregationFn_min: AggregationFn<any, any> = <
return minValue
}

/**
* Aggregation function for finding the maximum value of a column.
*/
export const aggregationFn_max: AggregationFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
Expand All @@ -67,6 +76,9 @@ export const aggregationFn_max: AggregationFn<any, any> = <
return maxValue
}

/**
* Aggregation function for finding the extent (min and max) of a column.
*/
export const aggregationFn_extent: AggregationFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
Expand All @@ -93,6 +105,9 @@ export const aggregationFn_extent: AggregationFn<any, any> = <
return [minValue, maxValue]
}

/**
* Aggregation function for finding the mean (average) of a column.
*/
export const aggregationFn_mean: AggregationFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
Expand All @@ -115,6 +130,9 @@ export const aggregationFn_mean: AggregationFn<any, any> = <
return
}

/**
* Aggregation function for finding the median value of a column.
*/
export const aggregationFn_median: AggregationFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
Expand All @@ -139,6 +157,9 @@ export const aggregationFn_median: AggregationFn<any, any> = <
return values.length % 2 !== 0 ? nums[mid] : (nums[mid - 1]! + nums[mid]!) / 2
}

/**
* Aggregation function for finding the unique values of a column.
*/
export const aggregationFn_unique: AggregationFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
Expand All @@ -149,6 +170,9 @@ export const aggregationFn_unique: AggregationFn<any, any> = <
return Array.from(new Set(leafRows.map((d) => d.getValue(columnId))).values())
}

/**
* Aggregation function for finding the count of unique values of a column.
*/
export const aggregationFn_uniqueCount: AggregationFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
Expand All @@ -159,6 +183,9 @@ export const aggregationFn_uniqueCount: AggregationFn<any, any> = <
return new Set(leafRows.map((d) => d.getValue(columnId))).size
}

/**
* Aggregation function for counting the number of rows in a column.
*/
export const aggregationFn_count: AggregationFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
Expand Down
Loading

0 comments on commit 2056b09

Please sign in to comment.