Skip to content

Commit

Permalink
refactor(csv): remove dead code and improve CsvParseStream test (#5153
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kt3k authored Jun 27, 2024
1 parent 9144a62 commit c8a8859
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion csv/_io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function parseRecord(
opt: ReadOptions,
startLine: number,
lineIndex: number = startLine,
): Promise<Array<string> | null> {
): Promise<Array<string>> {
// line starting with comment character is ignored
if (opt.comment && line[0] === opt.comment) {
return [];
Expand Down
5 changes: 0 additions & 5 deletions csv/csv_parse_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,6 @@ export class CsvParseStream<
this.#options,
this.#lineIndex,
);
if (record === null) {
controller.close();
this.#lineReader.cancel();
return;
}

if (this.#isFirstRow) {
this.#isFirstRow = false;
Expand Down
43 changes: 39 additions & 4 deletions csv/csv_parse_stream_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ Deno.test({
output: [["a", "b", "c"]],
separator: ";",
},
{
name: "Separator is undefined",
input: "a;b;c\n",
errorMessage: "Separator is required",
separator: undefined,
},
{
name: "MultiLine",
input: `"two
Expand All @@ -108,6 +114,12 @@ field"`,
input: " a, b, c\n",
output: [[" a", " b", " c"]],
},
{
name: "trimLeadingSpace = true",
input: " a, b, c\n",
output: [["a", "b", "c"]],
trimLeadingSpace: true,
},
{
name: "Comment",
input: "#1,2,3\na,b,c\n#comment",
Expand Down Expand Up @@ -310,22 +322,45 @@ x,,,
errorMessage:
"Error number of fields line: 1\nNumber of fields found: 3\nExpected number of fields: 2",
},
{
name: "bad quote in bare field",
input: `a "word",1,2,3`,
errorMessage: "Error line: 1\nBad quoting",
},
{
name: "bad quote in quoted field",
input: `"wo"rd",1,2,3`,
errorMessage: "Error line: 1\nBad quoting",
},
{
name: "lazy quote",
input: `a "word","1"2",a","b`,
output: [[`a "word"`, `1"2`, `a"`, `b`]],
lazyQuotes: true,
},
];
for (const testCase of testCases) {
await t.step(testCase.name, async () => {
const options: CsvParseStreamOptions = {};
if (testCase.separator) {
if ("separator" in testCase) {
options.separator = testCase.separator;
}
if (testCase.comment) {
if ("comment" in testCase) {
options.comment = testCase.comment;
}
if (testCase.skipFirstRow) {
if ("skipFirstRow" in testCase) {
options.skipFirstRow = testCase.skipFirstRow;
}
if (testCase.columns) {
if ("columns" in testCase) {
options.columns = testCase.columns;
}
if ("trimLeadingSpace" in testCase) {
options.trimLeadingSpace = testCase.trimLeadingSpace;
}
if ("lazyQuotes" in testCase) {
options.lazyQuotes = testCase.lazyQuotes;
}

const readable = ReadableStream.from(testCase.input)
.pipeThrough(new CsvParseStream(options));

Expand Down

0 comments on commit c8a8859

Please sign in to comment.