( rows: Record<string, unknown>[], tableSchema: TableSchema, headerToColumn: Map<string, string> )
| 405 | * acceptable). Pass the schema returned by `createTable` so ids are resolved. |
| 406 | */ |
| 407 | export function coerceRowsForTable( |
| 408 | rows: Record<string, unknown>[], |
| 409 | tableSchema: TableSchema, |
| 410 | headerToColumn: Map<string, string> |
| 411 | ): RowData[] { |
| 412 | const colByName = new Map(tableSchema.columns.map((c) => [c.name, c])) |
| 413 | |
| 414 | return rows.map((row) => { |
| 415 | const coerced: RowData = {} |
| 416 | for (const [header, value] of Object.entries(row)) { |
| 417 | const colName = headerToColumn.get(header) |
| 418 | if (!colName) continue |
| 419 | const col = colByName.get(colName) |
| 420 | if (!col) continue |
| 421 | const colType = (col.type as CsvColumnType) ?? 'string' |
| 422 | coerced[getColumnId(col)] = coerceValue(value, colType) as RowData[string] |
| 423 | } |
| 424 | return coerced |
| 425 | }) |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Sanitizes raw JSON keys so they conform to the same column-name rules as CSV |
no test coverage detected