@tanstack/table-core 8.9.0 → 8.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/table-core",
3
3
  "author": "Tanner Linsley",
4
- "version": "8.9.0",
4
+ "version": "8.9.2",
5
5
  "description": "Headless UI for building powerful tables & datagrids for TS/JS.",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/tanstack/table#readme",
@@ -1,4 +1,5 @@
1
1
  import { AggregationFn } from './features/Grouping'
2
+ import { isNumberArray } from './utils'
2
3
 
3
4
  const sum: AggregationFn<any> = (columnId, _leafRows, childRows) => {
4
5
  // It's faster to just add the aggregations together instead of
@@ -82,18 +83,17 @@ const median: AggregationFn<any> = (columnId, leafRows) => {
82
83
  return
83
84
  }
84
85
 
85
- let min = 0
86
- let max = 0
87
-
88
- leafRows.forEach(row => {
89
- let value = row.getValue(columnId)
90
- if (typeof value === 'number') {
91
- min = Math.min(min, value)
92
- max = Math.max(max, value)
93
- }
94
- })
86
+ const values = leafRows.map(row => row.getValue(columnId))
87
+ if (!isNumberArray(values)) {
88
+ return
89
+ }
90
+ if (values.length === 1) {
91
+ return values[0]
92
+ }
95
93
 
96
- return (min + max) / 2
94
+ const mid = Math.floor(values.length / 2)
95
+ const nums = values.sort((a, b) => a - b)
96
+ return values.length % 2 !== 0 ? nums[mid] : (nums[mid - 1]! + nums[mid]!) / 2
97
97
  }
98
98
 
99
99
  const unique: AggregationFn<any> = (columnId, leafRows) => {
@@ -61,11 +61,18 @@ export function getSortedRowModel<TData extends RowData>(): (
61
61
  const bUndefined = typeof bValue === 'undefined'
62
62
 
63
63
  if (aUndefined || bUndefined) {
64
- return aUndefined && bUndefined
65
- ? 0
66
- : aUndefined
67
- ? columnInfo.sortUndefined
68
- : -columnInfo.sortUndefined
64
+ let undefinedSort =
65
+ aUndefined && bUndefined
66
+ ? 0
67
+ : aUndefined
68
+ ? columnInfo.sortUndefined
69
+ : -columnInfo.sortUndefined
70
+
71
+ if (isDesc && undefinedSort !== 0) {
72
+ undefinedSort *= -1
73
+ }
74
+
75
+ return undefinedSort
69
76
  }
70
77
  }
71
78
 
package/src/utils.ts CHANGED
@@ -103,6 +103,10 @@ export function isFunction<T extends AnyFunction>(d: any): d is T {
103
103
  return d instanceof Function
104
104
  }
105
105
 
106
+ export function isNumberArray(d: any): d is number[] {
107
+ return Array.isArray(d) && d.every(val => typeof val === 'number')
108
+ }
109
+
106
110
  export function flattenBy<TNode>(
107
111
  arr: TNode[],
108
112
  getChildren: (item: TNode) => TNode[]