@tanstack/table-core 8.0.0-alpha.30 → 8.0.0-alpha.32

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 (120) hide show
  1. package/build/cjs/aggregationTypes.js +1 -0
  2. package/build/cjs/aggregationTypes.js.map +1 -1
  3. package/build/cjs/core.js +22 -78
  4. package/build/cjs/core.js.map +1 -1
  5. package/build/cjs/createTable.js.map +1 -1
  6. package/build/cjs/features/ColumnSizing.js.map +1 -1
  7. package/build/cjs/features/Expanding.js +12 -16
  8. package/build/cjs/features/Expanding.js.map +1 -1
  9. package/build/cjs/features/Filters.js +28 -81
  10. package/build/cjs/features/Filters.js.map +1 -1
  11. package/build/cjs/features/Grouping.js +10 -16
  12. package/build/cjs/features/Grouping.js.map +1 -1
  13. package/build/cjs/features/Headers.js.map +1 -1
  14. package/build/cjs/features/Ordering.js.map +1 -1
  15. package/build/cjs/features/Pagination.js +8 -13
  16. package/build/cjs/features/Pagination.js.map +1 -1
  17. package/build/cjs/features/Pinning.js.map +1 -1
  18. package/build/cjs/features/RowSelection.js.map +1 -1
  19. package/build/cjs/features/Sorting.js +10 -16
  20. package/build/cjs/features/Sorting.js.map +1 -1
  21. package/build/cjs/features/Visibility.js.map +1 -1
  22. package/build/cjs/filterTypes.js.map +1 -1
  23. package/build/cjs/index.js +19 -12
  24. package/build/cjs/index.js.map +1 -1
  25. package/build/cjs/sortTypes.js.map +1 -1
  26. package/build/cjs/utils/filterRowsUtils.js.map +1 -1
  27. package/build/cjs/utils/getColumnFilteredRowModelSync.js +113 -0
  28. package/build/cjs/utils/getColumnFilteredRowModelSync.js.map +1 -0
  29. package/build/cjs/utils/getCoreRowModelAsync.js +125 -0
  30. package/build/cjs/utils/getCoreRowModelAsync.js.map +1 -0
  31. package/build/cjs/utils/getCoreRowModelSync.js +102 -0
  32. package/build/cjs/utils/getCoreRowModelSync.js.map +1 -0
  33. package/build/cjs/utils/getExpandedRowModel.js +61 -0
  34. package/build/cjs/utils/getExpandedRowModel.js.map +1 -0
  35. package/build/cjs/utils/getGlobalFilteredRowModelSync.js +89 -0
  36. package/build/cjs/utils/getGlobalFilteredRowModelSync.js.map +1 -0
  37. package/build/cjs/utils/getGroupedRowModel.js +174 -0
  38. package/build/cjs/utils/getGroupedRowModel.js.map +1 -0
  39. package/build/cjs/utils/getPaginationRowModel.js +61 -0
  40. package/build/cjs/utils/getPaginationRowModel.js.map +1 -0
  41. package/build/cjs/utils/getSortedRowModelSync.js +116 -0
  42. package/build/cjs/utils/getSortedRowModelSync.js.map +1 -0
  43. package/build/cjs/utils.js +155 -16
  44. package/build/cjs/utils.js.map +1 -1
  45. package/build/esm/index.js +841 -491
  46. package/build/esm/index.js.map +1 -1
  47. package/build/stats-html.html +1 -1
  48. package/build/stats-react.json +406 -298
  49. package/build/types/aggregationTypes.d.ts +1 -1
  50. package/build/types/core.d.ts +7 -1
  51. package/build/types/features/Expanding.d.ts +2 -1
  52. package/build/types/features/Filters.d.ts +6 -2
  53. package/build/types/features/Grouping.d.ts +3 -2
  54. package/build/types/features/Pagination.d.ts +2 -1
  55. package/build/types/features/Sorting.d.ts +2 -1
  56. package/build/types/index.d.ts +8 -6
  57. package/build/types/types.d.ts +2 -2
  58. package/build/types/utils/getColumnFilteredRowModelAsync.d.ts +4 -0
  59. package/build/types/utils/getColumnFilteredRowModelSync.d.ts +2 -0
  60. package/build/types/utils/getCoreRowModelAsync.d.ts +4 -0
  61. package/build/types/utils/getCoreRowModelSync.d.ts +2 -0
  62. package/build/types/utils/getExpandedRowModel.d.ts +7 -0
  63. package/build/types/utils/getGlobalFilteredRowModelAsync.d.ts +4 -0
  64. package/build/types/utils/getGlobalFilteredRowModelSync.d.ts +2 -0
  65. package/build/types/utils/getGroupedRowModel.d.ts +2 -0
  66. package/build/types/utils/getPaginationRowModel.d.ts +4 -0
  67. package/build/types/utils/{sortRowsQuicksortFn.d.ts → getSortedRowModelAsync.d.ts} +3 -1
  68. package/build/types/utils/getSortedRowModelSync.d.ts +2 -0
  69. package/build/types/utils.d.ts +16 -0
  70. package/build/umd/index.development.js +851 -496
  71. package/build/umd/index.development.js.map +1 -1
  72. package/build/umd/index.production.js +1 -1
  73. package/build/umd/index.production.js.map +1 -1
  74. package/package.json +1 -1
  75. package/src/aggregationTypes.ts +1 -1
  76. package/src/core.tsx +33 -97
  77. package/src/features/Expanding.ts +19 -27
  78. package/src/features/Filters.ts +52 -99
  79. package/src/features/Grouping.ts +19 -21
  80. package/src/features/Pagination.ts +17 -20
  81. package/src/features/Sorting.ts +19 -23
  82. package/src/index.tsx +8 -6
  83. package/src/types.ts +3 -1
  84. package/src/utils/getColumnFilteredRowModelAsync.ts +117 -0
  85. package/src/utils/getColumnFilteredRowModelSync.ts +97 -0
  86. package/src/utils/getCoreRowModelAsync.ts +103 -0
  87. package/src/utils/getCoreRowModelSync.ts +105 -0
  88. package/src/utils/getExpandedRowModel.ts +58 -0
  89. package/src/utils/getGlobalFilteredRowModelAsync.ts +95 -0
  90. package/src/utils/getGlobalFilteredRowModelSync.ts +74 -0
  91. package/src/utils/getGroupedRowModel.ts +187 -0
  92. package/src/utils/getPaginationRowModel.ts +45 -0
  93. package/src/utils/getSortedRowModelAsync.ts +199 -0
  94. package/src/utils/getSortedRowModelSync.ts +116 -0
  95. package/src/utils.tsx +189 -16
  96. package/build/cjs/utils/columnFilterRowsFn.js +0 -73
  97. package/build/cjs/utils/columnFilterRowsFn.js.map +0 -1
  98. package/build/cjs/utils/expandRowsFn.js +0 -37
  99. package/build/cjs/utils/expandRowsFn.js.map +0 -1
  100. package/build/cjs/utils/globalFilterRowsFn.js +0 -49
  101. package/build/cjs/utils/globalFilterRowsFn.js.map +0 -1
  102. package/build/cjs/utils/groupRowsFn.js +0 -155
  103. package/build/cjs/utils/groupRowsFn.js.map +0 -1
  104. package/build/cjs/utils/paginateRowsFn.js +0 -44
  105. package/build/cjs/utils/paginateRowsFn.js.map +0 -1
  106. package/build/cjs/utils/sortRowsFn.js +0 -94
  107. package/build/cjs/utils/sortRowsFn.js.map +0 -1
  108. package/build/types/utils/columnFilterRowsFn.d.ts +0 -2
  109. package/build/types/utils/expandRowsFn.d.ts +0 -2
  110. package/build/types/utils/globalFilterRowsFn.d.ts +0 -2
  111. package/build/types/utils/groupRowsFn.d.ts +0 -2
  112. package/build/types/utils/paginateRowsFn.d.ts +0 -2
  113. package/build/types/utils/sortRowsFn.d.ts +0 -2
  114. package/src/utils/columnFilterRowsFn.ts +0 -56
  115. package/src/utils/expandRowsFn.ts +0 -34
  116. package/src/utils/globalFilterRowsFn.ts +0 -39
  117. package/src/utils/groupRowsFn.ts +0 -174
  118. package/src/utils/paginateRowsFn.ts +0 -28
  119. package/src/utils/sortRowsFn.ts +0 -99
  120. package/src/utils/sortRowsQuicksortFn.ts +0 -174
@@ -0,0 +1,74 @@
1
+ import { TableInstance, RowModel, AnyGenerics, Row } from '../types'
2
+ import { memo } from '../utils'
3
+ import {
4
+ filterRowModelFromLeafs,
5
+ filterRowModelFromRoot,
6
+ } from './filterRowsUtils'
7
+
8
+ export function getGlobalFilteredRowModelSync<
9
+ TGenerics extends AnyGenerics
10
+ >(): (instance: TableInstance<TGenerics>) => () => RowModel<TGenerics> {
11
+ return instance =>
12
+ memo(
13
+ () => [instance.getState().globalFilter, instance.getCoreRowModel()],
14
+ (globalFilter, rowModel) => {
15
+ const globalFilteredRowModel: RowModel<TGenerics> = (() => {
16
+ if (!rowModel.rows.length || !globalFilter) {
17
+ return rowModel
18
+ }
19
+
20
+ const filterFromLeafRows = instance.options.filterFromLeafRows
21
+
22
+ const filterFn = instance.getGlobalFilterFn()
23
+
24
+ if (!filterFn) {
25
+ if (process.env.NODE_ENV !== 'production') {
26
+ console.warn(`Could not find a valid 'globalFilterType'`)
27
+ }
28
+ return rowModel
29
+ }
30
+
31
+ const filterableColumns = instance
32
+ .getAllLeafColumns()
33
+ .filter(column => column.getCanGlobalFilter())
34
+
35
+ const filterableColumnIds = filterableColumns.map(d => d.id)
36
+
37
+ const filterRows = (rows: Row<TGenerics>[]) => {
38
+ return filterFn(rows, filterableColumnIds, globalFilter)
39
+ }
40
+
41
+ if (filterFromLeafRows) {
42
+ filterRowModelFromLeafs(rowModel.rows, filterRows, instance)
43
+ }
44
+
45
+ return filterRowModelFromRoot(rowModel.rows, filterRows, instance)
46
+ })()
47
+
48
+ // Now that each filtered column has it's partially filtered rows,
49
+ // lets assign the final filtered rows to all of the other columns
50
+ const nonFilteredColumns = instance
51
+ .getAllLeafColumns()
52
+ .filter(
53
+ column =>
54
+ !instance.getState().columnFilters?.find(d => d.id === column.id)
55
+ )
56
+
57
+ // This essentially enables faceted filter options to be built easily
58
+ // using every column's preFilteredRows value
59
+
60
+ nonFilteredColumns.forEach(column => {
61
+ column.getPreFilteredRows = () => globalFilteredRowModel.rows
62
+ })
63
+
64
+ return globalFilteredRowModel
65
+ },
66
+ {
67
+ key: 'getGlobalFilteredRowModelSync',
68
+ debug: () => instance.options.debugAll ?? instance.options.debugTable,
69
+ onChange: () => {
70
+ instance._notifySortingReset()
71
+ },
72
+ }
73
+ )
74
+ }
@@ -0,0 +1,187 @@
1
+ import { TableInstance, Row, RowModel, AnyGenerics } from '../types'
2
+ import { flattenBy, memo } from '../utils'
3
+
4
+ export function getGroupedRowModelSync<TGenerics extends AnyGenerics>(): (
5
+ instance: TableInstance<TGenerics>
6
+ ) => () => RowModel<TGenerics> {
7
+ return instance =>
8
+ memo(
9
+ () => [instance.getState().grouping, instance.getSortedRowModel()],
10
+ (grouping, rowModel) => {
11
+ if (!rowModel.rows.length || !grouping.length) {
12
+ return rowModel
13
+ }
14
+
15
+ // Filter the grouping list down to columns that exist
16
+ const existingGrouping = grouping.filter(columnId =>
17
+ instance.getColumn(columnId)
18
+ )
19
+
20
+ // Find the columns that can or are aggregating
21
+ // Uses each column to aggregate rows into a single value
22
+ const aggregateRowsToValues = (
23
+ leafRows: Row<TGenerics>[],
24
+ groupedRows: Row<TGenerics>[],
25
+ depth: number
26
+ ) => {
27
+ const values: Record<string, unknown> = {}
28
+
29
+ instance.getAllLeafColumns().forEach(column => {
30
+ // Don't aggregate columns that are in the grouping
31
+ if (existingGrouping.includes(column.id)) {
32
+ values[column.id] = groupedRows[0]
33
+ ? groupedRows[0].values[column.id]
34
+ : null
35
+ return
36
+ }
37
+
38
+ // Aggregate the values
39
+ const aggregateFn = instance.getColumnAggregationFn(column.id)
40
+
41
+ if (aggregateFn) {
42
+ values[column.id] = aggregateFn(
43
+ () =>
44
+ leafRows.map(row => {
45
+ let columnValue = row.values[column.id]
46
+
47
+ if (!depth && column.aggregateValue) {
48
+ columnValue = column.aggregateValue(columnValue)
49
+ }
50
+
51
+ return columnValue
52
+ }),
53
+ () => groupedRows.map(row => row.values[column.id])
54
+ )
55
+ } else if (column.aggregationType) {
56
+ console.info({ column })
57
+ throw new Error(
58
+ process.env.NODE_ENV !== 'production'
59
+ ? `Table: Invalid column.aggregateType option for column listed above`
60
+ : ''
61
+ )
62
+ } else {
63
+ values[column.id] = null
64
+ }
65
+ })
66
+
67
+ return values
68
+ }
69
+
70
+ const groupedFlatRows: Row<TGenerics>[] = []
71
+ const groupedRowsById: Record<string, Row<TGenerics>> = {}
72
+ // const onlyGroupedFlatRows: Row[] = [];
73
+ // const onlyGroupedRowsById: Record<RowId, Row> = {};
74
+ // const nonGroupedFlatRows: Row[] = [];
75
+ // const nonGroupedRowsById: Record<RowId, Row> = {};
76
+
77
+ // Recursively group the data
78
+ const groupUpRecursively = (
79
+ rows: Row<TGenerics>[],
80
+ depth = 0,
81
+ parentId: string
82
+ ) => {
83
+ // This is the last level, just return the rows
84
+ if (depth === existingGrouping.length) {
85
+ return rows
86
+ }
87
+
88
+ const columnId = existingGrouping[depth]!
89
+
90
+ // Group the rows together for this level
91
+ const rowGroupsMap = groupBy(rows, columnId)
92
+
93
+ // Peform aggregations for each group
94
+ const aggregatedGroupedRows = Array.from(rowGroupsMap.entries()).map(
95
+ ([groupingValue, groupedRows], index) => {
96
+ let id = `${columnId}:${groupingValue}`
97
+ id = parentId ? `${parentId}>${id}` : id
98
+
99
+ // First, Recurse to group sub rows before aggregation
100
+ const subRows = groupUpRecursively(groupedRows, depth + 1, id)
101
+
102
+ // Flatten the leaf rows of the rows in this group
103
+ const leafRows = depth
104
+ ? flattenBy(groupedRows, row => row.subRows)
105
+ : groupedRows
106
+
107
+ const values = aggregateRowsToValues(leafRows, groupedRows, depth)
108
+
109
+ const row = instance.createRow(
110
+ id,
111
+ undefined,
112
+ index,
113
+ depth,
114
+ values
115
+ )
116
+
117
+ Object.assign(row, {
118
+ groupingColumnId: columnId,
119
+ groupingValue,
120
+ subRows,
121
+ leafRows,
122
+ })
123
+
124
+ subRows.forEach(subRow => {
125
+ groupedFlatRows.push(subRow)
126
+ groupedRowsById[subRow.id] = subRow
127
+ // if (subRow.getIsGrouped?.()) {
128
+ // onlyGroupedFlatRows.push(subRow);
129
+ // onlyGroupedRowsById[subRow.id] = subRow;
130
+ // } else {
131
+ // nonGroupedFlatRows.push(subRow);
132
+ // nonGroupedRowsById[subRow.id] = subRow;
133
+ // }
134
+ })
135
+
136
+ return row
137
+ }
138
+ )
139
+
140
+ return aggregatedGroupedRows
141
+ }
142
+
143
+ const groupedRows = groupUpRecursively(rowModel.rows, 0, '')
144
+
145
+ groupedRows.forEach(subRow => {
146
+ groupedFlatRows.push(subRow)
147
+ groupedRowsById[subRow.id] = subRow
148
+ // if (subRow.getIsGrouped?.()) {
149
+ // onlyGroupedFlatRows.push(subRow);
150
+ // onlyGroupedRowsById[subRow.id] = subRow;
151
+ // } else {
152
+ // nonGroupedFlatRows.push(subRow);
153
+ // nonGroupedRowsById[subRow.id] = subRow;
154
+ // }
155
+ })
156
+
157
+ return {
158
+ rows: groupedRows,
159
+ flatRows: groupedFlatRows,
160
+ rowsById: groupedRowsById,
161
+ }
162
+ },
163
+ {
164
+ key: 'getGroupedRowModel',
165
+ debug: () => instance.options.debugAll ?? instance.options.debugTable,
166
+ onChange: () => instance._notifyExpandedReset(),
167
+ }
168
+ )
169
+ }
170
+
171
+ function groupBy<TGenerics extends AnyGenerics>(
172
+ rows: Row<TGenerics>[],
173
+ columnId: string
174
+ ) {
175
+ const groupMap = new Map<any, Row<TGenerics>[]>()
176
+
177
+ return rows.reduce((map, row) => {
178
+ const resKey = `${row.values[columnId]}`
179
+ const previous = map.get(resKey)
180
+ if (!previous) {
181
+ map.set(resKey, [row])
182
+ } else {
183
+ map.set(resKey, [...previous, row])
184
+ }
185
+ return map
186
+ }, groupMap)
187
+ }
@@ -0,0 +1,45 @@
1
+ import { TableInstance, RowModel, AnyGenerics } from '../types'
2
+ import { memo } from '../utils'
3
+ import { expandRows } from './getExpandedRowModel'
4
+
5
+ export function getPaginationRowModel<TGenerics extends AnyGenerics>(opts?: {
6
+ initialSync: boolean
7
+ }): (instance: TableInstance<TGenerics>) => () => RowModel<TGenerics> {
8
+ return instance =>
9
+ memo(
10
+ () => [instance.getState().pagination, instance.getExpandedRowModel()],
11
+ (pagination, rowModel) => {
12
+ if (!rowModel.rows.length) {
13
+ return rowModel
14
+ }
15
+
16
+ const { pageSize, pageIndex } = pagination
17
+ let { rows, flatRows, rowsById } = rowModel
18
+ const pageStart = pageSize * pageIndex
19
+ const pageEnd = pageStart + pageSize
20
+
21
+ rows = rows.slice(pageStart, pageEnd)
22
+
23
+ if (!instance.options.paginateExpandedRows) {
24
+ return expandRows(
25
+ {
26
+ rows,
27
+ flatRows,
28
+ rowsById,
29
+ },
30
+ instance
31
+ )
32
+ }
33
+
34
+ return {
35
+ rows,
36
+ flatRows,
37
+ rowsById,
38
+ }
39
+ },
40
+ {
41
+ key: 'getPaginationRowModel',
42
+ debug: () => instance.options.debugAll ?? instance.options.debugTable,
43
+ }
44
+ )
45
+ }
@@ -0,0 +1,199 @@
1
+ import { TableInstance, Row, RowModel, AnyGenerics } from '../types'
2
+ import { SortingFn } from '../features/Sorting'
3
+ import { incrementalMemo, memo } from '../utils'
4
+
5
+ export function getSortedRowModelSync<TGenerics extends AnyGenerics>(opts?: {
6
+ initialSync?: boolean
7
+ }): (instance: TableInstance<TGenerics>) => () => RowModel<TGenerics> {
8
+ return instance =>
9
+ incrementalMemo(
10
+ () => [instance.getState().sorting, instance.getGlobalFilteredRowModel()],
11
+ (_sorting, rowModel): RowModel<TGenerics> => {
12
+ return {
13
+ rows: rowModel.rows.slice(),
14
+ flatRows: [],
15
+ rowsById: rowModel.rowsById,
16
+ }
17
+ },
18
+ (sorting, rowModel) => rowModelRef => scheduleTask => {
19
+ // TODO: Figure out how to do async sorting
20
+ // We probably need to use a sorting algo that is "divide and
21
+ // conquer", since that will probably distribute the easiest into
22
+ // separate tasks. The trick will be batching those tasks in a way
23
+ // that makes them fast. JS work loops don't tend to do well with
24
+ // a high number of tasks that do one thing. Instead, shoot for each
25
+ // task to have a few thousand opts (or whatever amount is generally
26
+ // fast for most devices).
27
+
28
+ throw new Error()
29
+
30
+ // if (!rowModel.rows.length || !sorting?.length) {
31
+ // rowModelRef.current = rowModel
32
+ // return
33
+ // }
34
+
35
+ // const sortingState = instance.getState().sorting
36
+
37
+ // const sortedFlatRows: Row<TGenerics>[] = []
38
+
39
+ // // Filter out sortings that correspond to non existing columns
40
+ // const availableSorting = sortingState.filter(sort =>
41
+ // instance.getColumnCanSort(sort.id)
42
+ // )
43
+
44
+ // const sorters = availableSorting.map(columnSorting => {
45
+ // const column = instance.getColumn(columnSorting.id)!
46
+ // const sortingFn = instance.getColumnSortingFn(columnSorting.id)!
47
+
48
+ // return {
49
+ // id: columnSorting.id,
50
+ // compare: (rowA: Row<TGenerics>, rowB: Row<TGenerics>) =>
51
+ // sortingFn(rowA, rowB, columnSorting.id),
52
+ // desc: columnSorting.desc,
53
+ // invertSorting: column.invertSorting,
54
+ // sortUndefined: column.sortUndefined,
55
+ // }
56
+ // })
57
+
58
+ // const sortData = (rows: Row<TGenerics>[]) => {
59
+ // // This will also perform a stable sorting using the row index
60
+ // // if needed.
61
+
62
+ // const sortedData = quicksort(rows, sorters)
63
+
64
+ // // If there are sub-rows, sort them
65
+ // sortedData.forEach(row => {
66
+ // rowModelRef.current.flatRows.push(row)
67
+ // if (!row.subRows || row.subRows.length <= 1) {
68
+ // return
69
+ // }
70
+ // row.subRows = sortData(row.subRows)
71
+ // })
72
+
73
+ // return sortedData
74
+ // }
75
+
76
+ // sortData(rowModelRef.current.rows)
77
+ },
78
+ {
79
+ key: 'getSortedRowModel',
80
+ initialSync: opts?.initialSync,
81
+ onProgress: progress => {
82
+ instance.setState(old => ({ ...old, sortingProgress: progress }))
83
+ },
84
+ debug: () => instance.options.debugAll ?? instance.options.debugTable,
85
+ onChange: () => {
86
+ instance._notifyGroupingReset()
87
+ },
88
+ }
89
+ )
90
+ }
91
+
92
+ type Sorter<TGenerics extends AnyGenerics> = {
93
+ id: string
94
+ compare: (a: Row<TGenerics>, b: Row<TGenerics>) => number
95
+ desc?: boolean
96
+ sortUndefined?: false | -1 | 1
97
+ invertSorting?: boolean
98
+ }
99
+
100
+ export function quicksort<TGenerics extends AnyGenerics>(
101
+ rows: Row<TGenerics>[],
102
+ sorters: Sorter<TGenerics>[]
103
+ ): Row<TGenerics>[] {
104
+ if (!rows.length) {
105
+ return rows
106
+ }
107
+
108
+ // Copy the rows so we can mutate the order
109
+ rows = rows.slice()
110
+
111
+ // Start the quicksort recursion
112
+ recurse(0, rows.length - 1)
113
+
114
+ // Return the rows
115
+ return rows
116
+
117
+ function recurse(start: number, end: number) {
118
+ const pivotIndex = pivot(start, end)
119
+
120
+ if (start < pivotIndex - 1) {
121
+ recurse(start, pivotIndex - 1)
122
+ }
123
+
124
+ if (pivotIndex < end) {
125
+ recurse(pivotIndex + 1, end)
126
+ }
127
+ }
128
+
129
+ function pivot(start: number, end: number) {
130
+ const pivotIndex = Math.floor((start + end) / 2)
131
+ let i = start
132
+ let j = end
133
+
134
+ const compare = (a: number, b: number) => {
135
+ const rowA = rows[a]
136
+ const rowB = rows[b]
137
+
138
+ for (let i = 0; i < sorters.length; i += 1) {
139
+ const sorter = sorters[i]!
140
+ const isDesc = sorter?.desc ?? false
141
+
142
+ if (sorter.sortUndefined) {
143
+ const aValue = rowA.values[sorter.id]
144
+ const bValue = rowB.values[sorter.id]
145
+
146
+ const aUndefined = typeof aValue === 'undefined'
147
+ const bUndefined = typeof bValue === 'undefined'
148
+
149
+ if (aUndefined || bUndefined) {
150
+ if (aUndefined !== bUndefined) {
151
+ return aUndefined ? sorter.sortUndefined : -sorter.sortUndefined
152
+ }
153
+ }
154
+ }
155
+
156
+ // This function should always return in ascending order
157
+ let sortInt = sorter.compare(rowA, rowB)
158
+
159
+ if (sortInt !== 0) {
160
+ if (isDesc) {
161
+ sortInt *= -1
162
+ }
163
+
164
+ if (sorter.invertSorting) {
165
+ sortInt *= -1
166
+ }
167
+
168
+ return sortInt
169
+ }
170
+ }
171
+
172
+ return rowA.index < rowB.index ? -1 : 1
173
+ }
174
+
175
+ while (i <= j) {
176
+ while (compare(i, pivotIndex) < 0) {
177
+ i++
178
+ }
179
+
180
+ while (compare(pivotIndex, j) < 0) {
181
+ j--
182
+ }
183
+
184
+ if (i <= j) {
185
+ swap(rows, i, j)
186
+ i++
187
+ j--
188
+ }
189
+ }
190
+
191
+ return i
192
+ }
193
+
194
+ function swap<T>(arr: T[], a: number, b: number) {
195
+ const t = arr[a]
196
+ arr[a] = arr[b]
197
+ arr[b] = t
198
+ }
199
+ }
@@ -0,0 +1,116 @@
1
+ import { TableInstance, Row, RowModel, AnyGenerics } from '../types'
2
+ import { SortingFn } from '../features/Sorting'
3
+ import { memo } from '../utils'
4
+
5
+ export function getSortedRowModelSync<TGenerics extends AnyGenerics>(): (
6
+ instance: TableInstance<TGenerics>
7
+ ) => () => RowModel<TGenerics> {
8
+ return instance =>
9
+ memo(
10
+ () => [instance.getState().sorting, instance.getGlobalFilteredRowModel()],
11
+ (sorting, rowModel) => {
12
+ if (!rowModel.rows.length || !sorting?.length) {
13
+ return rowModel
14
+ }
15
+
16
+ const sortingState = instance.getState().sorting
17
+
18
+ const sortedFlatRows: Row<TGenerics>[] = []
19
+
20
+ // Filter out sortings that correspond to non existing columns
21
+ const availableSorting = sortingState.filter(sort =>
22
+ instance.getColumnCanSort(sort.id)
23
+ )
24
+
25
+ const columnInfoById: Record<
26
+ string,
27
+ {
28
+ sortUndefined?: false | -1 | 1
29
+ invertSorting?: boolean
30
+ sortingFn: SortingFn<TGenerics>
31
+ }
32
+ > = {}
33
+
34
+ availableSorting.forEach(sortEntry => {
35
+ const column = instance.getColumn(sortEntry.id)!
36
+
37
+ columnInfoById[sortEntry.id] = {
38
+ sortUndefined: column.sortUndefined,
39
+ invertSorting: column.invertSorting,
40
+ sortingFn: instance.getColumnSortingFn(sortEntry.id)!,
41
+ }
42
+ })
43
+
44
+ const sortData = (rows: Row<TGenerics>[]) => {
45
+ // This will also perform a stable sorting using the row index
46
+ // if needed.
47
+ const sortedData = rows.slice()
48
+
49
+ sortedData.sort((rowA, rowB) => {
50
+ for (let i = 0; i < availableSorting.length; i += 1) {
51
+ const sortEntry = availableSorting[i]!
52
+ const columnInfo = columnInfoById[sortEntry.id]!
53
+ const isDesc = sortEntry?.desc ?? false
54
+
55
+ if (columnInfo.sortUndefined) {
56
+ const aValue = rowA.values[sortEntry.id]
57
+ const bValue = rowB.values[sortEntry.id]
58
+
59
+ const aUndefined = typeof aValue === 'undefined'
60
+ const bUndefined = typeof bValue === 'undefined'
61
+
62
+ if (aUndefined || bUndefined) {
63
+ return aUndefined && bUndefined
64
+ ? 0
65
+ : aUndefined
66
+ ? columnInfo.sortUndefined
67
+ : -columnInfo.sortUndefined
68
+ }
69
+ }
70
+
71
+ // This function should always return in ascending order
72
+ let sortInt = columnInfo.sortingFn(rowA, rowB, sortEntry.id)
73
+
74
+ if (sortInt !== 0) {
75
+ if (isDesc) {
76
+ sortInt *= -1
77
+ }
78
+
79
+ if (columnInfo.invertSorting) {
80
+ sortInt *= -1
81
+ }
82
+
83
+ return sortInt
84
+ }
85
+ }
86
+
87
+ return rowA.index - rowB.index
88
+ })
89
+
90
+ // If there are sub-rows, sort them
91
+ sortedData.forEach(row => {
92
+ sortedFlatRows.push(row)
93
+ if (!row.subRows || row.subRows.length <= 1) {
94
+ return
95
+ }
96
+ row.subRows = sortData(row.subRows)
97
+ })
98
+
99
+ return sortedData
100
+ }
101
+
102
+ return {
103
+ rows: sortData(rowModel.rows),
104
+ flatRows: sortedFlatRows,
105
+ rowsById: rowModel.rowsById,
106
+ }
107
+ },
108
+ {
109
+ key: 'getSortedRowModel',
110
+ debug: () => instance.options.debugAll ?? instance.options.debugTable,
111
+ onChange: () => {
112
+ instance._notifyGroupingReset()
113
+ },
114
+ }
115
+ )
116
+ }