(csvHeaders: string[], tableSchema: TableSchema)
| 365 | * comparison. Unmapped headers are set to `null`. |
| 366 | */ |
| 367 | export function buildAutoMapping(csvHeaders: string[], tableSchema: TableSchema): CsvHeaderMapping { |
| 368 | const mapping: CsvHeaderMapping = {} |
| 369 | const columns = tableSchema.columns |
| 370 | |
| 371 | const exactByName = new Map(columns.map((c) => [c.name, c.name])) |
| 372 | const loose = new Map<string, string>() |
| 373 | for (const col of columns) { |
| 374 | loose.set(col.name.toLowerCase().replace(/[^a-z0-9]/g, ''), col.name) |
| 375 | } |
| 376 | |
| 377 | const usedTargets = new Set<string>() |
| 378 | |
| 379 | for (const header of csvHeaders) { |
| 380 | const sanitized = sanitizeName(header) |
| 381 | const exact = exactByName.get(sanitized) |
| 382 | if (exact && !usedTargets.has(exact)) { |
| 383 | mapping[header] = exact |
| 384 | usedTargets.add(exact) |
| 385 | continue |
| 386 | } |
| 387 | const key = header.toLowerCase().replace(/[^a-z0-9]/g, '') |
| 388 | const fuzzy = loose.get(key) |
| 389 | if (fuzzy && !usedTargets.has(fuzzy)) { |
| 390 | mapping[header] = fuzzy |
| 391 | usedTargets.add(fuzzy) |
| 392 | continue |
| 393 | } |
| 394 | mapping[header] = null |
| 395 | } |
| 396 | |
| 397 | return mapping |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Coerces parsed CSV rows into `RowData` objects keyed by the target column's |
no test coverage detected