refactor(csv): remove dead code and improve tests (#5151)

This commit is contained in:
Yoshiya Hinosawa 2024-06-26 19:31:53 +09:00 committed by GitHub
parent 2fd7c33676
commit ba256e3579
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 26 deletions

View File

@ -50,35 +50,29 @@ class Parser {
#readLine(): string | null {
if (this.#isEOF()) return null;
if (
!this.#input.startsWith("\r\n", this.#cursor) ||
!this.#input.startsWith("\n", this.#cursor)
) {
let buffer = "";
let hadNewline = false;
while (this.#cursor < this.#input.length) {
if (this.#input.startsWith("\r\n", this.#cursor)) {
hadNewline = true;
this.#cursor += 2;
break;
}
if (
this.#input.startsWith("\n", this.#cursor)
) {
hadNewline = true;
this.#cursor += 1;
break;
}
buffer += this.#input[this.#cursor];
let buffer = "";
let hadNewline = false;
while (this.#cursor < this.#input.length) {
if (this.#input.startsWith("\r\n", this.#cursor)) {
hadNewline = true;
this.#cursor += 2;
break;
}
if (
this.#input.startsWith("\n", this.#cursor)
) {
hadNewline = true;
this.#cursor += 1;
break;
}
if (!hadNewline && buffer.endsWith("\r")) {
buffer = buffer.slice(0, -1);
}
return buffer;
buffer += this.#input[this.#cursor];
this.#cursor += 1;
}
return null;
if (!hadNewline && buffer.endsWith("\r")) {
buffer = buffer.slice(0, -1);
}
return buffer;
}
#isEOF(): boolean {
return this.#cursor >= this.#input.length;

View File

@ -813,6 +813,24 @@ Deno.test({
assertEquals(parse(input, { trimLeadingSpace: true }), output);
},
});
await t.step({
name: "leading line breaks",
fn() {
const input = "\n\na,b,c";
const output = [["a", "b", "c"]];
assertEquals(parse(input), output);
},
});
await t.step({
name: "throws when skipFirstRow=true with empty data",
fn() {
assertThrows(
() => parse("", { skipFirstRow: true }),
Error,
"Headers must be defined",
);
},
});
},
});