Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(csv): remove dead code and improve CsvParseStream test #5153

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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