@tanstack/table-core 8.0.0-alpha.20 → 8.0.0-alpha.23

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.
Files changed (53) hide show
  1. package/build/cjs/aggregationTypes.js +21 -19
  2. package/build/cjs/aggregationTypes.js.map +1 -1
  3. package/build/cjs/core.js +8 -8
  4. package/build/cjs/core.js.map +1 -1
  5. package/build/cjs/features/Expanding.js +5 -8
  6. package/build/cjs/features/Expanding.js.map +1 -1
  7. package/build/cjs/features/Filters.js +1 -1
  8. package/build/cjs/features/Filters.js.map +1 -1
  9. package/build/cjs/features/Grouping.js.map +1 -1
  10. package/build/cjs/features/Pinning.js +6 -0
  11. package/build/cjs/features/Pinning.js.map +1 -1
  12. package/build/cjs/features/RowSelection.js +3 -3
  13. package/build/cjs/features/RowSelection.js.map +1 -1
  14. package/build/cjs/utils/columnFilterRowsFn.js +20 -78
  15. package/build/cjs/utils/columnFilterRowsFn.js.map +1 -1
  16. package/build/cjs/utils/filterRowsUtils.js +100 -0
  17. package/build/cjs/utils/filterRowsUtils.js.map +1 -0
  18. package/build/cjs/utils/globalFilterRowsFn.js +10 -62
  19. package/build/cjs/utils/globalFilterRowsFn.js.map +1 -1
  20. package/build/cjs/utils/groupRowsFn.js +15 -15
  21. package/build/cjs/utils/groupRowsFn.js.map +1 -1
  22. package/build/cjs/utils/sortRowsFn.js +1 -1
  23. package/build/cjs/utils/sortRowsFn.js.map +1 -1
  24. package/build/esm/index.js +166 -192
  25. package/build/esm/index.js.map +1 -1
  26. package/build/stats-html.html +1 -1
  27. package/build/stats-react.json +303 -270
  28. package/build/types/aggregationTypes.d.ts +9 -9
  29. package/build/types/core.d.ts +1 -1
  30. package/build/types/features/Expanding.d.ts +1 -0
  31. package/build/types/features/Filters.d.ts +1 -1
  32. package/build/types/features/Grouping.d.ts +1 -1
  33. package/build/types/features/Pinning.d.ts +1 -0
  34. package/build/types/utils/filterRowsUtils.d.ts +3 -0
  35. package/build/types/utils/sortRowsQuicksortFn.d.ts +11 -0
  36. package/build/umd/index.development.js +166 -192
  37. package/build/umd/index.development.js.map +1 -1
  38. package/build/umd/index.production.js +1 -1
  39. package/build/umd/index.production.js.map +1 -1
  40. package/package.json +1 -1
  41. package/src/aggregationTypes.ts +23 -19
  42. package/src/core.tsx +3 -5
  43. package/src/features/Expanding.ts +7 -10
  44. package/src/features/Filters.ts +2 -2
  45. package/src/features/Grouping.ts +2 -2
  46. package/src/features/Pinning.ts +7 -0
  47. package/src/features/RowSelection.ts +1 -3
  48. package/src/utils/columnFilterRowsFn.ts +12 -73
  49. package/src/utils/filterRowsUtils.ts +107 -0
  50. package/src/utils/globalFilterRowsFn.ts +10 -66
  51. package/src/utils/groupRowsFn.ts +14 -16
  52. package/src/utils/sortRowsFn.ts +6 -8
  53. package/src/utils/sortRowsQuicksortFn.ts +174 -0
@@ -0,0 +1,174 @@
1
+ import { TableInstance, Row, RowModel, AnyGenerics } from '../types'
2
+ import { SortingFn } from '../features/Sorting'
3
+
4
+ export function sortRowsFn<TGenerics extends AnyGenerics>(
5
+ instance: TableInstance<TGenerics>,
6
+ rowModel: RowModel<TGenerics>
7
+ ): RowModel<TGenerics> {
8
+ // window.tanner = []
9
+
10
+ const sortingState = instance.getState().sorting
11
+
12
+ const sortedFlatRows: Row<TGenerics>[] = []
13
+
14
+ // Filter out sortings that correspond to non existing columns
15
+ const availableSorting = sortingState.filter(sort =>
16
+ instance.getColumnCanSort(sort.id)
17
+ )
18
+
19
+ const sorters = availableSorting.map(columnSorting => {
20
+ const column = instance.getColumn(columnSorting.id)!
21
+ const sortingFn = instance.getColumnSortingFn(columnSorting.id)!
22
+
23
+ return {
24
+ id: columnSorting.id,
25
+ compare: (rowA: Row<TGenerics>, rowB: Row<TGenerics>) =>
26
+ sortingFn(rowA, rowB, columnSorting.id),
27
+ desc: columnSorting.desc,
28
+ invertSorting: column.invertSorting,
29
+ sortUndefined: column.sortUndefined,
30
+ }
31
+ })
32
+
33
+ const sortData = (rows: Row<TGenerics>[]) => {
34
+ // This will also perform a stable sorting using the row index
35
+ // if needed.
36
+
37
+ const sortedData = quicksort(rows, sorters)
38
+
39
+ // If there are sub-rows, sort them
40
+ sortedData.forEach(row => {
41
+ sortedFlatRows.push(row)
42
+ if (!row.subRows || row.subRows.length <= 1) {
43
+ return
44
+ }
45
+ row.subRows = sortData(row.subRows)
46
+ })
47
+
48
+ return sortedData
49
+ }
50
+
51
+ return {
52
+ rows: sortData(rowModel.rows),
53
+ flatRows: sortedFlatRows,
54
+ rowsById: rowModel.rowsById,
55
+ }
56
+ }
57
+
58
+ type Sorter<TGenerics extends AnyGenerics> = {
59
+ id: string
60
+ compare: (a: Row<TGenerics>, b: Row<TGenerics>) => number
61
+ desc?: boolean
62
+ sortUndefined?: false | -1 | 1
63
+ invertSorting?: boolean
64
+ }
65
+
66
+ export function quicksort<TGenerics extends AnyGenerics>(
67
+ rows: Row<TGenerics>[],
68
+ sorters: Sorter<TGenerics>[]
69
+ ): Row<TGenerics>[] {
70
+ if (!rows.length) {
71
+ return rows
72
+ }
73
+
74
+ // Copy the rows so we can mutate the order
75
+ rows = rows.slice()
76
+
77
+ // Start the quicksort recursion
78
+ recurse(0, rows.length - 1)
79
+
80
+ // Return the rows
81
+ return rows
82
+
83
+ function recurse(start: number, end: number) {
84
+ const pivotIndex = pivot(start, end)
85
+
86
+ if (start < pivotIndex - 1) {
87
+ recurse(start, pivotIndex - 1)
88
+ }
89
+
90
+ if (pivotIndex < end) {
91
+ recurse(pivotIndex + 1, end)
92
+ }
93
+ }
94
+
95
+ function pivot(start: number, end: number) {
96
+ const pivotIndex = Math.floor((start + end) / 2)
97
+ let i = start
98
+ let j = end
99
+
100
+ const compare = (a: number, b: number) => {
101
+ const rowA = rows[a]
102
+ const rowB = rows[b]
103
+
104
+ for (let i = 0; i < sorters.length; i += 1) {
105
+ const sorter = sorters[i]!
106
+ const isDesc = sorter?.desc ?? false
107
+
108
+ if (sorter.sortUndefined) {
109
+ const aValue = rowA.values[sorter.id]
110
+ const bValue = rowB.values[sorter.id]
111
+
112
+ const aUndefined = typeof aValue === 'undefined'
113
+ const bUndefined = typeof bValue === 'undefined'
114
+
115
+ if (aUndefined || bUndefined) {
116
+ if (aUndefined !== bUndefined) {
117
+ return aUndefined ? sorter.sortUndefined : -sorter.sortUndefined
118
+ }
119
+ }
120
+ }
121
+
122
+ // This function should always return in ascending order
123
+ let sortInt = sorter.compare(rowA, rowB)
124
+
125
+ if (i > 0) {
126
+ // window.tanner.push([
127
+ // sorter.id,
128
+ // rowA.values[sorter.id],
129
+ // rowB.values[sorter.id],
130
+ // sortInt,
131
+ // ])
132
+ }
133
+
134
+ if (sortInt !== 0) {
135
+ if (isDesc) {
136
+ sortInt *= -1
137
+ }
138
+
139
+ if (sorter.invertSorting) {
140
+ sortInt *= -1
141
+ }
142
+
143
+ return sortInt
144
+ }
145
+ }
146
+
147
+ return rowA.index < rowB.index ? -1 : 1
148
+ }
149
+
150
+ while (i <= j) {
151
+ while (compare(i, pivotIndex) < 0) {
152
+ i++
153
+ }
154
+
155
+ while (compare(pivotIndex, j) < 0) {
156
+ j--
157
+ }
158
+
159
+ if (i <= j) {
160
+ swap(rows, i, j)
161
+ i++
162
+ j--
163
+ }
164
+ }
165
+
166
+ return i
167
+ }
168
+
169
+ function swap<T>(arr: T[], a: number, b: number) {
170
+ const t = arr[a]
171
+ arr[a] = arr[b]
172
+ arr[b] = t
173
+ }
174
+ }