@svgrid/enterprise 1.2.0 → 2.0.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.
Files changed (159) hide show
  1. package/dist/cdn/svgrid-enterprise.svelte-external.js +26039 -805
  2. package/dist/designer/assets/index-Dp44bTid.js +939 -0
  3. package/dist/designer/assets/index-RJp6x8tw.css +1 -0
  4. package/dist/designer/assets/jszip.min-CjMo-QGg.js +2 -0
  5. package/dist/designer/index.html +13 -0
  6. package/dist/node/studio.js +22554 -0
  7. package/package.json +10 -3
  8. package/src/SvAuthGate.svelte +115 -0
  9. package/src/SvBoard.dom.test.ts +67 -0
  10. package/src/SvBoard.svelte +192 -0
  11. package/src/SvExportMenu.svelte +553 -0
  12. package/src/SvFileInput.svelte +113 -0
  13. package/src/SvGridEditPanel.dom.test.ts +146 -0
  14. package/src/SvGridEditPanel.svelte +793 -0
  15. package/src/SvGridMasterDetail.svelte +95 -0
  16. package/src/SvImportDialog.svelte +1020 -0
  17. package/src/SvLookupInput.svelte +180 -0
  18. package/src/SvRecordDetail.dom.test.ts +137 -0
  19. package/src/SvRecordDetail.svelte +288 -0
  20. package/src/SvSchedule.dom.test.ts +57 -0
  21. package/src/SvSchedule.svelte +156 -0
  22. package/src/SvSchemaChart.svelte +233 -0
  23. package/src/SvSchemaDashboard.svelte +130 -0
  24. package/src/ai-export-pdf.test.ts +74 -0
  25. package/src/ai-export-xlsx.test.ts +87 -0
  26. package/src/ai-export.test.ts +110 -0
  27. package/src/ai.ts +1188 -782
  28. package/src/edit-panel.test.ts +213 -0
  29. package/src/edit-panel.ts +228 -0
  30. package/src/export-conditional.test.ts +60 -0
  31. package/src/export-conditional.ts +94 -0
  32. package/src/export-ooxml.test.ts +152 -0
  33. package/src/export-ooxml.ts +485 -0
  34. package/src/export-pdf.test.ts +138 -0
  35. package/src/export-pdf.ts +245 -0
  36. package/src/export-print.test.ts +66 -0
  37. package/src/export-print.ts +132 -0
  38. package/src/export-serialize.test.ts +56 -0
  39. package/src/export-serialize.ts +24 -0
  40. package/src/export-xls.ts +196 -0
  41. package/src/export-xlsx-roundtrip.test.ts +120 -0
  42. package/src/export-xlsx.test.ts +150 -0
  43. package/src/export.test.ts +349 -0
  44. package/src/export.ts +1452 -549
  45. package/src/import-automap.test.ts +131 -0
  46. package/src/import-hardening.test.ts +158 -0
  47. package/src/import.ts +1042 -648
  48. package/src/index.ts +282 -0
  49. package/src/install.ts +134 -114
  50. package/src/license-core.test.ts +23 -0
  51. package/src/license-core.ts +35 -0
  52. package/src/license.ts +103 -90
  53. package/src/master-detail.test.ts +61 -0
  54. package/src/master-detail.ts +42 -0
  55. package/src/print.ts +107 -127
  56. package/src/schema-designer.test.ts +121 -0
  57. package/src/schema-designer.ts +142 -0
  58. package/src/schema.test.ts +268 -0
  59. package/src/schema.ts +499 -0
  60. package/src/smart-shim.ts +107 -105
  61. package/src/sources/aggregate.test.ts +79 -0
  62. package/src/sources/aggregate.ts +102 -0
  63. package/src/sources/auth-supabase.test.ts +80 -0
  64. package/src/sources/auth-supabase.ts +87 -0
  65. package/src/sources/dashboard.kpi.test.ts +50 -0
  66. package/src/sources/dashboard.test.ts +61 -0
  67. package/src/sources/dashboard.ts +136 -0
  68. package/src/sources/field-inference.test.ts +60 -0
  69. package/src/sources/field-inference.ts +63 -0
  70. package/src/sources/filters.test.ts +58 -0
  71. package/src/sources/filters.ts +64 -0
  72. package/src/sources/index.ts +67 -0
  73. package/src/sources/introspect-supabase.test.ts +100 -0
  74. package/src/sources/introspect-supabase.ts +119 -0
  75. package/src/sources/realtime-supabase.test.ts +93 -0
  76. package/src/sources/realtime-supabase.ts +99 -0
  77. package/src/sources/relation-lookup.test.ts +99 -0
  78. package/src/sources/relation-lookup.ts +128 -0
  79. package/src/sources/rest-adapters.test.ts +95 -0
  80. package/src/sources/rest-adapters.ts +120 -0
  81. package/src/sources/rest.test.ts +104 -0
  82. package/src/sources/rest.ts +129 -0
  83. package/src/sources/schema-from-columns.test.ts +106 -0
  84. package/src/sources/schema-from-columns.ts +88 -0
  85. package/src/sources/supabase.test.ts +110 -0
  86. package/src/sources/supabase.ts +99 -0
  87. package/src/sources/with-entity-rules.test.ts +104 -0
  88. package/src/sources/with-entity-rules.ts +78 -0
  89. package/src/sources/with-relation-labels.test.ts +75 -0
  90. package/src/sources/with-relation-labels.ts +73 -0
  91. package/src/studio/bug-report.test.ts +101 -0
  92. package/src/studio/bug-report.ts +165 -0
  93. package/src/studio/cli.test.ts +187 -0
  94. package/src/studio/cli.ts +112 -0
  95. package/src/studio/csv.test.ts +90 -0
  96. package/src/studio/csv.ts +165 -0
  97. package/src/studio/db-connect-string.test.ts +94 -0
  98. package/src/studio/db-connect-string.ts +128 -0
  99. package/src/studio/emit-project.test.ts +923 -0
  100. package/src/studio/emit-project.ts +1273 -0
  101. package/src/studio/emit-schema.test.ts +94 -0
  102. package/src/studio/emit-schema.ts +920 -0
  103. package/src/studio/index.ts +195 -0
  104. package/src/studio/introspect-db.test.ts +186 -0
  105. package/src/studio/introspect-db.ts +312 -0
  106. package/src/studio/introspect-prisma.test.ts +99 -0
  107. package/src/studio/introspect-prisma.ts +175 -0
  108. package/src/studio/introspect.test.ts +172 -0
  109. package/src/studio/introspect.ts +310 -0
  110. package/src/studio/pipeline.test.ts +42 -0
  111. package/src/studio/project-robust.test.ts +157 -0
  112. package/src/studio/project.test.ts +473 -0
  113. package/src/studio/project.ts +916 -0
  114. package/src/studio/sample-data.test.ts +58 -0
  115. package/src/studio/sample-data.ts +200 -0
  116. package/src/studio/samples/ats.ts +202 -0
  117. package/src/studio/samples/clinic.ts +177 -0
  118. package/src/studio/samples/crm.ts +250 -0
  119. package/src/studio/samples/ecommerce.ts +187 -0
  120. package/src/studio/samples/events.ts +199 -0
  121. package/src/studio/samples/fleet.ts +184 -0
  122. package/src/studio/samples/gym.ts +213 -0
  123. package/src/studio/samples/hr.ts +193 -0
  124. package/src/studio/samples/index.ts +55 -0
  125. package/src/studio/samples/insurance.ts +195 -0
  126. package/src/studio/samples/inventory.ts +182 -0
  127. package/src/studio/samples/invoicing.ts +162 -0
  128. package/src/studio/samples/library.ts +180 -0
  129. package/src/studio/samples/live-data.test.ts +98 -0
  130. package/src/studio/samples/live-data.ts +308 -0
  131. package/src/studio/samples/projects.ts +190 -0
  132. package/src/studio/samples/realestate.ts +196 -0
  133. package/src/studio/samples/restaurant.ts +187 -0
  134. package/src/studio/samples/samples.test.ts +208 -0
  135. package/src/studio/samples/school.ts +197 -0
  136. package/src/studio/samples/seed-floor.test.ts +25 -0
  137. package/src/studio/samples/shared.ts +305 -0
  138. package/src/studio/samples/subscriptions.ts +183 -0
  139. package/src/studio/samples/support.ts +182 -0
  140. package/src/studio/scaffold-app.test.ts +91 -0
  141. package/src/studio/scaffold-app.ts +161 -0
  142. package/src/studio/scaffold.test.ts +178 -0
  143. package/src/studio/scaffold.ts +374 -0
  144. package/src/studio/themes.ts +172 -0
  145. package/src/studio/verify.test.ts +47 -0
  146. package/src/studio/verify.ts +79 -0
  147. package/src/sveltekit/in-memory.test.ts +104 -0
  148. package/src/sveltekit/in-memory.ts +129 -0
  149. package/src/sveltekit/index.ts +23 -0
  150. package/src/sveltekit/query-plan.test.ts +109 -0
  151. package/src/sveltekit/query-plan.ts +125 -0
  152. package/src/sveltekit/sql-source.test.ts +185 -0
  153. package/src/sveltekit/sql-source.ts +166 -0
  154. package/src/sveltekit/sql.test.ts +80 -0
  155. package/src/sveltekit/sql.ts +131 -0
  156. package/src/sveltekit/transport.test.ts +175 -0
  157. package/src/sveltekit/transport.ts +254 -0
  158. package/src/sveltekit/types.ts +11 -0
  159. package/src/upgrade-prompt.ts +149 -148
package/src/import.ts CHANGED
@@ -1,648 +1,1042 @@
1
- /**
2
- * Pro Import feature
3
- * ------------------
4
- *
5
- * Read Excel (.xlsx), CSV, TSV, or JSON in the browser and produce a typed
6
- * preview of the parsed rows, including per-cell validation errors. The
7
- * caller decides whether to commit the parsed rows into the grid via
8
- * `api.addRows(...)`, or to render an editing modal so the user can fix
9
- * issues first.
10
- *
11
- * Two flavours of usage:
12
- *
13
- * 1. Read + preview, then commit explicitly:
14
- *
15
- * const result = await api.importData({ file, format: 'auto' })
16
- * // result.headers, result.rows, result.errors
17
- * if (result.errors.length === 0) api.addRows(result.rows, 'bottom')
18
- *
19
- * 2. Read and commit in one go (no preview UX):
20
- *
21
- * await api.importData({ file, format: 'auto', commit: true })
22
- *
23
- * Both go through the same polite license soft-gate as `exportData` and
24
- * `print`, so unlicensed evaluation works but emits a one-time nudge.
25
- */
26
-
27
- import type { RowData, SvGridApi, TableFeatures } from '@svgrid/grid'
28
- import { assertEnterpriseLicensed } from './license'
29
-
30
- // ---------------------------------------------------------------------------
31
- // Public types
32
- // ---------------------------------------------------------------------------
33
-
34
- export type ImportFormat = 'xlsx' | 'csv' | 'tsv' | 'json' | 'auto'
35
-
36
- export type ImportColumnMap = Record<string, string>
37
-
38
- /**
39
- * Declared data type per target field. When `columnTypes` is set on
40
- * `ImportOptions`, the parser tries to coerce each non-empty cell into
41
- * the declared shape and emits an `ImportRowError` when it can't -
42
- * stricter than the ad-hoc "looks like a number" fallback.
43
- */
44
- export type ImportFieldType =
45
- | 'string'
46
- | 'number'
47
- | 'integer'
48
- | 'boolean'
49
- | 'date' // returns ISO yyyy-mm-dd
50
- | 'datetime' // returns ISO yyyy-mm-ddThh:mm:ss
51
- | 'json' // parses the cell as JSON (object / array / primitive)
52
-
53
- export type ImportColumnTypes = Record<string, ImportFieldType>
54
-
55
- export type ImportRowError = {
56
- /** 0-based row index in the SOURCE file (excluding the header). */
57
- rowIndex: number
58
- /** Target field name (the grid's column field), or '*' for whole-row errors. */
59
- field: string
60
- message: string
61
- }
62
-
63
- export type ImportValidator<TData> = (
64
- row: TData,
65
- rowIndex: number,
66
- ) => Array<{ field: string; message: string }>
67
-
68
- export type ImportOptions<TData> = {
69
- /** The file to read. A `File`/`Blob` works for xlsx; a string is treated as
70
- * inline CSV/TSV/JSON text. */
71
- file: File | Blob | string
72
- /** When 'auto', the format is sniffed from `file.name`'s extension (Files
73
- * only), otherwise the function inspects the first characters of the
74
- * text payload. */
75
- format?: ImportFormat
76
- /**
77
- * Map source-header -> target-field. Missing entries fall back to the
78
- * source header verbatim (lowercased + trimmed). Pass `null` for a
79
- * source header you want to drop on the floor.
80
- */
81
- columnMap?: ImportColumnMap
82
- /**
83
- * Declared data types per target field. When set, the importer uses
84
- * strict per-type coercion (`'2024-03-15'` -> ISO date; `'$1,234'`
85
- * -> 1234 for `number` fields) and emits an `ImportRowError` whenever
86
- * a value can't be coerced. Fields not listed fall back to the
87
- * built-in best-effort coercion (currency / number / date sniffing
88
- * by value shape).
89
- */
90
- columnTypes?: ImportColumnTypes
91
- /** Per-row validator. Returned errors land in `result.errors`. */
92
- validator?: ImportValidator<TData>
93
- /** When true, the parsed rows are appended to the grid via
94
- * `api.addRows(...)` automatically. Defaults to false (preview mode). */
95
- commit?: boolean
96
- /** Where to insert when `commit` is true. Defaults to 'bottom'. */
97
- commitAt?: 'top' | 'bottom' | number
98
- }
99
-
100
- export type ImportResult<TData> = {
101
- /** Source headers as found in the file, in their original order. */
102
- headers: string[]
103
- /** Parsed + mapped rows. Order matches the source. */
104
- rows: TData[]
105
- /** All validation errors (zero-length if the file passed clean). */
106
- errors: ImportRowError[]
107
- /** Rows skipped because they were entirely blank. */
108
- skipped: number
109
- /** Total source rows the parser saw (including blanks and bad rows). */
110
- total: number
111
- /** Detected format (resolved from 'auto'). */
112
- format: Exclude<ImportFormat, 'auto'>
113
- }
114
-
115
- // ---------------------------------------------------------------------------
116
- // Lazy peer dependency: jszip (xlsx only)
117
- // ---------------------------------------------------------------------------
118
-
119
- let jszipPromise: Promise<unknown> | null = null
120
- async function getJSZip(): Promise<any> {
121
- if (typeof window === 'undefined') {
122
- throw new Error('@svgrid/enterprise: importData requires a browser environment')
123
- }
124
- const g = globalThis as unknown as { JSZip?: unknown }
125
- if (g.JSZip) return g.JSZip
126
- if (!jszipPromise) {
127
- jszipPromise = (async () => {
128
- let mod: unknown
129
- try {
130
- mod = await import('jszip')
131
- } catch {
132
- throw new Error(
133
- '@svgrid/enterprise: xlsx import requires the "jszip" peer dependency. ' +
134
- 'Install it with: pnpm add jszip',
135
- )
136
- }
137
- const J = (mod as { default?: unknown }).default ?? mod
138
- g.JSZip = J
139
- return J
140
- })()
141
- }
142
- return jszipPromise
143
- }
144
-
145
- // ---------------------------------------------------------------------------
146
- // Format sniffing
147
- // ---------------------------------------------------------------------------
148
-
149
- function sniffFormat(
150
- file: File | Blob | string,
151
- declared: ImportFormat,
152
- ): Exclude<ImportFormat, 'auto'> {
153
- if (declared !== 'auto') return declared
154
- // File: trust the extension.
155
- if (file instanceof File && file.name) {
156
- const ext = file.name.toLowerCase().split('.').pop() ?? ''
157
- if (ext === 'xlsx') return 'xlsx'
158
- if (ext === 'csv') return 'csv'
159
- if (ext === 'tsv' || ext === 'tab') return 'tsv'
160
- if (ext === 'json') return 'json'
161
- }
162
- // String: sniff the first non-whitespace char.
163
- if (typeof file === 'string') {
164
- const trimmed = file.trimStart()
165
- if (trimmed.startsWith('[') || trimmed.startsWith('{')) return 'json'
166
- // Tab-rich heads are tsv; otherwise default to csv.
167
- const firstLine = trimmed.slice(0, 200).split(/\r?\n/)[0] ?? ''
168
- if ((firstLine.match(/\t/g)?.length ?? 0) >= 1) return 'tsv'
169
- return 'csv'
170
- }
171
- // Blob without a filename / extension: assume xlsx (the most common case
172
- // the importer is asked to handle).
173
- return 'xlsx'
174
- }
175
-
176
- // ---------------------------------------------------------------------------
177
- // CSV / TSV parser
178
- // ---------------------------------------------------------------------------
179
-
180
- /**
181
- * RFC 4180-flavour CSV / TSV parser. Handles:
182
- * - quoted fields with embedded commas / tabs / newlines
183
- * - escaped quotes ("") inside quoted fields
184
- * - both \r\n and \n line endings
185
- *
186
- * Doesn't aim to be the world's fastest parser - it walks the string
187
- * character-by-character. For files in the 10-100k row range this is
188
- * fine; for million-row imports, do the import server-side.
189
- */
190
- function parseDelimited(text: string, sep: ',' | '\t'): string[][] {
191
- const rows: string[][] = []
192
- let row: string[] = []
193
- let field = ''
194
- let i = 0
195
- let inQuotes = false
196
- // Strip a UTF-8 BOM if present so the first header isn't named "ID".
197
- if (text.charCodeAt(0) === 0xfeff) i = 1
198
- while (i < text.length) {
199
- const c = text[i]!
200
- if (inQuotes) {
201
- if (c === '"') {
202
- if (text[i + 1] === '"') { field += '"'; i += 2; continue }
203
- inQuotes = false
204
- i += 1
205
- continue
206
- }
207
- field += c
208
- i += 1
209
- continue
210
- }
211
- if (c === '"') { inQuotes = true; i += 1; continue }
212
- if (c === sep) { row.push(field); field = ''; i += 1; continue }
213
- if (c === '\r') {
214
- if (text[i + 1] === '\n') i += 1
215
- row.push(field); rows.push(row); row = []; field = ''; i += 1; continue
216
- }
217
- if (c === '\n') {
218
- row.push(field); rows.push(row); row = []; field = ''; i += 1; continue
219
- }
220
- field += c
221
- i += 1
222
- }
223
- // Flush the trailing field / row (no trailing newline).
224
- if (field.length > 0 || row.length > 0) {
225
- row.push(field)
226
- rows.push(row)
227
- }
228
- return rows
229
- }
230
-
231
- // ---------------------------------------------------------------------------
232
- // XLSX parser (minimal)
233
- // ---------------------------------------------------------------------------
234
-
235
- /**
236
- * Minimal SpreadsheetML reader. Supports the .xlsx shape Excel and Google
237
- * Sheets produce by default: one default sheet, optional shared-strings
238
- * table, inline strings, numbers, and dates expressed as serial numbers.
239
- *
240
- * The grid does NOT try to be a fully-featured Excel reader. Formulas
241
- * are read as their cached value when present. Multi-sheet imports
242
- * require the caller to pre-extract the desired sheet (or for a future
243
- * release).
244
- */
245
- async function parseXlsx(file: File | Blob): Promise<string[][]> {
246
- const J = await getJSZip()
247
- const buf = await (file as Blob).arrayBuffer()
248
- const zip = await J.loadAsync(buf)
249
-
250
- // Read sharedStrings.xml if it exists.
251
- let sharedStrings: string[] = []
252
- const ssEntry = zip.file('xl/sharedStrings.xml')
253
- if (ssEntry) {
254
- const ssText = await ssEntry.async('string')
255
- sharedStrings = parseSharedStrings(ssText)
256
- }
257
-
258
- // Read the FIRST sheet. Workbook.xml -> rels mapping is involved; for the
259
- // 99% case the first sheet lives at xl/worksheets/sheet1.xml.
260
- const sheetEntry =
261
- zip.file('xl/worksheets/sheet1.xml') ??
262
- zip.file('xl/worksheets/sheet 1.xml')
263
- if (!sheetEntry) {
264
- throw new Error(
265
- '@svgrid/enterprise: could not locate sheet1.xml in the .xlsx archive',
266
- )
267
- }
268
- const sheetText = await sheetEntry.async('string')
269
- return parseSheetXml(sheetText, sharedStrings)
270
- }
271
-
272
- /** Pull every `<t>...</t>` out of sharedStrings.xml in document order. */
273
- function parseSharedStrings(xml: string): string[] {
274
- const out: string[] = []
275
- // A shared-string item may be a plain <si><t>X</t></si> or a rich-text
276
- // <si><r><t>X</t></r><r><t>Y</t></r></si>. Concatenate every <t> inside
277
- // each <si>.
278
- const itemRe = /<si\b[^>]*>([\s\S]*?)<\/si>/g
279
- const tRe = /<t\b[^>]*>([\s\S]*?)<\/t>/g
280
- let m: RegExpExecArray | null
281
- while ((m = itemRe.exec(xml)) !== null) {
282
- let combined = ''
283
- let n: RegExpExecArray | null
284
- tRe.lastIndex = 0
285
- while ((n = tRe.exec(m[1] ?? '')) !== null) {
286
- combined += decodeXmlEntities(n[1] ?? '')
287
- }
288
- out.push(combined)
289
- }
290
- return out
291
- }
292
-
293
- function decodeXmlEntities(s: string): string {
294
- return s
295
- .replace(/&amp;/g, '&')
296
- .replace(/&lt;/g, '<')
297
- .replace(/&gt;/g, '>')
298
- .replace(/&quot;/g, '"')
299
- .replace(/&apos;/g, "'")
300
- .replace(/&#10;/g, '\n')
301
- .replace(/&#9;/g, '\t')
302
- }
303
-
304
- /**
305
- * Convert a sheet's XML body into a dense `string[][]`. Holes in the
306
- * cell stream (e.g. row jumps from col B to col E) are filled with
307
- * empty strings so the result is rectangular.
308
- */
309
- function parseSheetXml(xml: string, sharedStrings: string[]): string[][] {
310
- const rows: string[][] = []
311
- const rowRe = /<row\b[^>]*>([\s\S]*?)<\/row>/g
312
- const cellRe = /<c\b([^>]*)>([\s\S]*?)<\/c>/g
313
- const inlineStrRe = /<is>\s*([\s\S]*?)\s*<\/is>/
314
- const tRe = /<t\b[^>]*>([\s\S]*?)<\/t>/g
315
- const vRe = /<v>([\s\S]*?)<\/v>/
316
- let rowMatch: RegExpExecArray | null
317
- while ((rowMatch = rowRe.exec(xml)) !== null) {
318
- const rowBody = rowMatch[1] ?? ''
319
- const cells: string[] = []
320
- let cellMatch: RegExpExecArray | null
321
- cellRe.lastIndex = 0
322
- while ((cellMatch = cellRe.exec(rowBody)) !== null) {
323
- const attrs = cellMatch[1] ?? ''
324
- const body = cellMatch[2] ?? ''
325
- const ref = /r="([A-Z]+)\d+"/.exec(attrs)?.[1] ?? ''
326
- const colIndex = colRefToIndex(ref)
327
- const type = /t="([^"]+)"/.exec(attrs)?.[1] ?? 'n'
328
- // Pad holes with empty strings so column alignment survives.
329
- while (cells.length < colIndex) cells.push('')
330
- let value = ''
331
- if (type === 's') {
332
- const idx = Number(vRe.exec(body)?.[1] ?? '-1')
333
- value = sharedStrings[idx] ?? ''
334
- } else if (type === 'inlineStr' || type === 'str') {
335
- const inlineBody = inlineStrRe.exec(body)?.[1] ?? body
336
- let parts = ''
337
- let n: RegExpExecArray | null
338
- tRe.lastIndex = 0
339
- while ((n = tRe.exec(inlineBody)) !== null) parts += decodeXmlEntities(n[1] ?? '')
340
- value = parts || decodeXmlEntities(vRe.exec(body)?.[1] ?? '')
341
- } else if (type === 'b') {
342
- value = vRe.exec(body)?.[1] === '1' ? 'true' : 'false'
343
- } else {
344
- // Numeric or date - we surface as the raw number string. The
345
- // caller's column type then handles parsing.
346
- value = decodeXmlEntities(vRe.exec(body)?.[1] ?? '')
347
- }
348
- cells.push(value)
349
- }
350
- rows.push(cells)
351
- }
352
- return rows
353
- }
354
-
355
- /** Excel column reference ("A", "B", ..., "AA", ...) to a zero-based index. */
356
- function colRefToIndex(ref: string): number {
357
- if (!ref) return 0
358
- let n = 0
359
- for (let i = 0; i < ref.length; i += 1) {
360
- n = n * 26 + (ref.charCodeAt(i) - 64)
361
- }
362
- return Math.max(0, n - 1)
363
- }
364
-
365
- // ---------------------------------------------------------------------------
366
- // Coerce raw string values into typed JS values based on a sample of other
367
- // values in the same column. We don't ask the consumer for a schema - it
368
- // would defeat the "drop a file in and go" UX - but we DO want
369
- // "$1,234.56" to become 1234.56 and "true" to become true.
370
- // ---------------------------------------------------------------------------
371
-
372
- function inferAndCoerce(value: string): unknown {
373
- if (value === '') return ''
374
- const trimmed = value.trim()
375
- if (trimmed === '') return ''
376
- // Boolean
377
- if (trimmed === 'true' || trimmed === 'TRUE') return true
378
- if (trimmed === 'false' || trimmed === 'FALSE') return false
379
- // Currency / number with grouping. Strip $, commas, whitespace.
380
- if (/^[-+]?\$?\s?[\d,]+(?:\.\d+)?$/.test(trimmed)) {
381
- const n = parseFloat(trimmed.replace(/[$,\s]/g, ''))
382
- if (!Number.isNaN(n)) return n
383
- }
384
- // Bare integer / float
385
- if (/^[-+]?\d+(?:\.\d+)?(?:e[+-]?\d+)?$/i.test(trimmed)) {
386
- const n = parseFloat(trimmed)
387
- if (!Number.isNaN(n)) return n
388
- }
389
- // ISO date (yyyy-mm-dd, optionally with time) -> keep as ISO string. We
390
- // don't return a Date because most grid columns prefer strings on the
391
- // wire (sort + filter + format paths are already date-aware).
392
- if (/^\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2})?(?:Z|[+-]\d{2}:?\d{2})?)?$/.test(trimmed)) {
393
- return trimmed
394
- }
395
- return trimmed
396
- }
397
-
398
- // ---------------------------------------------------------------------------
399
- // Map source rows -> typed records.
400
- // ---------------------------------------------------------------------------
401
-
402
- function buildRecords<TData>(
403
- matrix: string[][],
404
- columnMap: ImportColumnMap | undefined,
405
- columnTypes: ImportColumnTypes | undefined,
406
- ): { headers: string[]; rows: TData[]; skipped: number; errors: ImportRowError[] } {
407
- if (matrix.length === 0) return { headers: [], rows: [], skipped: 0, errors: [] }
408
- const headers = (matrix[0] ?? []).map((h) => h.trim())
409
- const dataRows = matrix.slice(1)
410
- const out: TData[] = []
411
- const errors: ImportRowError[] = []
412
- let skipped = 0
413
-
414
- // Pre-compute target-field per source header, honouring the columnMap.
415
- const fields = headers.map((h) => {
416
- if (columnMap && Object.prototype.hasOwnProperty.call(columnMap, h)) {
417
- return columnMap[h] ?? null
418
- }
419
- // Default: lowercase + collapse whitespace to camelCase-ish.
420
- const k = h.toLowerCase().replace(/\s+/g, '_').replace(/[^a-z0-9_]/g, '')
421
- return k
422
- })
423
-
424
- // Skipped (blank) rows don't count toward the output row index used
425
- // in errors, so the indices match what the consumer's preview UI
426
- // shows.
427
- let outRowIndex = 0
428
- for (const cells of dataRows) {
429
- const allBlank = cells.every((c) => c === undefined || c.trim() === '')
430
- if (allBlank) { skipped += 1; continue }
431
- const rec: Record<string, unknown> = {}
432
- for (let i = 0; i < headers.length; i += 1) {
433
- const field = fields[i]
434
- if (field == null) continue
435
- const raw = cells[i] ?? ''
436
- const declaredType = columnTypes?.[field]
437
- if (declaredType) {
438
- const result = coerceTyped(raw, declaredType)
439
- if (result.ok) {
440
- rec[field] = result.value
441
- } else {
442
- errors.push({ rowIndex: outRowIndex, field, message: result.message })
443
- // Park the raw string so downstream code still sees SOMETHING.
444
- rec[field] = raw
445
- }
446
- } else {
447
- rec[field] = inferAndCoerce(raw)
448
- }
449
- }
450
- out.push(rec as unknown as TData)
451
- outRowIndex += 1
452
- }
453
- return { headers, rows: out, skipped, errors }
454
- }
455
-
456
- /**
457
- * Strict per-type coercion used when the consumer declares
458
- * `columnTypes`. Returns either `{ ok: true, value }` or
459
- * `{ ok: false, message }` describing why the value couldn't be
460
- * shaped into the declared type.
461
- *
462
- * Empty values for required types (number, date) error rather than
463
- * silently coerce to 0 / null - the consumer can drop the row in
464
- * their validator if blanks should be tolerated.
465
- */
466
- function coerceTyped(raw: string, type: ImportFieldType):
467
- | { ok: true; value: unknown }
468
- | { ok: false; message: string }
469
- {
470
- const trimmed = raw.trim()
471
- // Empty cells map to `null` for most types and the empty string for
472
- // `string` - this is the only case where typed import returns a
473
- // null. Validator step is the right place to insist on "required".
474
- if (trimmed === '') {
475
- if (type === 'string') return { ok: true, value: '' }
476
- return { ok: true, value: null }
477
- }
478
-
479
- switch (type) {
480
- case 'string':
481
- return { ok: true, value: trimmed }
482
-
483
- case 'boolean': {
484
- const low = trimmed.toLowerCase()
485
- if (low === 'true' || low === '1' || low === 'yes' || low === 'y') return { ok: true, value: true }
486
- if (low === 'false' || low === '0' || low === 'no' || low === 'n') return { ok: true, value: false }
487
- return { ok: false, message: `not a boolean: ${trimmed}` }
488
- }
489
-
490
- case 'number': {
491
- const stripped = trimmed.replace(/[$,\s]/g, '')
492
- const n = Number(stripped)
493
- if (!Number.isFinite(n)) return { ok: false, message: `not a number: ${trimmed}` }
494
- return { ok: true, value: n }
495
- }
496
-
497
- case 'integer': {
498
- const stripped = trimmed.replace(/[$,\s]/g, '')
499
- const n = Number(stripped)
500
- if (!Number.isFinite(n) || !Number.isInteger(n)) {
501
- return { ok: false, message: `not an integer: ${trimmed}` }
502
- }
503
- return { ok: true, value: n }
504
- }
505
-
506
- case 'date': {
507
- // Accept ISO dates and a handful of common day-first / month-first
508
- // shapes. Returns ISO yyyy-mm-dd.
509
- const iso = trimmed.match(/^(\d{4})-(\d{2})-(\d{2})/)
510
- if (iso) return { ok: true, value: `${iso[1]}-${iso[2]}-${iso[3]}` }
511
- // mm/dd/yyyy or dd/mm/yyyy - we don't try to disambiguate; the
512
- // demo can declare the locale via its own validator if it cares.
513
- const slash = trimmed.match(/^(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/)
514
- if (slash) {
515
- const [, a, b, y] = slash
516
- // Default to mm/dd/yyyy because it's the dominant locale where
517
- // Excel exports without an explicit ISO format. Consumers
518
- // wanting EU-style can pre-normalise the file.
519
- return { ok: true, value: `${y}-${a!.padStart(2, '0')}-${b!.padStart(2, '0')}` }
520
- }
521
- const parsed = Date.parse(trimmed)
522
- if (!Number.isNaN(parsed)) {
523
- return { ok: true, value: new Date(parsed).toISOString().slice(0, 10) }
524
- }
525
- return { ok: false, message: `not a date: ${trimmed}` }
526
- }
527
-
528
- case 'datetime': {
529
- // Accept anything Date.parse handles.
530
- const parsed = Date.parse(trimmed)
531
- if (Number.isNaN(parsed)) return { ok: false, message: `not a datetime: ${trimmed}` }
532
- return { ok: true, value: new Date(parsed).toISOString().slice(0, 19) }
533
- }
534
-
535
- case 'json': {
536
- try {
537
- return { ok: true, value: JSON.parse(trimmed) }
538
- } catch {
539
- return { ok: false, message: `not valid JSON: ${trimmed.slice(0, 30)}...` }
540
- }
541
- }
542
- }
543
- }
544
-
545
- // ---------------------------------------------------------------------------
546
- // Public entry point
547
- // ---------------------------------------------------------------------------
548
-
549
- /**
550
- * Read a file (or inline text), parse, optionally validate + commit. The
551
- * sister function of `exportGrid`.
552
- */
553
- export async function importData<
554
- TFeatures extends TableFeatures,
555
- TData extends RowData,
556
- >(
557
- api: SvGridApi<TFeatures, TData>,
558
- opts: ImportOptions<TData>,
559
- ): Promise<ImportResult<TData>> {
560
- assertEnterpriseLicensed('Import')
561
- const format = sniffFormat(opts.file, opts.format ?? 'auto')
562
-
563
- let matrix: string[][]
564
- if (format === 'xlsx') {
565
- if (typeof opts.file === 'string') {
566
- throw new Error(
567
- '@svgrid/enterprise: xlsx import expects a File or Blob, not a string. ' +
568
- 'Use format: "csv" or "tsv" for inline text.',
569
- )
570
- }
571
- matrix = await parseXlsx(opts.file)
572
- } else if (format === 'json') {
573
- const text = typeof opts.file === 'string' ? opts.file : await readText(opts.file)
574
- matrix = jsonToMatrix(text)
575
- } else {
576
- const sep = format === 'tsv' ? '\t' : ','
577
- const text = typeof opts.file === 'string' ? opts.file : await readText(opts.file)
578
- matrix = parseDelimited(text, sep)
579
- }
580
-
581
- const { headers, rows, skipped, errors: typeErrors } =
582
- buildRecords<TData>(matrix, opts.columnMap, opts.columnTypes)
583
-
584
- // Type-coercion errors come first; the validator runs on the rows
585
- // we managed to build, so its errors have the same `rowIndex` basis.
586
- const errors: ImportRowError[] = [...typeErrors]
587
- if (opts.validator) {
588
- for (let i = 0; i < rows.length; i += 1) {
589
- const errs = opts.validator(rows[i]!, i)
590
- for (const e of errs) errors.push({ rowIndex: i, field: e.field, message: e.message })
591
- }
592
- }
593
-
594
- if (opts.commit && errors.length === 0 && rows.length > 0) {
595
- api.addRows(rows, opts.commitAt ?? 'bottom')
596
- }
597
-
598
- return {
599
- headers,
600
- rows,
601
- errors,
602
- skipped,
603
- total: matrix.length > 0 ? matrix.length - 1 : 0,
604
- format,
605
- }
606
- }
607
-
608
- // ---------------------------------------------------------------------------
609
- // Helpers
610
- // ---------------------------------------------------------------------------
611
-
612
- async function readText(blob: Blob): Promise<string> {
613
- return blob.text()
614
- }
615
-
616
- /** Convert JSON text containing an array of objects into a `string[][]`
617
- * matrix shaped like CSV: header row, then one row per record. */
618
- function jsonToMatrix(text: string): string[][] {
619
- const parsed = JSON.parse(text)
620
- if (!Array.isArray(parsed)) {
621
- throw new Error('@svgrid/enterprise: JSON import expects a top-level array')
622
- }
623
- if (parsed.length === 0) return []
624
- // Collect the union of keys across the first ~50 records so we don't
625
- // miss a column present only on later rows.
626
- const headers: string[] = []
627
- const seen = new Set<string>()
628
- const sample = parsed.slice(0, 50) as Record<string, unknown>[]
629
- for (const r of sample) {
630
- if (r && typeof r === 'object') {
631
- for (const k of Object.keys(r)) {
632
- if (!seen.has(k)) { seen.add(k); headers.push(k) }
633
- }
634
- }
635
- }
636
- const rows: string[][] = [headers.slice()]
637
- for (const r of parsed as Array<Record<string, unknown>>) {
638
- const row: string[] = []
639
- for (const h of headers) {
640
- const v = r?.[h]
641
- if (v == null) row.push('')
642
- else if (typeof v === 'object') row.push(JSON.stringify(v))
643
- else row.push(String(v))
644
- }
645
- rows.push(row)
646
- }
647
- return rows
648
- }
1
+ /**
2
+ * Pro Import feature
3
+ * ------------------
4
+ *
5
+ * Read Excel (.xlsx), CSV, TSV, or JSON in the browser and produce a typed
6
+ * preview of the parsed rows, including per-cell validation errors. The
7
+ * caller decides whether to commit the parsed rows into the grid via
8
+ * `api.addRows(...)`, or to render an editing modal so the user can fix
9
+ * issues first.
10
+ *
11
+ * Two flavours of usage:
12
+ *
13
+ * 1. Read + preview, then commit explicitly:
14
+ *
15
+ * const result = await api.importData({ file, format: 'auto' })
16
+ * // result.headers, result.rows, result.errors
17
+ * if (result.errors.length === 0) api.addRows(result.rows, 'bottom')
18
+ *
19
+ * 2. Read and commit in one go (no preview UX):
20
+ *
21
+ * await api.importData({ file, format: 'auto', commit: true })
22
+ *
23
+ * Both go through the same polite license soft-gate as `exportData` and
24
+ * `print`, so unlicensed evaluation works but emits a one-time nudge.
25
+ */
26
+
27
+ import type { RowData, SvGridApi, TableFeatures } from '@svgrid/grid'
28
+ import { assertEnterpriseLicensed } from './license'
29
+
30
+ // ---------------------------------------------------------------------------
31
+ // Public types
32
+ // ---------------------------------------------------------------------------
33
+
34
+ export type ImportFormat = 'xlsx' | 'csv' | 'tsv' | 'json' | 'auto'
35
+
36
+ export type ImportColumnMap = Record<string, string>
37
+
38
+ /**
39
+ * Declared data type per target field. When `columnTypes` is set on
40
+ * `ImportOptions`, the parser tries to coerce each non-empty cell into
41
+ * the declared shape and emits an `ImportRowError` when it can't -
42
+ * stricter than the ad-hoc "looks like a number" fallback.
43
+ */
44
+ export type ImportFieldType =
45
+ | 'string'
46
+ | 'number'
47
+ | 'integer'
48
+ | 'boolean'
49
+ | 'date' // returns ISO yyyy-mm-dd
50
+ | 'datetime' // returns ISO yyyy-mm-ddThh:mm:ss
51
+ | 'json' // parses the cell as JSON (object / array / primitive)
52
+
53
+ export type ImportColumnTypes = Record<string, ImportFieldType>
54
+
55
+ /**
56
+ * The slice of a grid column the auto-mapper needs: its target `field`,
57
+ * a human `header` to match source headers against, and the `format`
58
+ * config to infer a column type from. Shaped to accept the output of
59
+ * `api.getColumns()` directly.
60
+ */
61
+ export type ImportGridColumn = {
62
+ field?: string
63
+ header?: string
64
+ format?: { type?: string } | null
65
+ }
66
+
67
+ /** Progress ticks emitted during a large import so a UI can show a bar. */
68
+ export type ImportProgress = {
69
+ /** Which stage is running. */
70
+ phase: 'parse' | 'map'
71
+ /** 0..1 completion of the current phase. */
72
+ ratio: number
73
+ /** Rows processed so far in this phase. */
74
+ done: number
75
+ /** Total rows this phase will process (0 when unknown, e.g. mid-parse). */
76
+ total: number
77
+ }
78
+
79
+ export type ImportRowError = {
80
+ /** 0-based row index in the SOURCE file (excluding the header). */
81
+ rowIndex: number
82
+ /** Target field name (the grid's column field), or '*' for whole-row errors. */
83
+ field: string
84
+ message: string
85
+ }
86
+
87
+ export type ImportValidator<TData> = (
88
+ row: TData,
89
+ rowIndex: number,
90
+ ) => Array<{ field: string; message: string }>
91
+
92
+ export type ImportOptions<TData> = {
93
+ /** The file to read. A `File`/`Blob` works for xlsx; a string is treated as
94
+ * inline CSV/TSV/JSON text. */
95
+ file: File | Blob | string
96
+ /** When 'auto', the format is sniffed from `file.name`'s extension (Files
97
+ * only), otherwise the function inspects the first characters of the
98
+ * text payload. */
99
+ format?: ImportFormat
100
+ /**
101
+ * Map source-header -> target-field. Missing entries fall back to the
102
+ * source header verbatim (lowercased + trimmed). Pass `null` for a
103
+ * source header you want to drop on the floor.
104
+ */
105
+ columnMap?: ImportColumnMap
106
+ /**
107
+ * Declared data types per target field. When set, the importer uses
108
+ * strict per-type coercion (`'2024-03-15'` -> ISO date; `'$1,234'`
109
+ * -> 1234 for `number` fields) and emits an `ImportRowError` whenever
110
+ * a value can't be coerced. Fields not listed fall back to the
111
+ * built-in best-effort coercion (currency / number / date sniffing
112
+ * by value shape).
113
+ */
114
+ columnTypes?: ImportColumnTypes
115
+ /** Per-row validator. Returned errors land in `result.errors`. */
116
+ validator?: ImportValidator<TData>
117
+ /**
118
+ * When true, source headers are fuzzy-matched to the grid's own columns
119
+ * (by header label or field name) to build the `columnMap` automatically,
120
+ * and `columnTypes` are inferred from each matched column's `format`
121
+ * (currency / number / percent -> number, date -> date, datetime ->
122
+ * datetime). Anything you pass explicitly in `columnMap` / `columnTypes`
123
+ * wins over the guess. This is what powers "drop a file and it lines up
124
+ * with my grid" in `SvImportDialog`.
125
+ */
126
+ autoMap?: boolean
127
+ /** When true, the parsed rows are appended to the grid via
128
+ * `api.addRows(...)` automatically. Defaults to false (preview mode). */
129
+ commit?: boolean
130
+ /** Where to insert when `commit` is true. Defaults to 'bottom'. */
131
+ commitAt?: 'top' | 'bottom' | number
132
+ /**
133
+ * Drop duplicate rows by this key field, keeping the LAST occurrence
134
+ * (upsert-style, so a later row in the file wins). Runs after mapping,
135
+ * so pass the TARGET field name. Rows with a null/undefined key are kept.
136
+ */
137
+ dedupeBy?: keyof TData | (string & {})
138
+ // --- Enterprise guard-rails ----------------------------------------------
139
+ /** Reject the import if the source has more than this many data rows.
140
+ * Guards against a runaway upload locking up the tab. */
141
+ maxRows?: number
142
+ /**
143
+ * What to do when the source exceeds `maxRows`. `'reject'` (default)
144
+ * throws so no data is silently lost; `'truncate'` keeps the first
145
+ * `maxRows` rows and sets `result.truncated` so you can warn the user.
146
+ * Only affects `maxRows` - `maxBytes` always rejects (a partial binary /
147
+ * CSV can't be truncated safely).
148
+ */
149
+ overLimit?: 'reject' | 'truncate'
150
+ /** Reject a File/Blob larger than this many bytes before it is read. */
151
+ maxBytes?: number
152
+ /** Stop collecting validation errors past this many (keeps memory bounded
153
+ * on a pathological file). `result.errorsTruncated` flags when it hit. */
154
+ maxErrors?: number
155
+ /** Text encoding for CSV/TSV/JSON read from a File/Blob. Default 'utf-8'.
156
+ * Set e.g. 'windows-1252' / 'latin1' for legacy Excel CSV exports. */
157
+ encoding?: string
158
+ /** Abort a long parse/map (wire to an AbortController). Throws an
159
+ * AbortError, matching the export API. */
160
+ signal?: AbortSignal
161
+ /** Progress callback for large files (parse + map phases). */
162
+ onProgress?: (progress: ImportProgress) => void
163
+ }
164
+
165
+ export type ImportResult<TData> = {
166
+ /** Source headers as found in the file, in their original order. */
167
+ headers: string[]
168
+ /** Parsed + mapped rows. Order matches the source. */
169
+ rows: TData[]
170
+ /** All validation errors (zero-length if the file passed clean). */
171
+ errors: ImportRowError[]
172
+ /** Rows skipped because they were entirely blank. */
173
+ skipped: number
174
+ /** Total source rows the parser saw (including blanks and bad rows). */
175
+ total: number
176
+ /** Detected format (resolved from 'auto'). */
177
+ format: Exclude<ImportFormat, 'auto'>
178
+ /** True when `maxErrors` capped the error list (there were more). */
179
+ errorsTruncated?: boolean
180
+ /** Rows dropped by `dedupeBy` (0 when not deduping). */
181
+ deduped?: number
182
+ /** True when `maxRows` + `overLimit:'truncate'` dropped trailing rows.
183
+ * `total` still reports the original source row count. */
184
+ truncated?: boolean
185
+ }
186
+
187
+ // ---------------------------------------------------------------------------
188
+ // Lazy peer dependency: jszip (xlsx only)
189
+ // ---------------------------------------------------------------------------
190
+
191
+ let jszipPromise: Promise<unknown> | null = null
192
+ async function getJSZip(): Promise<any> {
193
+ if (typeof window === 'undefined') {
194
+ throw new Error('@svgrid/enterprise: importData requires a browser environment')
195
+ }
196
+ const g = globalThis as unknown as { JSZip?: unknown }
197
+ if (g.JSZip) return g.JSZip
198
+ if (!jszipPromise) {
199
+ jszipPromise = (async () => {
200
+ let mod: unknown
201
+ try {
202
+ mod = await import('jszip')
203
+ } catch {
204
+ throw new Error(
205
+ '@svgrid/enterprise: xlsx import requires the "jszip" peer dependency. ' +
206
+ 'Install it with: pnpm add jszip',
207
+ )
208
+ }
209
+ const J = (mod as { default?: unknown }).default ?? mod
210
+ g.JSZip = J
211
+ return J
212
+ })()
213
+ }
214
+ return jszipPromise
215
+ }
216
+
217
+ // ---------------------------------------------------------------------------
218
+ // Format sniffing
219
+ // ---------------------------------------------------------------------------
220
+
221
+ function sniffFormat(
222
+ file: File | Blob | string,
223
+ declared: ImportFormat,
224
+ ): Exclude<ImportFormat, 'auto'> {
225
+ if (declared !== 'auto') return declared
226
+ // File: trust the extension.
227
+ if (file instanceof File && file.name) {
228
+ const ext = file.name.toLowerCase().split('.').pop() ?? ''
229
+ if (ext === 'xlsx') return 'xlsx'
230
+ if (ext === 'csv') return 'csv'
231
+ if (ext === 'tsv' || ext === 'tab') return 'tsv'
232
+ if (ext === 'json') return 'json'
233
+ }
234
+ // String: sniff the first non-whitespace char.
235
+ if (typeof file === 'string') {
236
+ const trimmed = file.trimStart()
237
+ if (trimmed.startsWith('[') || trimmed.startsWith('{')) return 'json'
238
+ // Tab-rich heads are tsv; otherwise default to csv.
239
+ const firstLine = trimmed.slice(0, 200).split(/\r?\n/)[0] ?? ''
240
+ if ((firstLine.match(/\t/g)?.length ?? 0) >= 1) return 'tsv'
241
+ return 'csv'
242
+ }
243
+ // Blob without a filename / extension: assume xlsx (the most common case
244
+ // the importer is asked to handle).
245
+ return 'xlsx'
246
+ }
247
+
248
+ // ---------------------------------------------------------------------------
249
+ // CSV / TSV parser
250
+ // ---------------------------------------------------------------------------
251
+
252
+ /**
253
+ * RFC 4180-flavour CSV / TSV parser. Handles:
254
+ * - quoted fields with embedded commas / tabs / newlines
255
+ * - escaped quotes ("") inside quoted fields
256
+ * - both \r\n and \n line endings
257
+ *
258
+ * Doesn't aim to be the world's fastest parser - it walks the string
259
+ * character-by-character. For files in the 10-100k row range this is
260
+ * fine; for million-row imports, do the import server-side.
261
+ */
262
+ function parseDelimited(text: string, sep: ',' | '\t'): string[][] {
263
+ const rows: string[][] = []
264
+ let row: string[] = []
265
+ let field = ''
266
+ let i = 0
267
+ let inQuotes = false
268
+ // Strip a UTF-8 BOM if present so the first header isn't named "ID".
269
+ if (text.charCodeAt(0) === 0xfeff) i = 1
270
+ while (i < text.length) {
271
+ const c = text[i]!
272
+ if (inQuotes) {
273
+ if (c === '"') {
274
+ if (text[i + 1] === '"') { field += '"'; i += 2; continue }
275
+ inQuotes = false
276
+ i += 1
277
+ continue
278
+ }
279
+ field += c
280
+ i += 1
281
+ continue
282
+ }
283
+ if (c === '"') { inQuotes = true; i += 1; continue }
284
+ if (c === sep) { row.push(field); field = ''; i += 1; continue }
285
+ if (c === '\r') {
286
+ if (text[i + 1] === '\n') i += 1
287
+ row.push(field); rows.push(row); row = []; field = ''; i += 1; continue
288
+ }
289
+ if (c === '\n') {
290
+ row.push(field); rows.push(row); row = []; field = ''; i += 1; continue
291
+ }
292
+ field += c
293
+ i += 1
294
+ }
295
+ // Flush the trailing field / row (no trailing newline).
296
+ if (field.length > 0 || row.length > 0) {
297
+ row.push(field)
298
+ rows.push(row)
299
+ }
300
+ return rows
301
+ }
302
+
303
+ // ---------------------------------------------------------------------------
304
+ // XLSX parser (minimal)
305
+ // ---------------------------------------------------------------------------
306
+
307
+ /**
308
+ * Minimal SpreadsheetML reader. Supports the .xlsx shape Excel and Google
309
+ * Sheets produce by default: one default sheet, optional shared-strings
310
+ * table, inline strings, numbers, and dates expressed as serial numbers.
311
+ *
312
+ * The grid does NOT try to be a fully-featured Excel reader. Formulas
313
+ * are read as their cached value when present. Multi-sheet imports
314
+ * require the caller to pre-extract the desired sheet (or for a future
315
+ * release).
316
+ */
317
+ async function parseXlsx(file: File | Blob): Promise<string[][]> {
318
+ const J = await getJSZip()
319
+ const buf = await (file as Blob).arrayBuffer()
320
+ const zip = await J.loadAsync(buf)
321
+
322
+ // Read sharedStrings.xml if it exists.
323
+ let sharedStrings: string[] = []
324
+ const ssEntry = zip.file('xl/sharedStrings.xml')
325
+ if (ssEntry) {
326
+ const ssText = await ssEntry.async('string')
327
+ sharedStrings = parseSharedStrings(ssText)
328
+ }
329
+
330
+ // Read the FIRST sheet. Workbook.xml -> rels mapping is involved; for the
331
+ // 99% case the first sheet lives at xl/worksheets/sheet1.xml.
332
+ const sheetEntry =
333
+ zip.file('xl/worksheets/sheet1.xml') ??
334
+ zip.file('xl/worksheets/sheet 1.xml')
335
+ if (!sheetEntry) {
336
+ throw new Error(
337
+ '@svgrid/enterprise: could not locate sheet1.xml in the .xlsx archive',
338
+ )
339
+ }
340
+ const sheetText = await sheetEntry.async('string')
341
+ return parseSheetXml(sheetText, sharedStrings)
342
+ }
343
+
344
+ /** Pull every `<t>...</t>` out of sharedStrings.xml in document order. */
345
+ function parseSharedStrings(xml: string): string[] {
346
+ const out: string[] = []
347
+ // A shared-string item may be a plain <si><t>X</t></si> or a rich-text
348
+ // <si><r><t>X</t></r><r><t>Y</t></r></si>. Concatenate every <t> inside
349
+ // each <si>.
350
+ const itemRe = /<si\b[^>]*>([\s\S]*?)<\/si>/g
351
+ const tRe = /<t\b[^>]*>([\s\S]*?)<\/t>/g
352
+ let m: RegExpExecArray | null
353
+ while ((m = itemRe.exec(xml)) !== null) {
354
+ let combined = ''
355
+ let n: RegExpExecArray | null
356
+ tRe.lastIndex = 0
357
+ while ((n = tRe.exec(m[1] ?? '')) !== null) {
358
+ combined += decodeXmlEntities(n[1] ?? '')
359
+ }
360
+ out.push(combined)
361
+ }
362
+ return out
363
+ }
364
+
365
+ function decodeXmlEntities(s: string): string {
366
+ return s
367
+ .replace(/&amp;/g, '&')
368
+ .replace(/&lt;/g, '<')
369
+ .replace(/&gt;/g, '>')
370
+ .replace(/&quot;/g, '"')
371
+ .replace(/&apos;/g, "'")
372
+ .replace(/&#10;/g, '\n')
373
+ .replace(/&#9;/g, '\t')
374
+ }
375
+
376
+ /**
377
+ * Convert a sheet's XML body into a dense `string[][]`. Holes in the
378
+ * cell stream (e.g. row jumps from col B to col E) are filled with
379
+ * empty strings so the result is rectangular.
380
+ */
381
+ function parseSheetXml(xml: string, sharedStrings: string[]): string[][] {
382
+ const rows: string[][] = []
383
+ const rowRe = /<row\b[^>]*>([\s\S]*?)<\/row>/g
384
+ const cellRe = /<c\b([^>]*)>([\s\S]*?)<\/c>/g
385
+ const inlineStrRe = /<is>\s*([\s\S]*?)\s*<\/is>/
386
+ const tRe = /<t\b[^>]*>([\s\S]*?)<\/t>/g
387
+ const vRe = /<v>([\s\S]*?)<\/v>/
388
+ let rowMatch: RegExpExecArray | null
389
+ while ((rowMatch = rowRe.exec(xml)) !== null) {
390
+ const rowBody = rowMatch[1] ?? ''
391
+ const cells: string[] = []
392
+ let cellMatch: RegExpExecArray | null
393
+ cellRe.lastIndex = 0
394
+ while ((cellMatch = cellRe.exec(rowBody)) !== null) {
395
+ const attrs = cellMatch[1] ?? ''
396
+ const body = cellMatch[2] ?? ''
397
+ const ref = /r="([A-Z]+)\d+"/.exec(attrs)?.[1] ?? ''
398
+ const colIndex = colRefToIndex(ref)
399
+ const type = /t="([^"]+)"/.exec(attrs)?.[1] ?? 'n'
400
+ // Pad holes with empty strings so column alignment survives.
401
+ while (cells.length < colIndex) cells.push('')
402
+ let value = ''
403
+ if (type === 's') {
404
+ const idx = Number(vRe.exec(body)?.[1] ?? '-1')
405
+ value = sharedStrings[idx] ?? ''
406
+ } else if (type === 'inlineStr' || type === 'str') {
407
+ const inlineBody = inlineStrRe.exec(body)?.[1] ?? body
408
+ let parts = ''
409
+ let n: RegExpExecArray | null
410
+ tRe.lastIndex = 0
411
+ while ((n = tRe.exec(inlineBody)) !== null) parts += decodeXmlEntities(n[1] ?? '')
412
+ value = parts || decodeXmlEntities(vRe.exec(body)?.[1] ?? '')
413
+ } else if (type === 'b') {
414
+ value = vRe.exec(body)?.[1] === '1' ? 'true' : 'false'
415
+ } else {
416
+ // Numeric or date - we surface as the raw number string. The
417
+ // caller's column type then handles parsing.
418
+ value = decodeXmlEntities(vRe.exec(body)?.[1] ?? '')
419
+ }
420
+ cells.push(value)
421
+ }
422
+ rows.push(cells)
423
+ }
424
+ return rows
425
+ }
426
+
427
+ /** Excel column reference ("A", "B", ..., "AA", ...) to a zero-based index. */
428
+ function colRefToIndex(ref: string): number {
429
+ if (!ref) return 0
430
+ let n = 0
431
+ for (let i = 0; i < ref.length; i += 1) {
432
+ n = n * 26 + (ref.charCodeAt(i) - 64)
433
+ }
434
+ return Math.max(0, n - 1)
435
+ }
436
+
437
+ // ---------------------------------------------------------------------------
438
+ // Coerce raw string values into typed JS values based on a sample of other
439
+ // values in the same column. We don't ask the consumer for a schema - it
440
+ // would defeat the "drop a file in and go" UX - but we DO want
441
+ // "$1,234.56" to become 1234.56 and "true" to become true.
442
+ // ---------------------------------------------------------------------------
443
+
444
+ function inferAndCoerce(value: string): unknown {
445
+ if (value === '') return ''
446
+ const trimmed = value.trim()
447
+ if (trimmed === '') return ''
448
+ // Boolean
449
+ if (trimmed === 'true' || trimmed === 'TRUE') return true
450
+ if (trimmed === 'false' || trimmed === 'FALSE') return false
451
+ // Currency / number with grouping. Strip $, commas, whitespace.
452
+ if (/^[-+]?\$?\s?[\d,]+(?:\.\d+)?$/.test(trimmed)) {
453
+ const n = parseFloat(trimmed.replace(/[$,\s]/g, ''))
454
+ if (!Number.isNaN(n)) return n
455
+ }
456
+ // Bare integer / float
457
+ if (/^[-+]?\d+(?:\.\d+)?(?:e[+-]?\d+)?$/i.test(trimmed)) {
458
+ const n = parseFloat(trimmed)
459
+ if (!Number.isNaN(n)) return n
460
+ }
461
+ // ISO date (yyyy-mm-dd, optionally with time) -> keep as ISO string. We
462
+ // don't return a Date because most grid columns prefer strings on the
463
+ // wire (sort + filter + format paths are already date-aware).
464
+ if (/^\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2})?(?:Z|[+-]\d{2}:?\d{2})?)?$/.test(trimmed)) {
465
+ return trimmed
466
+ }
467
+ return trimmed
468
+ }
469
+
470
+ // ---------------------------------------------------------------------------
471
+ // Map source rows -> typed records.
472
+ // ---------------------------------------------------------------------------
473
+
474
+ /**
475
+ * Field names we refuse to write, because assigning them from untrusted file
476
+ * headers would let a malicious upload walk the prototype chain (prototype
477
+ * pollution). Applies to the default snake_case fallback AND anything a
478
+ * columnMap points at.
479
+ */
480
+ const UNSAFE_FIELDS = new Set(['__proto__', 'prototype', 'constructor'])
481
+
482
+ /** Resolve each source header to a target field (or null = drop the column),
483
+ * honouring the columnMap and refusing blank + pollution-prone names. */
484
+ function computeFields(
485
+ headers: string[],
486
+ columnMap: ImportColumnMap | undefined,
487
+ ): Array<string | null> {
488
+ return headers.map((h) => {
489
+ let field: string | null
490
+ if (columnMap && Object.prototype.hasOwnProperty.call(columnMap, h)) {
491
+ field = columnMap[h] ?? null
492
+ } else {
493
+ field = h.toLowerCase().replace(/\s+/g, '_').replace(/[^a-z0-9_]/g, '')
494
+ }
495
+ if (field == null || field === '' || UNSAFE_FIELDS.has(field)) return null
496
+ return field
497
+ })
498
+ }
499
+
500
+ /** Build one typed record from a row of cells. Pure; no side effects on the
501
+ * shared error list (returns its own). */
502
+ function buildRecord(
503
+ cells: string[],
504
+ headerCount: number,
505
+ fields: Array<string | null>,
506
+ columnTypes: ImportColumnTypes | undefined,
507
+ outRowIndex: number,
508
+ ): { rec: Record<string, unknown>; errors: ImportRowError[] } {
509
+ const rec: Record<string, unknown> = {}
510
+ const errors: ImportRowError[] = []
511
+ for (let i = 0; i < headerCount; i += 1) {
512
+ const field = fields[i]
513
+ if (field == null) continue
514
+ const raw = cells[i] ?? ''
515
+ const declaredType = columnTypes?.[field]
516
+ if (declaredType) {
517
+ const result = coerceTyped(raw, declaredType)
518
+ if (result.ok) {
519
+ rec[field] = result.value
520
+ } else {
521
+ errors.push({ rowIndex: outRowIndex, field, message: result.message })
522
+ // Park the raw string so downstream code still sees SOMETHING.
523
+ rec[field] = raw
524
+ }
525
+ } else {
526
+ rec[field] = inferAndCoerce(raw)
527
+ }
528
+ }
529
+ return { rec, errors }
530
+ }
531
+
532
+ /** Push `source` errors into `target`, stopping at `maxErrors`. Returns true
533
+ * when it had to truncate. */
534
+ function collectErrors(
535
+ target: ImportRowError[],
536
+ source: ImportRowError[],
537
+ maxErrors: number | undefined,
538
+ ): boolean {
539
+ for (const e of source) {
540
+ if (maxErrors != null && target.length >= maxErrors) return true
541
+ target.push(e)
542
+ }
543
+ return false
544
+ }
545
+
546
+ /** Drop duplicate rows by `key`, keeping the last occurrence in place. */
547
+ function dedupeRows<TData>(rows: TData[], key: string): { rows: TData[]; deduped: number } {
548
+ const idx = new Map<unknown, number>()
549
+ const out: TData[] = []
550
+ for (const r of rows) {
551
+ const k = (r as Record<string, unknown>)?.[key]
552
+ if (k == null) { out.push(r); continue }
553
+ const at = idx.get(k)
554
+ if (at !== undefined) out[at] = r
555
+ else { idx.set(k, out.length); out.push(r) }
556
+ }
557
+ return { rows: out, deduped: rows.length - out.length }
558
+ }
559
+
560
+ function isBlankRow(cells: string[]): boolean {
561
+ return cells.every((c) => c === undefined || c.trim() === '')
562
+ }
563
+
564
+ function abortError(): Error {
565
+ const e = new Error('@svgrid/enterprise: import cancelled')
566
+ e.name = 'AbortError'
567
+ return e
568
+ }
569
+
570
+ /** Yield to the event loop so a long map doesn't block paint / input. */
571
+ function nextTick(): Promise<void> {
572
+ return new Promise((resolve) => setTimeout(resolve, 0))
573
+ }
574
+
575
+ function buildRecords<TData>(
576
+ matrix: string[][],
577
+ columnMap: ImportColumnMap | undefined,
578
+ columnTypes: ImportColumnTypes | undefined,
579
+ maxErrors?: number,
580
+ ): { headers: string[]; rows: TData[]; skipped: number; errors: ImportRowError[]; errorsTruncated: boolean } {
581
+ if (matrix.length === 0) return { headers: [], rows: [], skipped: 0, errors: [], errorsTruncated: false }
582
+ const headers = (matrix[0] ?? []).map((h) => h.trim())
583
+ const fields = computeFields(headers, columnMap)
584
+ const dataRows = matrix.slice(1)
585
+ const out: TData[] = []
586
+ const errors: ImportRowError[] = []
587
+ let skipped = 0
588
+ let errorsTruncated = false
589
+ // Skipped (blank) rows don't count toward the output row index used
590
+ // in errors, so the indices match what the consumer's preview UI shows.
591
+ let outRowIndex = 0
592
+ for (const cells of dataRows) {
593
+ if (isBlankRow(cells)) { skipped += 1; continue }
594
+ const { rec, errors: rowErrs } = buildRecord(cells, headers.length, fields, columnTypes, outRowIndex)
595
+ if (rowErrs.length && collectErrors(errors, rowErrs, maxErrors)) errorsTruncated = true
596
+ out.push(rec as unknown as TData)
597
+ outRowIndex += 1
598
+ }
599
+ return { headers, rows: out, skipped, errors, errorsTruncated }
600
+ }
601
+
602
+ /**
603
+ * Strict per-type coercion used when the consumer declares
604
+ * `columnTypes`. Returns either `{ ok: true, value }` or
605
+ * `{ ok: false, message }` describing why the value couldn't be
606
+ * shaped into the declared type.
607
+ *
608
+ * Empty values for required types (number, date) error rather than
609
+ * silently coerce to 0 / null - the consumer can drop the row in
610
+ * their validator if blanks should be tolerated.
611
+ */
612
+ function coerceTyped(raw: string, type: ImportFieldType):
613
+ | { ok: true; value: unknown }
614
+ | { ok: false; message: string }
615
+ {
616
+ const trimmed = raw.trim()
617
+ // Empty cells map to `null` for most types and the empty string for
618
+ // `string` - this is the only case where typed import returns a
619
+ // null. Validator step is the right place to insist on "required".
620
+ if (trimmed === '') {
621
+ if (type === 'string') return { ok: true, value: '' }
622
+ return { ok: true, value: null }
623
+ }
624
+
625
+ switch (type) {
626
+ case 'string':
627
+ return { ok: true, value: trimmed }
628
+
629
+ case 'boolean': {
630
+ const low = trimmed.toLowerCase()
631
+ if (low === 'true' || low === '1' || low === 'yes' || low === 'y') return { ok: true, value: true }
632
+ if (low === 'false' || low === '0' || low === 'no' || low === 'n') return { ok: true, value: false }
633
+ return { ok: false, message: `not a boolean: ${trimmed}` }
634
+ }
635
+
636
+ case 'number': {
637
+ const stripped = trimmed.replace(/[$,\s]/g, '')
638
+ const n = Number(stripped)
639
+ if (!Number.isFinite(n)) return { ok: false, message: `not a number: ${trimmed}` }
640
+ return { ok: true, value: n }
641
+ }
642
+
643
+ case 'integer': {
644
+ const stripped = trimmed.replace(/[$,\s]/g, '')
645
+ const n = Number(stripped)
646
+ if (!Number.isFinite(n) || !Number.isInteger(n)) {
647
+ return { ok: false, message: `not an integer: ${trimmed}` }
648
+ }
649
+ return { ok: true, value: n }
650
+ }
651
+
652
+ case 'date': {
653
+ // Accept ISO dates and a handful of common day-first / month-first
654
+ // shapes. Returns ISO yyyy-mm-dd.
655
+ const iso = trimmed.match(/^(\d{4})-(\d{2})-(\d{2})/)
656
+ if (iso) return { ok: true, value: `${iso[1]}-${iso[2]}-${iso[3]}` }
657
+ // mm/dd/yyyy or dd/mm/yyyy - we don't try to disambiguate; the
658
+ // demo can declare the locale via its own validator if it cares.
659
+ const slash = trimmed.match(/^(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/)
660
+ if (slash) {
661
+ const [, a, b, y] = slash
662
+ // Default to mm/dd/yyyy because it's the dominant locale where
663
+ // Excel exports without an explicit ISO format. Consumers
664
+ // wanting EU-style can pre-normalise the file.
665
+ return { ok: true, value: `${y}-${a!.padStart(2, '0')}-${b!.padStart(2, '0')}` }
666
+ }
667
+ const parsed = Date.parse(trimmed)
668
+ if (!Number.isNaN(parsed)) {
669
+ return { ok: true, value: new Date(parsed).toISOString().slice(0, 10) }
670
+ }
671
+ return { ok: false, message: `not a date: ${trimmed}` }
672
+ }
673
+
674
+ case 'datetime': {
675
+ // Accept anything Date.parse handles.
676
+ const parsed = Date.parse(trimmed)
677
+ if (Number.isNaN(parsed)) return { ok: false, message: `not a datetime: ${trimmed}` }
678
+ return { ok: true, value: new Date(parsed).toISOString().slice(0, 19) }
679
+ }
680
+
681
+ case 'json': {
682
+ try {
683
+ return { ok: true, value: JSON.parse(trimmed) }
684
+ } catch {
685
+ return { ok: false, message: `not valid JSON: ${trimmed.slice(0, 30)}...` }
686
+ }
687
+ }
688
+ }
689
+ }
690
+
691
+ // ---------------------------------------------------------------------------
692
+ // Parse-once + map, so a UI (SvImportDialog) can read a file ONCE and then
693
+ // re-map / re-validate as the user tweaks the column mapping without paying
694
+ // the parse (or unzip, for xlsx) cost on every keystroke.
695
+ // ---------------------------------------------------------------------------
696
+
697
+ /**
698
+ * Read a file (or inline text) into a raw `string[][]` matrix - header row
699
+ * first - without any mapping, coercion, or validation. The cheap half of
700
+ * `importData`: parse the bytes once, then feed the matrix to
701
+ * `mapImportMatrix` as many times as the mapping UI needs.
702
+ */
703
+ export async function readImportMatrix(
704
+ file: File | Blob | string,
705
+ format: ImportFormat = 'auto',
706
+ opts: { maxBytes?: number; encoding?: string; signal?: AbortSignal } = {},
707
+ ): Promise<{ format: Exclude<ImportFormat, 'auto'>; matrix: string[][] }> {
708
+ if (opts.signal?.aborted) throw abortError()
709
+ // Guard the byte size BEFORE reading, so an oversized upload never lands
710
+ // in memory.
711
+ if (opts.maxBytes != null && file instanceof Blob && file.size > opts.maxBytes) {
712
+ throw new Error(
713
+ `@svgrid/enterprise: file is ${file.size.toLocaleString()} bytes, ` +
714
+ `over the ${opts.maxBytes.toLocaleString()}-byte import limit.`,
715
+ )
716
+ }
717
+ const fmt = sniffFormat(file, format)
718
+ let matrix: string[][]
719
+ if (fmt === 'xlsx') {
720
+ if (typeof file === 'string') {
721
+ throw new Error(
722
+ '@svgrid/enterprise: xlsx import expects a File or Blob, not a string. ' +
723
+ 'Use format: "csv" or "tsv" for inline text.',
724
+ )
725
+ }
726
+ matrix = await parseXlsx(file)
727
+ } else if (fmt === 'json') {
728
+ const text = typeof file === 'string' ? file : await readText(file, opts.encoding)
729
+ matrix = jsonToMatrix(text)
730
+ } else {
731
+ const sep = fmt === 'tsv' ? '\t' : ','
732
+ const text = typeof file === 'string' ? file : await readText(file, opts.encoding)
733
+ matrix = parseDelimited(text, sep)
734
+ }
735
+ if (opts.signal?.aborted) throw abortError()
736
+ return { format: fmt, matrix }
737
+ }
738
+
739
+ type MapMatrixOptions<TData> = {
740
+ columnMap?: ImportColumnMap
741
+ columnTypes?: ImportColumnTypes
742
+ validator?: ImportValidator<TData>
743
+ maxErrors?: number
744
+ dedupeBy?: keyof TData | (string & {})
745
+ }
746
+
747
+ type MapMatrixResult<TData> = {
748
+ headers: string[]
749
+ rows: TData[]
750
+ errors: ImportRowError[]
751
+ skipped: number
752
+ total: number
753
+ errorsTruncated: boolean
754
+ deduped: number
755
+ }
756
+
757
+ /** Run the validator over built rows, capping + flagging truncation. */
758
+ function applyValidator<TData>(
759
+ rows: TData[],
760
+ errors: ImportRowError[],
761
+ validator: ImportValidator<TData> | undefined,
762
+ maxErrors: number | undefined,
763
+ startIndex = 0,
764
+ ): boolean {
765
+ if (!validator) return false
766
+ let truncated = false
767
+ for (let i = startIndex; i < rows.length; i += 1) {
768
+ const errs = validator(rows[i]!, i)
769
+ if (errs.length) {
770
+ const mapped = errs.map((e) => ({ rowIndex: i, field: e.field, message: e.message }))
771
+ if (collectErrors(errors, mapped, maxErrors)) { truncated = true; break }
772
+ }
773
+ }
774
+ return truncated
775
+ }
776
+
777
+ /**
778
+ * Apply a column map + declared types + validator to an already-parsed
779
+ * matrix. The pure, synchronous half of `importData` - no I/O, no license
780
+ * gate - so a preview UI can call it on every mapping change. For very large
781
+ * files prefer `mapImportMatrixAsync`, which yields to keep the UI live.
782
+ */
783
+ export function mapImportMatrix<TData extends RowData>(
784
+ matrix: string[][],
785
+ opts: MapMatrixOptions<TData> = {},
786
+ ): MapMatrixResult<TData> {
787
+ const built = buildRecords<TData>(matrix, opts.columnMap, opts.columnTypes, opts.maxErrors)
788
+ const errors = built.errors
789
+ // Type-coercion errors come first; the validator runs on the rows we
790
+ // managed to build, so its errors share the same `rowIndex` basis.
791
+ let errorsTruncated = built.errorsTruncated
792
+ if (applyValidator(built.rows, errors, opts.validator, opts.maxErrors)) errorsTruncated = true
793
+ let rows = built.rows
794
+ let deduped = 0
795
+ if (opts.dedupeBy) {
796
+ const r = dedupeRows(rows, opts.dedupeBy as string)
797
+ rows = r.rows
798
+ deduped = r.deduped
799
+ }
800
+ return {
801
+ headers: built.headers,
802
+ rows,
803
+ errors,
804
+ skipped: built.skipped,
805
+ total: matrix.length > 0 ? matrix.length - 1 : 0,
806
+ errorsTruncated,
807
+ deduped,
808
+ }
809
+ }
810
+
811
+ /**
812
+ * Non-blocking sibling of `mapImportMatrix`: yields to the event loop every
813
+ * few thousand rows and reports progress, so mapping a 100k-row file never
814
+ * freezes the tab. Honors an AbortSignal. Used by `importData` and the
815
+ * SvImportDialog preview.
816
+ */
817
+ export async function mapImportMatrixAsync<TData extends RowData>(
818
+ matrix: string[][],
819
+ opts: MapMatrixOptions<TData> & {
820
+ signal?: AbortSignal
821
+ onProgress?: (progress: ImportProgress) => void
822
+ /** Rows per chunk before yielding. Default 5000. */
823
+ chunkSize?: number
824
+ } = {},
825
+ ): Promise<MapMatrixResult<TData>> {
826
+ const headers = (matrix[0] ?? []).map((h) => h.trim())
827
+ const fields = computeFields(headers, opts.columnMap)
828
+ const dataRows = matrix.slice(1)
829
+ const total = dataRows.length
830
+ const chunk = Math.max(1, opts.chunkSize ?? 5000)
831
+ const rows: TData[] = []
832
+ const errors: ImportRowError[] = []
833
+ let skipped = 0
834
+ let outRowIndex = 0
835
+ let errorsTruncated = false
836
+
837
+ for (let i = 0; i < total; i += 1) {
838
+ if (opts.signal?.aborted) throw abortError()
839
+ const cells = dataRows[i]!
840
+ if (isBlankRow(cells)) { skipped += 1 } else {
841
+ const { rec, errors: rowErrs } = buildRecord(cells, headers.length, fields, opts.columnTypes, outRowIndex)
842
+ if (rowErrs.length && collectErrors(errors, rowErrs, opts.maxErrors)) errorsTruncated = true
843
+ rows.push(rec as unknown as TData)
844
+ outRowIndex += 1
845
+ }
846
+ if ((i + 1) % chunk === 0) {
847
+ opts.onProgress?.({ phase: 'map', ratio: total ? (i + 1) / total : 1, done: i + 1, total })
848
+ await nextTick()
849
+ }
850
+ }
851
+
852
+ if (applyValidator(rows, errors, opts.validator, opts.maxErrors)) errorsTruncated = true
853
+
854
+ let finalRows = rows
855
+ let deduped = 0
856
+ if (opts.dedupeBy) {
857
+ const r = dedupeRows(rows, opts.dedupeBy as string)
858
+ finalRows = r.rows
859
+ deduped = r.deduped
860
+ }
861
+ opts.onProgress?.({ phase: 'map', ratio: 1, done: total, total })
862
+ return {
863
+ headers,
864
+ rows: finalRows,
865
+ errors,
866
+ skipped,
867
+ total: matrix.length > 0 ? matrix.length - 1 : 0,
868
+ errorsTruncated,
869
+ deduped,
870
+ }
871
+ }
872
+
873
+ /** Fold a header / field down to letters + digits so "Unit Price", "unit_price"
874
+ * and "UnitPrice" all collapse to the same key. */
875
+ function normalizeHeader(s: string): string {
876
+ return s.toLowerCase().replace(/[^a-z0-9]/g, '')
877
+ }
878
+
879
+ /**
880
+ * Fuzzy-match each source header to one of the grid's columns (by header
881
+ * label first, then field name) and return a `source header -> target
882
+ * field` map. Headers with no confident match are left OUT of the map, so
883
+ * `buildRecords` falls back to its snake_case default for them.
884
+ */
885
+ export function autoMapColumns(
886
+ headers: string[],
887
+ columns: ReadonlyArray<ImportGridColumn>,
888
+ ): ImportColumnMap {
889
+ const byKey = new Map<string, string>()
890
+ for (const c of columns) {
891
+ const field = c.field ?? c.header
892
+ if (!field) continue
893
+ // Field name is the strongest signal, so register header first then let
894
+ // the field key overwrite on a collision.
895
+ if (c.header) byKey.set(normalizeHeader(c.header), field)
896
+ if (c.field) byKey.set(normalizeHeader(c.field), c.field)
897
+ }
898
+ const map: ImportColumnMap = {}
899
+ for (const h of headers) {
900
+ const hit = byKey.get(normalizeHeader(h))
901
+ if (hit) map[h] = hit
902
+ }
903
+ return map
904
+ }
905
+
906
+ /**
907
+ * Infer strict import types from each grid column's `format`, so a currency
908
+ * / number / percent column parses "$1,234" -> 1234 and a date column parses
909
+ * "2026-03-04" -> an ISO date. Columns with no informative format are left
910
+ * out (best-effort coercion applies).
911
+ */
912
+ export function inferImportColumnTypes(
913
+ columns: ReadonlyArray<ImportGridColumn>,
914
+ ): ImportColumnTypes {
915
+ const types: ImportColumnTypes = {}
916
+ for (const c of columns) {
917
+ if (!c.field) continue
918
+ const t = c.format?.type
919
+ if (t === 'number' || t === 'currency' || t === 'percent') types[c.field] = 'number'
920
+ else if (t === 'date') types[c.field] = 'date'
921
+ else if (t === 'datetime') types[c.field] = 'datetime'
922
+ }
923
+ return types
924
+ }
925
+
926
+ // ---------------------------------------------------------------------------
927
+ // Public entry point
928
+ // ---------------------------------------------------------------------------
929
+
930
+ /**
931
+ * Read a file (or inline text), parse, optionally validate + commit. The
932
+ * sister function of `exportGrid`.
933
+ */
934
+ export async function importData<
935
+ TFeatures extends TableFeatures,
936
+ TData extends RowData,
937
+ >(
938
+ api: SvGridApi<TFeatures, TData>,
939
+ opts: ImportOptions<TData>,
940
+ ): Promise<ImportResult<TData>> {
941
+ assertEnterpriseLicensed('Import')
942
+ const { format, matrix } = await readImportMatrix(opts.file, opts.format ?? 'auto', {
943
+ maxBytes: opts.maxBytes,
944
+ encoding: opts.encoding,
945
+ signal: opts.signal,
946
+ })
947
+
948
+ // Enforce the row cap before we do the O(rows) mapping work: either reject
949
+ // outright, or truncate to the first maxRows and flag it.
950
+ const dataCount = matrix.length > 0 ? matrix.length - 1 : 0
951
+ let workMatrix = matrix
952
+ let truncated = false
953
+ if (opts.maxRows != null && dataCount > opts.maxRows) {
954
+ if (opts.overLimit === 'truncate') {
955
+ workMatrix = matrix.slice(0, opts.maxRows + 1) // header + first maxRows
956
+ truncated = true
957
+ } else {
958
+ throw new Error(
959
+ `@svgrid/enterprise: import has ${dataCount.toLocaleString()} rows, ` +
960
+ `over the maxRows limit of ${opts.maxRows.toLocaleString()}.`,
961
+ )
962
+ }
963
+ }
964
+
965
+ // Grid-aware auto-mapping: derive the columnMap + columnTypes from the
966
+ // grid's own columns, but let anything the caller passed win.
967
+ let columnMap = opts.columnMap
968
+ let columnTypes = opts.columnTypes
969
+ if (opts.autoMap) {
970
+ const gridCols = api.getColumns() as ReadonlyArray<ImportGridColumn>
971
+ const headers = (workMatrix[0] ?? []).map((h) => h.trim())
972
+ columnMap = { ...autoMapColumns(headers, gridCols), ...(opts.columnMap ?? {}) }
973
+ columnTypes = { ...inferImportColumnTypes(gridCols), ...(opts.columnTypes ?? {}) }
974
+ }
975
+
976
+ const { headers, rows, errors, skipped, errorsTruncated, deduped } =
977
+ await mapImportMatrixAsync<TData>(workMatrix, {
978
+ columnMap,
979
+ columnTypes,
980
+ validator: opts.validator,
981
+ maxErrors: opts.maxErrors,
982
+ dedupeBy: opts.dedupeBy,
983
+ signal: opts.signal,
984
+ onProgress: opts.onProgress,
985
+ })
986
+
987
+ if (opts.commit && errors.length === 0 && rows.length > 0) {
988
+ api.addRows(rows, opts.commitAt ?? 'bottom')
989
+ }
990
+
991
+ // `total` reports the ORIGINAL source row count even when truncated, so the
992
+ // caller can say "imported X of Y".
993
+ return { headers, rows, errors, skipped, total: dataCount, format, errorsTruncated, deduped, truncated }
994
+ }
995
+
996
+ // ---------------------------------------------------------------------------
997
+ // Helpers
998
+ // ---------------------------------------------------------------------------
999
+
1000
+ async function readText(blob: Blob, encoding?: string): Promise<string> {
1001
+ // The default UTF-8 path is fastest; a declared non-UTF-8 encoding (legacy
1002
+ // Windows-1252 CSVs, etc.) routes through TextDecoder.
1003
+ if (encoding && encoding.toLowerCase() !== 'utf-8' && encoding.toLowerCase() !== 'utf8') {
1004
+ const buf = await blob.arrayBuffer()
1005
+ return new TextDecoder(encoding).decode(buf)
1006
+ }
1007
+ return blob.text()
1008
+ }
1009
+
1010
+ /** Convert JSON text containing an array of objects into a `string[][]`
1011
+ * matrix shaped like CSV: header row, then one row per record. */
1012
+ function jsonToMatrix(text: string): string[][] {
1013
+ const parsed = JSON.parse(text)
1014
+ if (!Array.isArray(parsed)) {
1015
+ throw new Error('@svgrid/enterprise: JSON import expects a top-level array')
1016
+ }
1017
+ if (parsed.length === 0) return []
1018
+ // Collect the union of keys across the first ~50 records so we don't
1019
+ // miss a column present only on later rows.
1020
+ const headers: string[] = []
1021
+ const seen = new Set<string>()
1022
+ const sample = parsed.slice(0, 50) as Record<string, unknown>[]
1023
+ for (const r of sample) {
1024
+ if (r && typeof r === 'object') {
1025
+ for (const k of Object.keys(r)) {
1026
+ if (!seen.has(k)) { seen.add(k); headers.push(k) }
1027
+ }
1028
+ }
1029
+ }
1030
+ const rows: string[][] = [headers.slice()]
1031
+ for (const r of parsed as Array<Record<string, unknown>>) {
1032
+ const row: string[] = []
1033
+ for (const h of headers) {
1034
+ const v = r?.[h]
1035
+ if (v == null) row.push('')
1036
+ else if (typeof v === 'object') row.push(JSON.stringify(v))
1037
+ else row.push(String(v))
1038
+ }
1039
+ rows.push(row)
1040
+ }
1041
+ return rows
1042
+ }