Skip to content

Commit 2056b09

Browse files
committed
add new filterFns
1 parent ea6bc49 commit 2056b09

File tree

4 files changed

+245
-61
lines changed

4 files changed

+245
-61
lines changed

packages/table-core/src/features/column-filtering/ColumnFiltering.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export interface FilterFn<
5050
row: Row<TFeatures, TData>,
5151
columnId: string,
5252
filterValue: any,
53-
addMeta: (meta: FilterMeta) => void,
53+
addMeta?: (meta: FilterMeta) => void,
5454
): boolean
5555
autoRemove?: ColumnFilterAutoRemoveTestFn<TFeatures, TData>
5656
resolveFilterValue?: TransformFilterValueFn<TFeatures, TData>

packages/table-core/src/features/column-filtering/createFilteredRowModel.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ function _createFilteredRowModel<
5959
const resolvedGlobalFilters: Array<ResolvedColumnFilter<TFeatures, TData>> =
6060
[]
6161

62-
columnFilters?.forEach((d) => {
63-
const column = table_getColumn(table, d.id)
62+
columnFilters?.forEach((columnFilter) => {
63+
const column = table_getColumn(table, columnFilter.id)
6464

6565
if (!column) {
6666
return
@@ -78,9 +78,10 @@ function _createFilteredRowModel<
7878
}
7979

8080
resolvedColumnFilters.push({
81-
id: d.id,
81+
id: columnFilter.id,
8282
filterFn,
83-
resolvedValue: filterFn.resolveFilterValue?.(d.value) ?? d.value,
83+
resolvedValue:
84+
filterFn.resolveFilterValue?.(columnFilter.value) ?? columnFilter.value,
8485
})
8586
})
8687

@@ -105,7 +106,7 @@ function _createFilteredRowModel<
105106
})
106107
}
107108

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

171172
// Filter final rows using all of the active filters
172-
return filterRows(rowModel.rows as any, filterRowsImpl as any, table)
173+
return filterRows(rowModel.rows, filterRowsImpl as any, table)
173174
}

packages/table-core/src/fns/aggregationFns.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import type { TableFeatures } from '../types/TableFeatures'
44
import type { Row } from '../types/Row'
55
import type { AggregationFn } from '../features/column-grouping/ColumnGrouping.types'
66

7+
/**
8+
* Aggregation function for summing up the values of a column.
9+
*/
710
export const aggregationFn_sum: AggregationFn<any, any> = <
811
TFeatures extends TableFeatures,
912
TData extends RowData,
@@ -20,6 +23,9 @@ export const aggregationFn_sum: AggregationFn<any, any> = <
2023
}, 0)
2124
}
2225

26+
/**
27+
* Aggregation function for finding the minimum value of a column.
28+
*/
2329
export const aggregationFn_min: AggregationFn<any, any> = <
2430
TFeatures extends TableFeatures,
2531
TData extends RowData,
@@ -44,6 +50,9 @@ export const aggregationFn_min: AggregationFn<any, any> = <
4450
return minValue
4551
}
4652

53+
/**
54+
* Aggregation function for finding the maximum value of a column.
55+
*/
4756
export const aggregationFn_max: AggregationFn<any, any> = <
4857
TFeatures extends TableFeatures,
4958
TData extends RowData,
@@ -67,6 +76,9 @@ export const aggregationFn_max: AggregationFn<any, any> = <
6776
return maxValue
6877
}
6978

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

108+
/**
109+
* Aggregation function for finding the mean (average) of a column.
110+
*/
96111
export const aggregationFn_mean: AggregationFn<any, any> = <
97112
TFeatures extends TableFeatures,
98113
TData extends RowData,
@@ -115,6 +130,9 @@ export const aggregationFn_mean: AggregationFn<any, any> = <
115130
return
116131
}
117132

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

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

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

186+
/**
187+
* Aggregation function for counting the number of rows in a column.
188+
*/
162189
export const aggregationFn_count: AggregationFn<any, any> = <
163190
TFeatures extends TableFeatures,
164191
TData extends RowData,

0 commit comments

Comments
 (0)