| 225 | } |
| 226 | |
| 227 | export function convertRowToObject( |
| 228 | row: readonly string[], |
| 229 | headers: readonly string[], |
| 230 | zeroBasedLine: number, |
| 231 | ) { |
| 232 | if (row.length !== headers.length) { |
| 233 | throw new Error( |
| 234 | `Syntax error on line ${ |
| 235 | zeroBasedLine + 1 |
| 236 | }: The record has ${row.length} fields, but the header has ${headers.length} fields`, |
| 237 | ); |
| 238 | } |
| 239 | const out: Record<string, unknown> = {}; |
| 240 | for (const [index, header] of headers.entries()) { |
| 241 | out[header] = row[index]; |
| 242 | } |
| 243 | return out; |
| 244 | } |
| 245 | |
| 246 | /** Parse result type for {@linkcode parse} and {@linkcode CsvParseStream}. */ |
| 247 | export type ParseResult<ParseOptions, T> = |