mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
refactor(csv): rename arguments, variables and loop (#5297)
This commit is contained in:
parent
a8d22d2343
commit
e052dcb8ae
28
csv/_io.ts
28
csv/_io.ts
@ -61,39 +61,41 @@ export interface LineReader {
|
||||
}
|
||||
|
||||
export async function parseRecord(
|
||||
line: string,
|
||||
fullLine: string,
|
||||
reader: LineReader,
|
||||
opt: ReadOptions,
|
||||
options: ReadOptions,
|
||||
startLine: number,
|
||||
lineIndex: number = startLine,
|
||||
): Promise<Array<string>> {
|
||||
// line starting with comment character is ignored
|
||||
if (opt.comment && line[0] === opt.comment) {
|
||||
if (options.comment && fullLine[0] === options.comment) {
|
||||
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 quoteLen = quote.length;
|
||||
const separatorLen = opt.separator.length;
|
||||
const separatorLen = options.separator.length;
|
||||
let recordBuffer = "";
|
||||
const fieldIndexes = [] as number[];
|
||||
parseField: while (true) {
|
||||
if (opt.trimLeadingSpace) {
|
||||
if (options.trimLeadingSpace) {
|
||||
line = line.trimStart();
|
||||
}
|
||||
|
||||
if (line.length === 0 || !line.startsWith(quote)) {
|
||||
// Non-quoted string field
|
||||
const i = line.indexOf(opt.separator);
|
||||
const i = line.indexOf(options.separator);
|
||||
let field = line;
|
||||
if (i >= 0) {
|
||||
field = field.substring(0, i);
|
||||
}
|
||||
// Check to make sure a quote does not appear in field.
|
||||
if (!opt.lazyQuotes) {
|
||||
if (!options.lazyQuotes) {
|
||||
const j = field.indexOf(quote);
|
||||
if (j >= 0) {
|
||||
const col = graphemeLength(
|
||||
@ -122,7 +124,7 @@ export async function parseRecord(
|
||||
// `""` sequence (append quote).
|
||||
recordBuffer += quote;
|
||||
line = line.substring(quoteLen);
|
||||
} else if (line.startsWith(opt.separator)) {
|
||||
} else if (line.startsWith(options.separator)) {
|
||||
// `","` sequence (end of field).
|
||||
line = line.substring(separatorLen);
|
||||
fieldIndexes.push(recordBuffer.length);
|
||||
@ -131,7 +133,7 @@ export async function parseRecord(
|
||||
// `"\n` sequence (end of line).
|
||||
fieldIndexes.push(recordBuffer.length);
|
||||
break parseField;
|
||||
} else if (opt.lazyQuotes) {
|
||||
} else if (options.lazyQuotes) {
|
||||
// `"` sequence (bare quote).
|
||||
recordBuffer += quote;
|
||||
} else {
|
||||
@ -150,7 +152,7 @@ export async function parseRecord(
|
||||
fullLine = line;
|
||||
if (r === null) {
|
||||
// Abrupt end of file (EOF or error).
|
||||
if (!opt.lazyQuotes) {
|
||||
if (!options.lazyQuotes) {
|
||||
const col = graphemeLength(fullLine);
|
||||
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.)
|
||||
} else {
|
||||
// Abrupt end of file (EOF on error).
|
||||
if (!opt.lazyQuotes) {
|
||||
if (!options.lazyQuotes) {
|
||||
const col = graphemeLength(fullLine);
|
||||
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
|
||||
}
|
||||
|
10
csv/parse.ts
10
csv/parse.ts
@ -74,20 +74,20 @@ class Parser {
|
||||
return this.#cursor >= this.#input.length;
|
||||
}
|
||||
#parseRecord(startLine: number): string[] | null {
|
||||
let line = this.#readLine();
|
||||
if (line === null) return null;
|
||||
if (line.length === 0) {
|
||||
let fullLine = this.#readLine();
|
||||
if (fullLine === null) return null;
|
||||
if (fullLine.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
let lineIndex = startLine + 1;
|
||||
|
||||
// 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 [];
|
||||
}
|
||||
|
||||
let fullLine = line;
|
||||
let line = fullLine;
|
||||
const quote = '"';
|
||||
const quoteLen = quote.length;
|
||||
const separatorLen = this.#options.separator.length;
|
||||
|
Loading…
Reference in New Issue
Block a user