( headers: string[], rows: Record<string, unknown>[] )
| 432 | * underscore. Returns the headers and rows untouched when no key needs renaming. |
| 433 | */ |
| 434 | export function sanitizeJsonHeaders( |
| 435 | headers: string[], |
| 436 | rows: Record<string, unknown>[] |
| 437 | ): { headers: string[]; rows: Record<string, unknown>[] } { |
| 438 | const renamed = new Map<string, string>() |
| 439 | const seen = new Set<string>() |
| 440 | |
| 441 | for (const raw of headers) { |
| 442 | let safe = sanitizeName(raw) |
| 443 | while (seen.has(safe)) safe = `${safe}_` |
| 444 | seen.add(safe) |
| 445 | renamed.set(raw, safe) |
| 446 | } |
| 447 | |
| 448 | const noChange = headers.every((h) => renamed.get(h) === h) |
| 449 | if (noChange) return { headers, rows } |
| 450 | |
| 451 | return { |
| 452 | headers: headers.map((h) => renamed.get(h)!), |
| 453 | rows: rows.map((row) => { |
| 454 | const out: Record<string, unknown> = {} |
| 455 | for (const [raw, safe] of renamed) { |
| 456 | if (raw in row) out[safe] = row[raw] |
| 457 | } |
| 458 | return out |
| 459 | }), |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Parses a JSON payload that must be an array of plain objects into the same |
no test coverage detected