Open
Description
Describe the bug
According to fieldsPerRecord
documentation, I should be able to parse CSV with variable length records. I can successfully do so but not when I use skipFirstRow
and/or columns
.
working example
import { CsvParseStream } from "jsr:@std/[email protected]/parse-stream";
import { assertEquals } from "jsr:@std/[email protected]/equals";
import { assertType, IsExact } from "jsr:@std/[email protected]/types";
const source = ReadableStream.from([
"name,age\n",
"Alice,34\n",
"Bob\n", // incomplete record
]);
const stream = source.pipeThrough(new CsvParseStream({ fieldsPerRecord: -1 }));
const result = await Array.fromAsync(stream);
assertEquals(result, [
["name", "age"],
["Alice", "34"],
["Bob"],
]);
assertType<IsExact<typeof result, string[][]>>(true);
Steps to Reproduce
-
Create a script with the contents of any of the following examples
not working with
skipFirstRow
import { CsvParseStream } from "jsr:@std/[email protected]/parse-stream"; import { assertEquals } from "jsr:@std/[email protected]/equals"; import { assertType, IsExact } from "jsr:@std/[email protected]/types"; const source = ReadableStream.from([ "name,age\n", "Alice,34\n", "Bob\n", // incomplete record ]); const stream = source.pipeThrough( new CsvParseStream({ fieldsPerRecord: -1, skipFirstRow: true, }), ); const result = await Array.fromAsync(stream); assertEquals(result, [ { name: "Alice", age: "34" }, { name: "Bob", age: undefined }, ]); assertType<IsExact<typeof result, Record<string, string | undefined>[]>>(true);
not working with
columns
import { CsvParseStream } from "jsr:@std/[email protected]/parse-stream"; import { assertEquals } from "jsr:@std/[email protected]/equals"; import { assertType, IsExact } from "jsr:@std/[email protected]/types"; const source = ReadableStream.from([ "Alice,34\n", "Bob\n", // incomplete record ]); const stream = source.pipeThrough( new CsvParseStream({ fieldsPerRecord: -1, columns: ["name", "age"], }), ); const result = await Array.fromAsync(stream); assertEquals(result, [ { name: "Alice", age: "34" }, { name: "Bob", age: undefined }, ]); assertType< IsExact<typeof result, Record<"name" | "age", string | undefined>[]> >(true);
not working with
skipFirstRow
andcolumns
import { CsvParseStream } from "jsr:@std/[email protected]/parse-stream"; import { assertEquals } from "jsr:@std/[email protected]/equals"; import { assertType, IsExact } from "jsr:@std/[email protected]/types"; const source = ReadableStream.from([ "name,age\n", "Alice,34\n", "Bob\n", // incomplete record ]); const stream = source.pipeThrough( new CsvParseStream({ fieldsPerRecord: -1, skipFirstRow: true, columns: ["name", "age"], }), ); const result = await Array.fromAsync(stream); assertEquals(result, [ { name: "Alice", age: "34" }, { name: "Bob", age: undefined }, ]); assertType< IsExact<typeof result, Record<"name" | "age", string | undefined>[]> >(true);
-
Execute the script with
deno run
-
See error:
error: Uncaught (in promise) Error: Syntax error on line 3: The record has 1 fields, but the header has 2 fields throw new Error( ^ at convertRowToObject (https://jsr.io/@std/csv/1.0.5/_io.ts:233:11) at CsvParseStream.#pull (https://jsr.io/@std/csv/1.0.5/parse_stream.ts:460:28)
Expected behavior
- The scripts should run successfully.
- TypeScript type checking should also pass.
Record<T, string | undefined>
orPartial<Record<T, string>>
should be used instead ofRecord<T, string>
Environment
- OS: MacOS 15.3.1
- deno version: 2.2.0
- std version: jsr:@std/[email protected]