@svgrid/grid 1.0.2 → 1.1.1

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 (216) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +137 -39
  3. package/dist/GridFooter.svelte +164 -0
  4. package/dist/GridFooter.svelte.d.ts +29 -0
  5. package/dist/GridMenus.svelte +648 -0
  6. package/dist/GridMenus.svelte.d.ts +31 -0
  7. package/dist/SvGrid.controller.svelte.d.ts +472 -0
  8. package/dist/SvGrid.controller.svelte.js +1917 -0
  9. package/dist/SvGrid.css +2012 -0
  10. package/dist/SvGrid.helpers.d.ts +35 -0
  11. package/dist/SvGrid.helpers.js +160 -0
  12. package/dist/SvGrid.helpers.test.d.ts +1 -0
  13. package/dist/SvGrid.helpers.test.js +298 -0
  14. package/dist/SvGrid.svelte +2346 -8742
  15. package/dist/SvGrid.svelte.d.ts +4 -357
  16. package/dist/SvGrid.types.d.ts +523 -0
  17. package/dist/SvGrid.types.js +1 -0
  18. package/dist/SvGridChart.svelte +1060 -23
  19. package/dist/SvGridChart.svelte.d.ts +17 -0
  20. package/dist/aligned-grids.d.ts +6 -0
  21. package/dist/aligned-grids.js +84 -0
  22. package/dist/aligned-grids.test.d.ts +1 -0
  23. package/dist/aligned-grids.test.js +75 -0
  24. package/dist/build-api.coverage.test.d.ts +20 -0
  25. package/dist/build-api.coverage.test.js +505 -0
  26. package/dist/build-api.d.ts +5 -0
  27. package/dist/build-api.js +557 -0
  28. package/dist/cell-render.d.ts +15 -0
  29. package/dist/cell-render.js +246 -0
  30. package/dist/cell-render.test.d.ts +1 -0
  31. package/dist/cell-render.test.js +338 -0
  32. package/dist/cell-values.d.ts +28 -0
  33. package/dist/cell-values.js +89 -0
  34. package/dist/chart-export.test.d.ts +1 -0
  35. package/dist/chart-export.test.js +302 -0
  36. package/dist/chart.coverage.test.d.ts +1 -0
  37. package/dist/chart.coverage.test.js +748 -0
  38. package/dist/chart.d.ts +370 -3
  39. package/dist/chart.js +1135 -42
  40. package/dist/clipboard.d.ts +15 -0
  41. package/dist/clipboard.js +420 -0
  42. package/dist/clipboard.test.d.ts +1 -0
  43. package/dist/clipboard.test.js +700 -0
  44. package/dist/collaboration.coverage.test.d.ts +1 -0
  45. package/dist/collaboration.coverage.test.js +200 -0
  46. package/dist/column-groups.d.ts +19 -0
  47. package/dist/column-groups.js +62 -0
  48. package/dist/column-groups.test.d.ts +1 -0
  49. package/dist/column-groups.test.js +56 -0
  50. package/dist/column-types.d.ts +10 -0
  51. package/dist/column-types.js +63 -0
  52. package/dist/column-types.test.d.ts +1 -0
  53. package/dist/column-types.test.js +62 -0
  54. package/dist/columns.d.ts +30 -0
  55. package/dist/columns.js +277 -0
  56. package/dist/columns.test.d.ts +1 -0
  57. package/dist/columns.test.js +625 -0
  58. package/dist/core.d.ts +86 -1
  59. package/dist/css.d.ts +3 -0
  60. package/dist/editing.d.ts +31 -0
  61. package/dist/editing.js +529 -0
  62. package/dist/editing.test.d.ts +1 -0
  63. package/dist/editing.test.js +732 -0
  64. package/dist/editors/cell-editors.coverage.test.d.ts +1 -0
  65. package/dist/editors/cell-editors.coverage.test.js +139 -0
  66. package/dist/editors/cell-editors.d.ts +1 -1
  67. package/dist/facet-buckets.d.ts +13 -0
  68. package/dist/facet-buckets.js +54 -0
  69. package/dist/facet-buckets.test.d.ts +1 -0
  70. package/dist/facet-buckets.test.js +296 -0
  71. package/dist/features.d.ts +5 -0
  72. package/dist/features.js +30 -0
  73. package/dist/filter-operators.d.ts +16 -0
  74. package/dist/filter-operators.js +69 -0
  75. package/dist/filter-operators.test.d.ts +1 -0
  76. package/dist/filter-operators.test.js +135 -0
  77. package/dist/hyperformula-adapter.d.ts +82 -0
  78. package/dist/hyperformula-adapter.js +73 -0
  79. package/dist/hyperformula-adapter.test.d.ts +1 -0
  80. package/dist/hyperformula-adapter.test.js +205 -0
  81. package/dist/index.d.ts +9 -3
  82. package/dist/index.js +8 -2
  83. package/dist/keyboard-handlers.coverage.test.d.ts +1 -0
  84. package/dist/keyboard-handlers.coverage.test.js +495 -0
  85. package/dist/keyboard-handlers.d.ts +7 -0
  86. package/dist/keyboard-handlers.js +202 -0
  87. package/dist/menus.d.ts +40 -0
  88. package/dist/menus.js +390 -0
  89. package/dist/menus.test.d.ts +1 -0
  90. package/dist/menus.test.js +560 -0
  91. package/dist/named-views.coverage.test.d.ts +1 -0
  92. package/dist/named-views.coverage.test.js +180 -0
  93. package/dist/named-views.d.ts +27 -0
  94. package/dist/named-views.js +39 -0
  95. package/dist/row-drag.d.ts +49 -0
  96. package/dist/row-drag.js +221 -0
  97. package/dist/row-drag.test.d.ts +1 -0
  98. package/dist/row-drag.test.js +142 -0
  99. package/dist/row-resize.d.ts +43 -0
  100. package/dist/row-resize.js +158 -0
  101. package/dist/row-resize.test.d.ts +1 -0
  102. package/dist/row-resize.test.js +329 -0
  103. package/dist/scroll-sync.d.ts +9 -0
  104. package/dist/scroll-sync.js +89 -0
  105. package/dist/scroll-sync.test.d.ts +1 -0
  106. package/dist/scroll-sync.test.js +290 -0
  107. package/dist/selection.d.ts +32 -0
  108. package/dist/selection.js +427 -0
  109. package/dist/selection.multi-range.test.d.ts +1 -0
  110. package/dist/selection.multi-range.test.js +55 -0
  111. package/dist/selection.test.d.ts +1 -0
  112. package/dist/selection.test.js +647 -0
  113. package/dist/server-data-source.coverage.test.d.ts +1 -0
  114. package/dist/server-data-source.coverage.test.js +154 -0
  115. package/dist/spreadsheet.d.ts +110 -0
  116. package/dist/spreadsheet.js +242 -0
  117. package/dist/spreadsheet.test.d.ts +1 -0
  118. package/dist/spreadsheet.test.js +446 -0
  119. package/dist/summaries.d.ts +12 -0
  120. package/dist/summaries.js +65 -0
  121. package/dist/sv-grid-scrollbar.js +13 -1
  122. package/dist/svgrid-wrapper.types.d.ts +31 -1
  123. package/dist/svgrid.behavior.test.js +20 -0
  124. package/dist/svgrid.comments-autocomplete.test.d.ts +1 -0
  125. package/dist/svgrid.comments-autocomplete.test.js +96 -0
  126. package/dist/svgrid.context-menu.test.d.ts +1 -0
  127. package/dist/svgrid.context-menu.test.js +102 -0
  128. package/dist/svgrid.interaction.test.js +31 -0
  129. package/dist/svgrid.new-features.wrapper.test.js +64 -6
  130. package/dist/svgrid.wrapper.test.js +27 -1
  131. package/dist/test-setup.js +9 -3
  132. package/dist/virtualization/column-virtualizer.d.ts +2 -0
  133. package/dist/virtualization/scroll-scaling.d.ts +45 -0
  134. package/dist/virtualization/scroll-scaling.js +99 -0
  135. package/dist/virtualization/scroll-scaling.test.d.ts +1 -0
  136. package/dist/virtualization/scroll-scaling.test.js +127 -0
  137. package/dist/virtualization/svelte-virtualizer.svelte.d.ts +2 -0
  138. package/dist/virtualization/svelte-virtualizer.svelte.js +2 -0
  139. package/dist/virtualization/virtualizer.d.ts +7 -0
  140. package/dist/virtualization/virtualizer.js +30 -0
  141. package/package.json +2 -1
  142. package/src/GridFooter.svelte +164 -0
  143. package/src/GridMenus.svelte +648 -0
  144. package/src/SvGrid.controller.svelte.ts +2352 -0
  145. package/src/SvGrid.css +2012 -0
  146. package/src/SvGrid.helpers.test.ts +415 -0
  147. package/src/SvGrid.helpers.ts +185 -0
  148. package/src/SvGrid.svelte +2346 -8742
  149. package/src/SvGrid.types.ts +537 -0
  150. package/src/SvGridChart.svelte +1060 -23
  151. package/src/aligned-grids.test.ts +80 -0
  152. package/src/aligned-grids.ts +87 -0
  153. package/src/build-api.coverage.test.ts +532 -0
  154. package/src/build-api.ts +683 -0
  155. package/src/cell-render.test.ts +451 -0
  156. package/src/cell-render.ts +426 -0
  157. package/src/cell-values.ts +114 -0
  158. package/src/chart-export.test.ts +370 -0
  159. package/src/chart.coverage.test.ts +814 -0
  160. package/src/chart.ts +1352 -47
  161. package/src/clipboard.test.ts +780 -0
  162. package/src/clipboard.ts +552 -0
  163. package/src/collaboration.coverage.test.ts +220 -0
  164. package/src/column-groups.test.ts +59 -0
  165. package/src/column-groups.ts +80 -0
  166. package/src/column-types.test.ts +68 -0
  167. package/src/column-types.ts +82 -0
  168. package/src/columns.test.ts +702 -0
  169. package/src/columns.ts +419 -0
  170. package/src/core.ts +86 -0
  171. package/src/css.d.ts +3 -0
  172. package/src/editing.test.ts +837 -0
  173. package/src/editing.ts +669 -0
  174. package/src/editors/cell-editors.coverage.test.ts +156 -0
  175. package/src/editors/cell-editors.ts +1 -0
  176. package/src/facet-buckets.test.ts +353 -0
  177. package/src/facet-buckets.ts +67 -0
  178. package/src/features.ts +128 -0
  179. package/src/filter-operators.test.ts +174 -0
  180. package/src/filter-operators.ts +87 -0
  181. package/src/hyperformula-adapter.test.ts +256 -0
  182. package/src/hyperformula-adapter.ts +124 -0
  183. package/src/index.ts +49 -0
  184. package/src/keyboard-handlers.coverage.test.ts +560 -0
  185. package/src/keyboard-handlers.ts +353 -0
  186. package/src/keyboard.ts +97 -97
  187. package/src/menus.test.ts +620 -0
  188. package/src/menus.ts +555 -0
  189. package/src/named-views.coverage.test.ts +210 -0
  190. package/src/named-views.ts +48 -0
  191. package/src/row-drag.test.ts +168 -0
  192. package/src/row-drag.ts +255 -0
  193. package/src/row-resize.test.ts +369 -0
  194. package/src/row-resize.ts +171 -0
  195. package/src/scroll-sync.test.ts +330 -0
  196. package/src/scroll-sync.ts +218 -0
  197. package/src/selection.multi-range.test.ts +61 -0
  198. package/src/selection.test.ts +722 -0
  199. package/src/selection.ts +579 -0
  200. package/src/server-data-source.coverage.test.ts +180 -0
  201. package/src/spreadsheet.test.ts +489 -0
  202. package/src/spreadsheet.ts +304 -0
  203. package/src/summaries.ts +204 -0
  204. package/src/sv-grid-scrollbar.ts +13 -1
  205. package/src/svgrid-wrapper.types.ts +31 -1
  206. package/src/svgrid.behavior.test.ts +22 -0
  207. package/src/svgrid.comments-autocomplete.test.ts +112 -0
  208. package/src/svgrid.context-menu.test.ts +126 -0
  209. package/src/svgrid.interaction.test.ts +30 -0
  210. package/src/svgrid.new-features.wrapper.test.ts +67 -6
  211. package/src/svgrid.wrapper.test.ts +27 -1
  212. package/src/test-setup.ts +9 -6
  213. package/src/virtualization/scroll-scaling.test.ts +148 -0
  214. package/src/virtualization/scroll-scaling.ts +121 -0
  215. package/src/virtualization/svelte-virtualizer.svelte.ts +2 -0
  216. package/src/virtualization/virtualizer.ts +26 -0
@@ -0,0 +1,156 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { normalizeEditorOptions, parseEditorValue } from './cell-editors'
3
+
4
+ describe('normalizeEditorOptions', () => {
5
+ it('returns [] for undefined', () => {
6
+ expect(normalizeEditorOptions(undefined)).toEqual([])
7
+ })
8
+
9
+ it('treats a function (mistakenly passed) as empty', () => {
10
+ // The type says functions are resolved earlier, but the guard handles it.
11
+ const fn = (() => []) as unknown as undefined
12
+ expect(normalizeEditorOptions(fn)).toEqual([])
13
+ })
14
+
15
+ it('maps bare strings to { value, label }', () => {
16
+ expect(normalizeEditorOptions(['a', 'b'])).toEqual([
17
+ { value: 'a', label: 'a' },
18
+ { value: 'b', label: 'b' },
19
+ ])
20
+ })
21
+
22
+ it('maps bare numbers to stringified labels', () => {
23
+ expect(normalizeEditorOptions([1, 2])).toEqual([
24
+ { value: 1, label: '1' },
25
+ { value: 2, label: '2' },
26
+ ])
27
+ })
28
+
29
+ it('keeps an explicit label and color', () => {
30
+ expect(
31
+ normalizeEditorOptions([{ value: 'x', label: 'Ex', color: '#f00' }]),
32
+ ).toEqual([{ value: 'x', label: 'Ex', color: '#f00' }])
33
+ })
34
+
35
+ it('defaults a missing label to String(value)', () => {
36
+ expect(normalizeEditorOptions([{ value: 7 }])).toEqual([
37
+ { value: 7, label: '7', color: undefined },
38
+ ])
39
+ })
40
+ })
41
+
42
+ describe('parseEditorValue - rating', () => {
43
+ it('rounds and clamps into 0..5', () => {
44
+ expect(parseEditorValue('rating', 3.4)).toBe(3)
45
+ expect(parseEditorValue('rating', 4.6)).toBe(5)
46
+ expect(parseEditorValue('rating', 9)).toBe(5)
47
+ expect(parseEditorValue('rating', -2)).toBe(0)
48
+ })
49
+ it('returns 0 for non-finite input', () => {
50
+ expect(parseEditorValue('rating', 'nope')).toBe(0)
51
+ expect(parseEditorValue('rating', Infinity)).toBe(0)
52
+ })
53
+ })
54
+
55
+ describe('parseEditorValue - color', () => {
56
+ it('returns the trimmed color string', () => {
57
+ expect(parseEditorValue('color', ' #abcdef ')).toBe('#abcdef')
58
+ })
59
+ it('passes non-hex color strings through', () => {
60
+ expect(parseEditorValue('color', 'rebeccapurple')).toBe('rebeccapurple')
61
+ })
62
+ it('falls back to #000000 for empty/nullish', () => {
63
+ expect(parseEditorValue('color', '')).toBe('#000000')
64
+ expect(parseEditorValue('color', null)).toBe('#000000')
65
+ expect(parseEditorValue('color', undefined)).toBe('#000000')
66
+ })
67
+ })
68
+
69
+ describe('parseEditorValue - time', () => {
70
+ it('returns null for empty input', () => {
71
+ expect(parseEditorValue('time', '')).toBe(null)
72
+ expect(parseEditorValue('time', null)).toBe(null)
73
+ })
74
+ it('returns null for malformed strings', () => {
75
+ expect(parseEditorValue('time', 'noon')).toBe(null)
76
+ expect(parseEditorValue('time', '12')).toBe(null)
77
+ })
78
+ it('normalizes HH:MM', () => {
79
+ expect(parseEditorValue('time', '9:05')).toBe('09:05')
80
+ })
81
+ it('keeps seconds when present', () => {
82
+ expect(parseEditorValue('time', '23:59:59')).toBe('23:59:59')
83
+ })
84
+ it('clamps out-of-range components', () => {
85
+ expect(parseEditorValue('time', '99:99:99')).toBe('23:59:59')
86
+ })
87
+ })
88
+
89
+ describe('parseEditorValue - password', () => {
90
+ it('keeps an empty string instead of nulling it', () => {
91
+ expect(parseEditorValue('password', '')).toBe('')
92
+ })
93
+ it('stringifies and preserves content', () => {
94
+ expect(parseEditorValue('password', 'hunter2')).toBe('hunter2')
95
+ expect(parseEditorValue('password', null)).toBe('')
96
+ })
97
+ })
98
+
99
+ describe('parseEditorValue - select / rich-select', () => {
100
+ it('returns scalar value as-is', () => {
101
+ expect(parseEditorValue('select', 'one')).toBe('one')
102
+ expect(parseEditorValue('rich-select', 5)).toBe(5)
103
+ })
104
+ it('takes the first element of an array', () => {
105
+ expect(parseEditorValue('select', ['a', 'b'])).toBe('a')
106
+ })
107
+ it('coerces nullish to null', () => {
108
+ expect(parseEditorValue('select', null)).toBe(null)
109
+ expect(parseEditorValue('select', undefined)).toBe(null)
110
+ expect(parseEditorValue('rich-select', [])).toBe(null)
111
+ })
112
+ })
113
+
114
+ describe('parseEditorValue - textarea', () => {
115
+ it('stringifies, nullish to empty', () => {
116
+ expect(parseEditorValue('textarea', 'multi\nline')).toBe('multi\nline')
117
+ expect(parseEditorValue('textarea', null)).toBe('')
118
+ })
119
+ })
120
+
121
+ describe('parseEditorValue - list / chips (single)', () => {
122
+ it('returns scalar as-is', () => {
123
+ expect(parseEditorValue('list', 'x')).toBe('x')
124
+ expect(parseEditorValue('chips', 7)).toBe(7)
125
+ })
126
+ it('takes first element of array', () => {
127
+ expect(parseEditorValue('list', ['a', 'b'])).toBe('a')
128
+ expect(parseEditorValue('chips', [])).toBe(null)
129
+ })
130
+ it('coerces nullish to null', () => {
131
+ expect(parseEditorValue('list', null)).toBe(null)
132
+ })
133
+ })
134
+
135
+ describe('parseEditorValue - list / chips (multiple)', () => {
136
+ it('clones an array', () => {
137
+ const input = ['a', 'b']
138
+ const out = parseEditorValue('list', input, { multiple: true }) as string[]
139
+ expect(out).toEqual(['a', 'b'])
140
+ expect(out).not.toBe(input)
141
+ })
142
+ it('returns [] for empty/nullish', () => {
143
+ expect(parseEditorValue('chips', null, { multiple: true })).toEqual([])
144
+ expect(parseEditorValue('chips', '', { multiple: true })).toEqual([])
145
+ })
146
+ it('wraps a scalar into a single-element array', () => {
147
+ expect(parseEditorValue('list', 'solo', { multiple: true })).toEqual(['solo'])
148
+ })
149
+ })
150
+
151
+ describe('parseEditorValue - unknown type fallback', () => {
152
+ it('stringifies, nullish to empty', () => {
153
+ expect(parseEditorValue('autocomplete', 'hey')).toBe('hey')
154
+ expect(parseEditorValue('autocomplete', undefined)).toBe('')
155
+ })
156
+ })
@@ -10,6 +10,7 @@
10
10
  | 'chips'
11
11
  | 'select'
12
12
  | 'rich-select'
13
+ | 'autocomplete'
13
14
  | 'textarea'
14
15
  | 'color'
15
16
  | 'rating'
@@ -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
+ }