mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
refactor(csv): remove dead code and improve tests (#5151)
This commit is contained in:
parent
2fd7c33676
commit
ba256e3579
46
csv/parse.ts
46
csv/parse.ts
@ -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;
|
||||
|
@ -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",
|
||||
);
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user