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

View File

@ -813,6 +813,24 @@ Deno.test({
assertEquals(parse(input, { trimLeadingSpace: true }), output); 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",
);
},
});
}, },
}); });