mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +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 {
|
#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;
|
||||||
|
@ -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",
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user