@svgrid/grid 1.0.2 → 1.1.0
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/LICENSE +1 -1
- package/README.md +137 -39
- package/dist/GridFooter.svelte +164 -0
- package/dist/GridFooter.svelte.d.ts +29 -0
- package/dist/GridMenus.svelte +570 -0
- package/dist/GridMenus.svelte.d.ts +31 -0
- package/dist/SvGrid.controller.svelte.d.ts +429 -0
- package/dist/SvGrid.controller.svelte.js +1732 -0
- package/dist/SvGrid.css +1709 -0
- package/dist/SvGrid.helpers.d.ts +35 -0
- package/dist/SvGrid.helpers.js +160 -0
- package/dist/SvGrid.svelte +344 -7043
- package/dist/SvGrid.svelte.d.ts +4 -357
- package/dist/SvGrid.types.d.ts +436 -0
- package/dist/SvGrid.types.js +1 -0
- package/dist/SvGridChart.svelte +1060 -23
- package/dist/SvGridChart.svelte.d.ts +17 -0
- package/dist/build-api.d.ts +5 -0
- package/dist/build-api.js +527 -0
- package/dist/cell-render.d.ts +15 -0
- package/dist/cell-render.js +246 -0
- package/dist/cell-values.d.ts +28 -0
- package/dist/cell-values.js +89 -0
- package/dist/chart.d.ts +370 -3
- package/dist/chart.js +1135 -42
- package/dist/clipboard.d.ts +15 -0
- package/dist/clipboard.js +356 -0
- package/dist/columns.d.ts +30 -0
- package/dist/columns.js +277 -0
- package/dist/core.d.ts +1 -1
- package/dist/css.d.ts +3 -0
- package/dist/editing.d.ts +24 -0
- package/dist/editing.js +343 -0
- package/dist/editors/cell-editors.d.ts +1 -1
- package/dist/facet-buckets.d.ts +13 -0
- package/dist/facet-buckets.js +54 -0
- package/dist/features.d.ts +5 -0
- package/dist/features.js +30 -0
- package/dist/filter-operators.d.ts +16 -0
- package/dist/filter-operators.js +69 -0
- package/dist/hyperformula-adapter.d.ts +82 -0
- package/dist/hyperformula-adapter.js +73 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.js +5 -2
- package/dist/keyboard-handlers.d.ts +7 -0
- package/dist/keyboard-handlers.js +197 -0
- package/dist/menus.d.ts +40 -0
- package/dist/menus.js +389 -0
- package/dist/named-views.d.ts +27 -0
- package/dist/named-views.js +39 -0
- package/dist/row-resize.d.ts +43 -0
- package/dist/row-resize.js +158 -0
- package/dist/scroll-sync.d.ts +9 -0
- package/dist/scroll-sync.js +86 -0
- package/dist/selection.d.ts +26 -0
- package/dist/selection.js +387 -0
- package/dist/spreadsheet.d.ts +80 -0
- package/dist/spreadsheet.js +194 -0
- package/dist/summaries.d.ts +12 -0
- package/dist/summaries.js +65 -0
- package/dist/svgrid-wrapper.types.d.ts +12 -1
- package/dist/svgrid.comments-autocomplete.test.d.ts +1 -0
- package/dist/svgrid.comments-autocomplete.test.js +96 -0
- package/dist/svgrid.context-menu.test.d.ts +1 -0
- package/dist/svgrid.context-menu.test.js +102 -0
- package/dist/svgrid.new-features.wrapper.test.js +30 -4
- package/dist/svgrid.wrapper.test.js +27 -1
- package/dist/virtualization/column-virtualizer.d.ts +2 -0
- package/dist/virtualization/scroll-scaling.d.ts +28 -0
- package/dist/virtualization/scroll-scaling.js +64 -0
- package/dist/virtualization/scroll-scaling.test.d.ts +1 -0
- package/dist/virtualization/scroll-scaling.test.js +86 -0
- package/dist/virtualization/svelte-virtualizer.svelte.d.ts +2 -0
- package/dist/virtualization/svelte-virtualizer.svelte.js +2 -0
- package/dist/virtualization/virtualizer.d.ts +7 -0
- package/dist/virtualization/virtualizer.js +30 -0
- package/package.json +1 -1
- package/src/GridFooter.svelte +164 -0
- package/src/GridMenus.svelte +570 -0
- package/src/SvGrid.controller.svelte.ts +2195 -0
- package/src/SvGrid.css +1747 -0
- package/src/SvGrid.helpers.test.ts +415 -0
- package/src/SvGrid.helpers.ts +185 -0
- package/src/SvGrid.svelte +348 -7043
- package/src/SvGrid.types.ts +456 -0
- package/src/SvGridChart.svelte +1060 -23
- package/src/build-api.coverage.test.ts +532 -0
- package/src/build-api.ts +663 -0
- package/src/cell-render.test.ts +451 -0
- package/src/cell-render.ts +426 -0
- package/src/cell-values.ts +114 -0
- package/src/chart-export.test.ts +370 -0
- package/src/chart.coverage.test.ts +814 -0
- package/src/chart.ts +1352 -47
- package/src/clipboard.test.ts +731 -0
- package/src/clipboard.ts +524 -0
- package/src/collaboration.coverage.test.ts +220 -0
- package/src/columns.test.ts +702 -0
- package/src/columns.ts +419 -0
- package/src/core.ts +8 -0
- package/src/css.d.ts +3 -0
- package/src/editing.test.ts +837 -0
- package/src/editing.ts +513 -0
- package/src/editors/cell-editors.coverage.test.ts +156 -0
- package/src/editors/cell-editors.ts +1 -0
- package/src/facet-buckets.test.ts +353 -0
- package/src/facet-buckets.ts +67 -0
- package/src/features.ts +128 -0
- package/src/filter-operators.test.ts +174 -0
- package/src/filter-operators.ts +87 -0
- package/src/hyperformula-adapter.test.ts +256 -0
- package/src/hyperformula-adapter.ts +124 -0
- package/src/index.ts +37 -0
- package/src/keyboard-handlers.coverage.test.ts +560 -0
- package/src/keyboard-handlers.ts +353 -0
- package/src/keyboard.ts +97 -97
- package/src/menus.test.ts +620 -0
- package/src/menus.ts +554 -0
- package/src/named-views.coverage.test.ts +210 -0
- package/src/named-views.ts +48 -0
- package/src/row-resize.test.ts +369 -0
- package/src/row-resize.ts +171 -0
- package/src/scroll-sync.test.ts +330 -0
- package/src/scroll-sync.ts +216 -0
- package/src/selection.test.ts +722 -0
- package/src/selection.ts +545 -0
- package/src/server-data-source.coverage.test.ts +180 -0
- package/src/spreadsheet.test.ts +445 -0
- package/src/spreadsheet.ts +246 -0
- package/src/summaries.ts +204 -0
- package/src/sv-grid-scrollbar.ts +13 -1
- package/src/svgrid-wrapper.types.ts +12 -1
- package/src/svgrid.behavior.test.ts +22 -0
- package/src/svgrid.comments-autocomplete.test.ts +112 -0
- package/src/svgrid.context-menu.test.ts +126 -0
- package/src/svgrid.interaction.test.ts +30 -0
- package/src/svgrid.new-features.wrapper.test.ts +65 -4
- package/src/svgrid.wrapper.test.ts +27 -1
- package/src/test-setup.ts +9 -6
- package/src/virtualization/scroll-scaling.test.ts +148 -0
- package/src/virtualization/scroll-scaling.ts +121 -0
- package/src/virtualization/svelte-virtualizer.svelte.ts +2 -0
- package/src/virtualization/virtualizer.ts +26 -0
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for the facet value-bucketing helpers: isBucketableColumn,
|
|
3
|
+
* buildBuckets, isInBucket. These are pure functions - the only grid
|
|
4
|
+
* coupling is the Column type (we use createSvGrid to mint real columns
|
|
5
|
+
* carrying an `editorType`) and the rawToNumber/format helpers.
|
|
6
|
+
*/
|
|
7
|
+
import { describe, expect, it } from 'vitest'
|
|
8
|
+
import {
|
|
9
|
+
createCoreRowModel,
|
|
10
|
+
createSvGrid,
|
|
11
|
+
tableFeatures,
|
|
12
|
+
rowSortingFeature,
|
|
13
|
+
type Column,
|
|
14
|
+
type ColumnDef,
|
|
15
|
+
type RowData,
|
|
16
|
+
} from './index'
|
|
17
|
+
import {
|
|
18
|
+
isBucketableColumn,
|
|
19
|
+
buildBuckets,
|
|
20
|
+
isInBucket,
|
|
21
|
+
type FacetBucket,
|
|
22
|
+
} from './facet-buckets'
|
|
23
|
+
|
|
24
|
+
type Row = {
|
|
25
|
+
id: string
|
|
26
|
+
num: number
|
|
27
|
+
when: string
|
|
28
|
+
name: string
|
|
29
|
+
flag: boolean
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const features = tableFeatures({ rowSortingFeature })
|
|
33
|
+
|
|
34
|
+
/** Build a real grid and return its columns keyed by id so tests can grab
|
|
35
|
+
* a genuine Column<Row> object (with columnDef.editorType populated). */
|
|
36
|
+
function makeColumns(): Record<string, Column<Row>> {
|
|
37
|
+
const columns: ColumnDef<typeof features, Row>[] = [
|
|
38
|
+
{ field: 'id', header: 'ID', editorType: 'text' },
|
|
39
|
+
{ field: 'num', header: 'Number', editorType: 'number' },
|
|
40
|
+
{ field: 'when', header: 'When', editorType: 'date' },
|
|
41
|
+
{ field: 'name', header: 'Name' }, // no editorType
|
|
42
|
+
{ field: 'flag', header: 'Flag', editorType: 'checkbox' },
|
|
43
|
+
]
|
|
44
|
+
const grid = createSvGrid<typeof features, Row>({
|
|
45
|
+
_features: features,
|
|
46
|
+
_rowModels: { coreRowModel: createCoreRowModel<Row>() },
|
|
47
|
+
columns,
|
|
48
|
+
data: [],
|
|
49
|
+
})
|
|
50
|
+
const byId: Record<string, Column<Row>> = {}
|
|
51
|
+
for (const c of grid.getAllColumns()) byId[c.id] = c as Column<Row>
|
|
52
|
+
return byId
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Field accessor used by buildBuckets in production - read the property
|
|
56
|
+
// matching the column id off the row.
|
|
57
|
+
function accessor<TData extends RowData>(row: TData, column: Column<TData>): unknown {
|
|
58
|
+
return (row as Record<string, unknown>)[column.id]
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
describe('isBucketableColumn', () => {
|
|
62
|
+
const cols = makeColumns()
|
|
63
|
+
|
|
64
|
+
it('returns { isDate: false } for a number column', () => {
|
|
65
|
+
expect(isBucketableColumn(cols.num)).toEqual({ isDate: false })
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('returns { isDate: true } for a date column', () => {
|
|
69
|
+
expect(isBucketableColumn(cols.when)).toEqual({ isDate: true })
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('returns { isDate: true } for a datetime column', () => {
|
|
73
|
+
const grid = createSvGrid<typeof features, Row>({
|
|
74
|
+
_features: features,
|
|
75
|
+
_rowModels: { coreRowModel: createCoreRowModel<Row>() },
|
|
76
|
+
columns: [{ field: 'when', header: 'When', editorType: 'datetime' }],
|
|
77
|
+
data: [],
|
|
78
|
+
})
|
|
79
|
+
const col = grid.getAllColumns().find((c) => c.id === 'when') as Column<Row>
|
|
80
|
+
expect(isBucketableColumn(col)).toEqual({ isDate: true })
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('returns null for a text column', () => {
|
|
84
|
+
expect(isBucketableColumn(cols.id)).toBeNull()
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('returns null for a checkbox column', () => {
|
|
88
|
+
expect(isBucketableColumn(cols.flag)).toBeNull()
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('returns null for a column with no editorType', () => {
|
|
92
|
+
expect(isBucketableColumn(cols.name)).toBeNull()
|
|
93
|
+
})
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
describe('buildBuckets - numeric', () => {
|
|
97
|
+
const cols = makeColumns()
|
|
98
|
+
|
|
99
|
+
it('returns null when distinct values do not exceed the threshold (30)', () => {
|
|
100
|
+
// 30 distinct values -> not > 30 -> null
|
|
101
|
+
const data: Row[] = Array.from({ length: 30 }, (_, i) => ({
|
|
102
|
+
id: String(i),
|
|
103
|
+
num: i,
|
|
104
|
+
when: '',
|
|
105
|
+
name: '',
|
|
106
|
+
flag: false,
|
|
107
|
+
}))
|
|
108
|
+
expect(buildBuckets(cols.num, false, data, accessor)).toBeNull()
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('builds 10 buckets when distinct values exceed the threshold', () => {
|
|
112
|
+
// 31 distinct values 0..30 -> > 30 -> buckets
|
|
113
|
+
const data: Row[] = Array.from({ length: 31 }, (_, i) => ({
|
|
114
|
+
id: String(i),
|
|
115
|
+
num: i,
|
|
116
|
+
when: '',
|
|
117
|
+
name: '',
|
|
118
|
+
flag: false,
|
|
119
|
+
}))
|
|
120
|
+
const buckets = buildBuckets(cols.num, false, data, accessor)
|
|
121
|
+
expect(buckets).not.toBeNull()
|
|
122
|
+
expect(buckets!).toHaveLength(10)
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it('spans the full numeric range with contiguous edges', () => {
|
|
126
|
+
const data: Row[] = Array.from({ length: 31 }, (_, i) => ({
|
|
127
|
+
id: String(i),
|
|
128
|
+
num: i, // 0..30
|
|
129
|
+
when: '',
|
|
130
|
+
name: '',
|
|
131
|
+
flag: false,
|
|
132
|
+
}))
|
|
133
|
+
const buckets = buildBuckets(cols.num, false, data, accessor)!
|
|
134
|
+
expect(buckets[0].numericMin).toBe(0)
|
|
135
|
+
expect(buckets[9].numericMax).toBe(30)
|
|
136
|
+
// bucket i's max equals bucket i+1's min (contiguous, no gaps)
|
|
137
|
+
for (let i = 0; i < buckets.length - 1; i += 1) {
|
|
138
|
+
expect(buckets[i].numericMax).toBeCloseTo(buckets[i + 1].numericMin, 10)
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it('flags only the final bucket as isLast and sets isDate=false', () => {
|
|
143
|
+
const data: Row[] = Array.from({ length: 31 }, (_, i) => ({
|
|
144
|
+
id: String(i),
|
|
145
|
+
num: i,
|
|
146
|
+
when: '',
|
|
147
|
+
name: '',
|
|
148
|
+
flag: false,
|
|
149
|
+
}))
|
|
150
|
+
const buckets = buildBuckets(cols.num, false, data, accessor)!
|
|
151
|
+
expect(buckets.filter((b) => b.isLast)).toHaveLength(1)
|
|
152
|
+
expect(buckets[9].isLast).toBe(true)
|
|
153
|
+
expect(buckets[0].isLast).toBe(false)
|
|
154
|
+
expect(buckets.every((b) => b.isDate === false)).toBe(true)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('uses numeric labels containing an en-dash separator', () => {
|
|
158
|
+
const data: Row[] = Array.from({ length: 31 }, (_, i) => ({
|
|
159
|
+
id: String(i),
|
|
160
|
+
num: i,
|
|
161
|
+
when: '',
|
|
162
|
+
name: '',
|
|
163
|
+
flag: false,
|
|
164
|
+
}))
|
|
165
|
+
const buckets = buildBuckets(cols.num, false, data, accessor)!
|
|
166
|
+
expect(buckets[0].label).toContain('–')
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it('ignores non-finite / blank values when computing min/max and distinct', () => {
|
|
170
|
+
// 31 numeric values plus a pile of null/undefined/empty/NaN-ish ones
|
|
171
|
+
const data: Row[] = [
|
|
172
|
+
...Array.from({ length: 31 }, (_, i) => ({
|
|
173
|
+
id: String(i),
|
|
174
|
+
num: i,
|
|
175
|
+
when: '',
|
|
176
|
+
name: '',
|
|
177
|
+
flag: false,
|
|
178
|
+
})),
|
|
179
|
+
{ id: 'a', num: null as unknown as number, when: '', name: '', flag: false },
|
|
180
|
+
{ id: 'b', num: undefined as unknown as number, when: '', name: '', flag: false },
|
|
181
|
+
{ id: 'c', num: 'not-a-number' as unknown as number, when: '', name: '', flag: false },
|
|
182
|
+
]
|
|
183
|
+
const buckets = buildBuckets(cols.num, false, data, accessor)!
|
|
184
|
+
// range is still 0..30 - the junk rows were skipped
|
|
185
|
+
expect(buckets[0].numericMin).toBe(0)
|
|
186
|
+
expect(buckets[9].numericMax).toBe(30)
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
it('returns null when all finite values are identical (min === max)', () => {
|
|
190
|
+
// 40 rows but every num is 5 -> distinct.size === 1 -> <= threshold -> null.
|
|
191
|
+
// (Also exercises the min === max guard via a distinct-but-uniform case below.)
|
|
192
|
+
const data: Row[] = Array.from({ length: 40 }, (_, i) => ({
|
|
193
|
+
id: String(i),
|
|
194
|
+
num: 5,
|
|
195
|
+
when: '',
|
|
196
|
+
name: '',
|
|
197
|
+
flag: false,
|
|
198
|
+
}))
|
|
199
|
+
expect(buildBuckets(cols.num, false, data, accessor)).toBeNull()
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
it('returns null when there is no finite data at all', () => {
|
|
203
|
+
const data: Row[] = Array.from({ length: 40 }, (_, i) => ({
|
|
204
|
+
id: String(i),
|
|
205
|
+
num: null as unknown as number,
|
|
206
|
+
when: '',
|
|
207
|
+
name: '',
|
|
208
|
+
flag: false,
|
|
209
|
+
}))
|
|
210
|
+
expect(buildBuckets(cols.num, false, data, accessor)).toBeNull()
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
it('returns null for an empty dataset', () => {
|
|
214
|
+
expect(buildBuckets(cols.num, false, [], accessor)).toBeNull()
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
it('handles negative ranges', () => {
|
|
218
|
+
const data: Row[] = Array.from({ length: 41 }, (_, i) => ({
|
|
219
|
+
id: String(i),
|
|
220
|
+
num: i - 20, // -20..20, 41 distinct
|
|
221
|
+
when: '',
|
|
222
|
+
name: '',
|
|
223
|
+
flag: false,
|
|
224
|
+
}))
|
|
225
|
+
const buckets = buildBuckets(cols.num, false, data, accessor)!
|
|
226
|
+
expect(buckets[0].numericMin).toBe(-20)
|
|
227
|
+
expect(buckets[9].numericMax).toBe(20)
|
|
228
|
+
})
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
describe('buildBuckets - date', () => {
|
|
232
|
+
const cols = makeColumns()
|
|
233
|
+
|
|
234
|
+
it('builds buckets from date strings with isDate=true and date labels', () => {
|
|
235
|
+
const start = Date.UTC(2020, 0, 1)
|
|
236
|
+
const day = 24 * 60 * 60 * 1000
|
|
237
|
+
const data: Row[] = Array.from({ length: 40 }, (_, i) => ({
|
|
238
|
+
id: String(i),
|
|
239
|
+
num: 0,
|
|
240
|
+
when: new Date(start + i * day).toISOString(),
|
|
241
|
+
name: '',
|
|
242
|
+
flag: false,
|
|
243
|
+
}))
|
|
244
|
+
const buckets = buildBuckets(cols.when, true, data, accessor)!
|
|
245
|
+
expect(buckets).toHaveLength(10)
|
|
246
|
+
expect(buckets.every((b) => b.isDate === true)).toBe(true)
|
|
247
|
+
// labels should look like dates (contain a 4-digit year)
|
|
248
|
+
expect(buckets[0].label).toMatch(/\d{4}/)
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
it('accepts Date instances directly', () => {
|
|
252
|
+
const start = Date.UTC(2020, 0, 1)
|
|
253
|
+
const day = 24 * 60 * 60 * 1000
|
|
254
|
+
const data: Array<{ id: string; when: Date }> = Array.from(
|
|
255
|
+
{ length: 40 },
|
|
256
|
+
(_, i) => ({ id: 'when' + i, when: new Date(start + i * day) }),
|
|
257
|
+
)
|
|
258
|
+
// accessor reads row[column.id]; column id is 'when'
|
|
259
|
+
const rows = data.map((d) => ({ when: d.when })) as unknown as Row[]
|
|
260
|
+
const buckets = buildBuckets(cols.when, true, rows, accessor)!
|
|
261
|
+
expect(buckets).toHaveLength(10)
|
|
262
|
+
expect(buckets[0].numericMin).toBe(start)
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
it('returns null when date values are too few', () => {
|
|
266
|
+
const data: Row[] = Array.from({ length: 5 }, (_, i) => ({
|
|
267
|
+
id: String(i),
|
|
268
|
+
num: 0,
|
|
269
|
+
when: new Date(Date.UTC(2020, 0, 1 + i)).toISOString(),
|
|
270
|
+
name: '',
|
|
271
|
+
flag: false,
|
|
272
|
+
}))
|
|
273
|
+
expect(buildBuckets(cols.when, true, data, accessor)).toBeNull()
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
it('skips invalid date strings', () => {
|
|
277
|
+
const start = Date.UTC(2021, 0, 1)
|
|
278
|
+
const day = 24 * 60 * 60 * 1000
|
|
279
|
+
const data: Row[] = [
|
|
280
|
+
...Array.from({ length: 40 }, (_, i) => ({
|
|
281
|
+
id: String(i),
|
|
282
|
+
num: 0,
|
|
283
|
+
when: new Date(start + i * day).toISOString(),
|
|
284
|
+
name: '',
|
|
285
|
+
flag: false,
|
|
286
|
+
})),
|
|
287
|
+
{ id: 'bad', num: 0, when: 'not-a-date', name: '', flag: false },
|
|
288
|
+
]
|
|
289
|
+
const buckets = buildBuckets(cols.when, true, data, accessor)!
|
|
290
|
+
expect(buckets).toHaveLength(10)
|
|
291
|
+
expect(buckets[0].numericMin).toBe(start)
|
|
292
|
+
})
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
describe('isInBucket', () => {
|
|
296
|
+
const mid: FacetBucket = {
|
|
297
|
+
label: '10 - 20',
|
|
298
|
+
numericMin: 10,
|
|
299
|
+
numericMax: 20,
|
|
300
|
+
isLast: false,
|
|
301
|
+
isDate: false,
|
|
302
|
+
}
|
|
303
|
+
const last: FacetBucket = {
|
|
304
|
+
label: '20 - 30',
|
|
305
|
+
numericMin: 20,
|
|
306
|
+
numericMax: 30,
|
|
307
|
+
isLast: true,
|
|
308
|
+
isDate: false,
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
it('returns false for non-finite numbers', () => {
|
|
312
|
+
expect(isInBucket(Number.NaN, mid)).toBe(false)
|
|
313
|
+
expect(isInBucket(Number.POSITIVE_INFINITY, mid)).toBe(false)
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
it('returns false below the lower bound', () => {
|
|
317
|
+
expect(isInBucket(9.999, mid)).toBe(false)
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
it('includes the lower bound', () => {
|
|
321
|
+
expect(isInBucket(10, mid)).toBe(true)
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
it('excludes the upper bound for a non-last bucket (half-open interval)', () => {
|
|
325
|
+
expect(isInBucket(20, mid)).toBe(false)
|
|
326
|
+
expect(isInBucket(19.999, mid)).toBe(true)
|
|
327
|
+
})
|
|
328
|
+
|
|
329
|
+
it('includes the upper bound for the last bucket (closed interval)', () => {
|
|
330
|
+
expect(isInBucket(30, last)).toBe(true)
|
|
331
|
+
expect(isInBucket(20, last)).toBe(true)
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
it('rejects values past the upper bound even on the last bucket', () => {
|
|
335
|
+
expect(isInBucket(30.0001, last)).toBe(false)
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
it('partitions a value into exactly one bucket across a built set', () => {
|
|
339
|
+
const cols = makeColumns()
|
|
340
|
+
const data: Row[] = Array.from({ length: 101 }, (_, i) => ({
|
|
341
|
+
id: String(i),
|
|
342
|
+
num: i, // 0..100
|
|
343
|
+
when: '',
|
|
344
|
+
name: '',
|
|
345
|
+
flag: false,
|
|
346
|
+
}))
|
|
347
|
+
const buckets = buildBuckets(cols.num, false, data, accessor)!
|
|
348
|
+
for (const v of [0, 1, 15, 50, 99, 100]) {
|
|
349
|
+
const hits = buckets.filter((b) => isInBucket(v, b))
|
|
350
|
+
expect(hits).toHaveLength(1)
|
|
351
|
+
}
|
|
352
|
+
})
|
|
353
|
+
})
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Facet value-bucketing: turns a high-cardinality numeric/date column into a
|
|
2
|
+
// fixed set of range buckets for the filter menu. Pure - the caller passes the
|
|
3
|
+
// row data and an accessor, so this holds no grid state.
|
|
4
|
+
import type { Column, RowData } from "./index";
|
|
5
|
+
import { rawToNumber, formatFacetDate, formatFacetNumber } from "./SvGrid.helpers";
|
|
6
|
+
|
|
7
|
+
export type FacetBucket = {
|
|
8
|
+
label: string;
|
|
9
|
+
numericMin: number;
|
|
10
|
+
numericMax: number;
|
|
11
|
+
isLast: boolean;
|
|
12
|
+
isDate: boolean;
|
|
13
|
+
};
|
|
14
|
+
const FACET_BUCKET_THRESHOLD = 30;
|
|
15
|
+
const FACET_BUCKET_COUNT = 10;
|
|
16
|
+
|
|
17
|
+
export function isBucketableColumn<TData extends RowData>(column: Column<TData>): { isDate: boolean } | null {
|
|
18
|
+
const editorType = column.columnDef.editorType;
|
|
19
|
+
if (editorType === "number") return { isDate: false };
|
|
20
|
+
if (editorType === "date" || editorType === "datetime") return { isDate: true };
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function buildBuckets<TData extends RowData>(
|
|
25
|
+
column: Column<TData>,
|
|
26
|
+
isDate: boolean,
|
|
27
|
+
data: ReadonlyArray<TData>,
|
|
28
|
+
getAccessor: (row: TData, column: Column<TData>) => unknown,
|
|
29
|
+
): Array<FacetBucket> | null {
|
|
30
|
+
let min = Number.POSITIVE_INFINITY;
|
|
31
|
+
let max = Number.NEGATIVE_INFINITY;
|
|
32
|
+
const distinct = new Set<number>();
|
|
33
|
+
for (const rowData of data) {
|
|
34
|
+
const num = rawToNumber(getAccessor(rowData, column), isDate);
|
|
35
|
+
if (!Number.isFinite(num)) continue;
|
|
36
|
+
distinct.add(num);
|
|
37
|
+
if (num < min) min = num;
|
|
38
|
+
if (num > max) max = num;
|
|
39
|
+
}
|
|
40
|
+
if (distinct.size <= FACET_BUCKET_THRESHOLD) return null;
|
|
41
|
+
if (!Number.isFinite(min) || !Number.isFinite(max) || min === max)
|
|
42
|
+
return null;
|
|
43
|
+
|
|
44
|
+
const span = max - min;
|
|
45
|
+
const buckets: Array<FacetBucket> = [];
|
|
46
|
+
for (let i = 0; i < FACET_BUCKET_COUNT; i += 1) {
|
|
47
|
+
const lo = min + (span * i) / FACET_BUCKET_COUNT;
|
|
48
|
+
const hi = min + (span * (i + 1)) / FACET_BUCKET_COUNT;
|
|
49
|
+
const label = isDate
|
|
50
|
+
? `${formatFacetDate(lo)} – ${formatFacetDate(hi)}`
|
|
51
|
+
: `${formatFacetNumber(lo)} – ${formatFacetNumber(hi)}`;
|
|
52
|
+
buckets.push({
|
|
53
|
+
label,
|
|
54
|
+
numericMin: lo,
|
|
55
|
+
numericMax: hi,
|
|
56
|
+
isLast: i === FACET_BUCKET_COUNT - 1,
|
|
57
|
+
isDate,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return buckets;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function isInBucket(num: number, bucket: FacetBucket): boolean {
|
|
64
|
+
if (!Number.isFinite(num)) return false;
|
|
65
|
+
if (num < bucket.numericMin) return false;
|
|
66
|
+
return bucket.isLast ? num <= bucket.numericMax : num < bucket.numericMax;
|
|
67
|
+
}
|
package/src/features.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// features handlers extracted from the controller. Imperative event handlers
|
|
2
|
+
// reading/writing controller state via the `ctx` handle; the reactive core
|
|
3
|
+
// ($state/$derived/$effect) stays in the controller.
|
|
4
|
+
import {
|
|
5
|
+
applyExcelFilter,
|
|
6
|
+
normalizeForFilter,
|
|
7
|
+
createColumnVirtualizer,
|
|
8
|
+
createCoreRowModel,
|
|
9
|
+
createExpandedRowModel,
|
|
10
|
+
createFilteredRowModel,
|
|
11
|
+
createGroupedRowModel,
|
|
12
|
+
createSvelteVirtualizer,
|
|
13
|
+
createSortedRowModel,
|
|
14
|
+
createSvGrid,
|
|
15
|
+
getGridCellDomId,
|
|
16
|
+
sortFns,
|
|
17
|
+
tableFeatures,
|
|
18
|
+
rowSortingFeature,
|
|
19
|
+
columnFilteringFeature,
|
|
20
|
+
columnGroupingFeature,
|
|
21
|
+
type CellEditorOption,
|
|
22
|
+
type Column,
|
|
23
|
+
type ColumnDef,
|
|
24
|
+
type Row,
|
|
25
|
+
type RowData,
|
|
26
|
+
type TableFeatures,
|
|
27
|
+
} from "./index";
|
|
28
|
+
import "./sv-grid-scrollbar";
|
|
29
|
+
import {
|
|
30
|
+
computeColumnStat,
|
|
31
|
+
formatsNeedingStats,
|
|
32
|
+
type ColumnStat,
|
|
33
|
+
} from "./conditional-formatting";
|
|
34
|
+
import SvGridDropdown from "./SvGridDropdown.svelte";
|
|
35
|
+
import type {
|
|
36
|
+
Props,
|
|
37
|
+
SelectionRange,
|
|
38
|
+
CellEditState,
|
|
39
|
+
FilterOperator,
|
|
40
|
+
MenuPosition,
|
|
41
|
+
} from "./SvGrid.types";
|
|
42
|
+
import {
|
|
43
|
+
rawToNumber,
|
|
44
|
+
} from "./SvGrid.helpers";
|
|
45
|
+
import {
|
|
46
|
+
createScrollSync,
|
|
47
|
+
} from "./scroll-sync";
|
|
48
|
+
import {
|
|
49
|
+
createKeyboard,
|
|
50
|
+
} from "./keyboard-handlers";
|
|
51
|
+
import {
|
|
52
|
+
createSummaries,
|
|
53
|
+
} from "./summaries";
|
|
54
|
+
import {
|
|
55
|
+
createMenus,
|
|
56
|
+
} from "./menus";
|
|
57
|
+
import {
|
|
58
|
+
createCellRender,
|
|
59
|
+
} from "./cell-render";
|
|
60
|
+
import {
|
|
61
|
+
createEditing,
|
|
62
|
+
} from "./editing";
|
|
63
|
+
import {
|
|
64
|
+
createSelection,
|
|
65
|
+
} from "./selection";
|
|
66
|
+
import {
|
|
67
|
+
createColumns,
|
|
68
|
+
} from "./columns";
|
|
69
|
+
import {
|
|
70
|
+
createGridApi,
|
|
71
|
+
} from "./build-api";
|
|
72
|
+
import {
|
|
73
|
+
createClipboard,
|
|
74
|
+
} from "./clipboard";
|
|
75
|
+
import {
|
|
76
|
+
filterOperatorOptions,
|
|
77
|
+
fallbackOperatorOption,
|
|
78
|
+
TEXT_OPERATORS,
|
|
79
|
+
NUMBER_OPERATORS,
|
|
80
|
+
DATE_OPERATORS,
|
|
81
|
+
CHECKBOX_OPERATORS,
|
|
82
|
+
operatorOption,
|
|
83
|
+
operatorsForColumn,
|
|
84
|
+
defaultOperatorFor,
|
|
85
|
+
operatorLabelFor,
|
|
86
|
+
} from "./filter-operators";
|
|
87
|
+
import {
|
|
88
|
+
type FacetBucket,
|
|
89
|
+
isBucketableColumn,
|
|
90
|
+
buildBuckets,
|
|
91
|
+
isInBucket,
|
|
92
|
+
} from "./facet-buckets";
|
|
93
|
+
import {
|
|
94
|
+
getColumnBaseValue,
|
|
95
|
+
isGroupRow,
|
|
96
|
+
toolPanelHeaderLabel,
|
|
97
|
+
formatSummaryNumeric,
|
|
98
|
+
getColumnAlign,
|
|
99
|
+
getPinnedCellValue,
|
|
100
|
+
getColumnAccessorValue,
|
|
101
|
+
columnDefMatchesId,
|
|
102
|
+
} from "./cell-values";
|
|
103
|
+
|
|
104
|
+
export function createFeatures<
|
|
105
|
+
TFeatures extends TableFeatures = TableFeatures,
|
|
106
|
+
TData extends RowData = RowData,
|
|
107
|
+
>(ctx: any) {
|
|
108
|
+
// Merge the consumer's `features` set with whatever the boolean shortcuts
|
|
109
|
+
// imply. A shortcut set to `true` injects its feature; set to `false` it
|
|
110
|
+
// removes it (so `sortable={false}` wins even if `rowSortingFeature` was
|
|
111
|
+
// passed in `features`); left undefined it defers to `features`.
|
|
112
|
+
function resolveEffectiveFeatures(): TableFeatures {
|
|
113
|
+
const merged: Record<string, unknown> = { ...(ctx.props.features ?? {}) };
|
|
114
|
+
if (ctx.props.sortable === true) merged.rowSortingFeature = rowSortingFeature;
|
|
115
|
+
else if (ctx.props.sortable === false) delete merged.rowSortingFeature;
|
|
116
|
+
if (ctx.props.filterable === true)
|
|
117
|
+
merged.columnFilteringFeature = columnFilteringFeature;
|
|
118
|
+
else if (ctx.props.filterable === false) delete merged.columnFilteringFeature;
|
|
119
|
+
if (ctx.props.groupable === true)
|
|
120
|
+
merged.columnGroupingFeature = columnGroupingFeature;
|
|
121
|
+
else if (ctx.props.groupable === false) delete merged.columnGroupingFeature;
|
|
122
|
+
return tableFeatures(merged);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
resolveEffectiveFeatures,
|
|
127
|
+
};
|
|
128
|
+
}
|