refactor(csv): rename arguments, variables and loop (#5297)

This commit is contained in:
Tim Reichen 2024-07-04 13:15:24 +02:00 committed by GitHub
parent a8d22d2343
commit e052dcb8ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 18 deletions

View File

@ -61,39 +61,41 @@ export interface LineReader {
} }
export async function parseRecord( export async function parseRecord(
line: string, fullLine: string,
reader: LineReader, reader: LineReader,
opt: ReadOptions, options: ReadOptions,
startLine: number, startLine: number,
lineIndex: number = startLine, lineIndex: number = startLine,
): Promise<Array<string>> { ): Promise<Array<string>> {
// line starting with comment character is ignored // line starting with comment character is ignored
if (opt.comment && line[0] === opt.comment) { if (options.comment && fullLine[0] === options.comment) {
return []; return [];
} }
if (opt.separator === undefined) throw new TypeError("Separator is required"); if (options.separator === undefined) {
throw new TypeError("Separator is required");
}
let fullLine = line; let line = fullLine;
const quote = '"'; const quote = '"';
const quoteLen = quote.length; const quoteLen = quote.length;
const separatorLen = opt.separator.length; const separatorLen = options.separator.length;
let recordBuffer = ""; let recordBuffer = "";
const fieldIndexes = [] as number[]; const fieldIndexes = [] as number[];
parseField: while (true) { parseField: while (true) {
if (opt.trimLeadingSpace) { if (options.trimLeadingSpace) {
line = line.trimStart(); line = line.trimStart();
} }
if (line.length === 0 || !line.startsWith(quote)) { if (line.length === 0 || !line.startsWith(quote)) {
// Non-quoted string field // Non-quoted string field
const i = line.indexOf(opt.separator); const i = line.indexOf(options.separator);
let field = line; let field = line;
if (i >= 0) { if (i >= 0) {
field = field.substring(0, i); field = field.substring(0, i);
} }
// Check to make sure a quote does not appear in field. // Check to make sure a quote does not appear in field.
if (!opt.lazyQuotes) { if (!options.lazyQuotes) {
const j = field.indexOf(quote); const j = field.indexOf(quote);
if (j >= 0) { if (j >= 0) {
const col = graphemeLength( const col = graphemeLength(
@ -122,7 +124,7 @@ export async function parseRecord(
// `""` sequence (append quote). // `""` sequence (append quote).
recordBuffer += quote; recordBuffer += quote;
line = line.substring(quoteLen); line = line.substring(quoteLen);
} else if (line.startsWith(opt.separator)) { } else if (line.startsWith(options.separator)) {
// `","` sequence (end of field). // `","` sequence (end of field).
line = line.substring(separatorLen); line = line.substring(separatorLen);
fieldIndexes.push(recordBuffer.length); fieldIndexes.push(recordBuffer.length);
@ -131,7 +133,7 @@ export async function parseRecord(
// `"\n` sequence (end of line). // `"\n` sequence (end of line).
fieldIndexes.push(recordBuffer.length); fieldIndexes.push(recordBuffer.length);
break parseField; break parseField;
} else if (opt.lazyQuotes) { } else if (options.lazyQuotes) {
// `"` sequence (bare quote). // `"` sequence (bare quote).
recordBuffer += quote; recordBuffer += quote;
} else { } else {
@ -150,7 +152,7 @@ export async function parseRecord(
fullLine = line; fullLine = line;
if (r === null) { if (r === null) {
// Abrupt end of file (EOF or error). // Abrupt end of file (EOF or error).
if (!opt.lazyQuotes) { if (!options.lazyQuotes) {
const col = graphemeLength(fullLine); const col = graphemeLength(fullLine);
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE); throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
} }
@ -160,7 +162,7 @@ export async function parseRecord(
recordBuffer += "\n"; // preserve line feed (This is because TextProtoReader removes it.) recordBuffer += "\n"; // preserve line feed (This is because TextProtoReader removes it.)
} else { } else {
// Abrupt end of file (EOF on error). // Abrupt end of file (EOF on error).
if (!opt.lazyQuotes) { if (!options.lazyQuotes) {
const col = graphemeLength(fullLine); const col = graphemeLength(fullLine);
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE); throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
} }

View File

@ -74,20 +74,20 @@ class Parser {
return this.#cursor >= this.#input.length; return this.#cursor >= this.#input.length;
} }
#parseRecord(startLine: number): string[] | null { #parseRecord(startLine: number): string[] | null {
let line = this.#readLine(); let fullLine = this.#readLine();
if (line === null) return null; if (fullLine === null) return null;
if (line.length === 0) { if (fullLine.length === 0) {
return []; return [];
} }
let lineIndex = startLine + 1; let lineIndex = startLine + 1;
// line starting with comment character is ignored // line starting with comment character is ignored
if (this.#options.comment && line[0] === this.#options.comment) { if (this.#options.comment && fullLine[0] === this.#options.comment) {
return []; return [];
} }
let fullLine = line; let line = fullLine;
const quote = '"'; const quote = '"';
const quoteLen = quote.length; const quoteLen = quote.length;
const separatorLen = this.#options.separator.length; const separatorLen = this.#options.separator.length;