2022-04-06 04:46:45 +00:00
|
|
|
// Originally ported from Go:
|
|
|
|
// https://github.com/golang/go/blob/go1.12.5/src/encoding/csv/
|
|
|
|
// Copyright 2011 The Go Authors. All rights reserved. BSD license.
|
|
|
|
// https://github.com/golang/go/blob/master/LICENSE
|
2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-04-06 04:46:45 +00:00
|
|
|
|
2024-07-12 06:23:13 +00:00
|
|
|
import { codePointLength } from "./_shared.ts";
|
2024-07-04 08:34:22 +00:00
|
|
|
|
2024-01-11 06:02:30 +00:00
|
|
|
/** Options for {@linkcode parseRecord}. */
|
2022-04-06 04:46:45 +00:00
|
|
|
export interface ReadOptions {
|
2022-11-25 11:40:23 +00:00
|
|
|
/** Character which separates values.
|
|
|
|
*
|
|
|
|
* @default {","}
|
|
|
|
*/
|
2022-04-06 04:46:45 +00:00
|
|
|
separator?: string;
|
2022-11-25 11:40:23 +00:00
|
|
|
/** Character to start a comment.
|
2023-03-24 03:07:30 +00:00
|
|
|
*
|
|
|
|
* Lines beginning with the comment character without preceding whitespace
|
|
|
|
* are ignored. With leading whitespace the comment character becomes part of
|
|
|
|
* the field, even you provide `trimLeadingSpace: true`.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* @default {"#"}
|
|
|
|
*/
|
2022-04-06 04:46:45 +00:00
|
|
|
comment?: string;
|
2022-11-25 11:40:23 +00:00
|
|
|
/** Flag to trim the leading space of the value.
|
2023-03-24 03:07:30 +00:00
|
|
|
*
|
|
|
|
* This is done even if the field delimiter, `separator`, is white space.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* @default {false}
|
|
|
|
*/
|
2022-04-06 04:46:45 +00:00
|
|
|
trimLeadingSpace?: boolean;
|
2022-11-25 11:40:23 +00:00
|
|
|
/**
|
|
|
|
* Allow unquoted quote in a quoted field or non-double-quoted quotes in
|
|
|
|
* quoted field.
|
|
|
|
*
|
|
|
|
* @default {false}
|
|
|
|
*/
|
2022-04-06 04:46:45 +00:00
|
|
|
lazyQuotes?: boolean;
|
2022-11-25 11:40:23 +00:00
|
|
|
/**
|
2023-03-24 03:07:30 +00:00
|
|
|
* Enabling checking number of expected fields for each row.
|
|
|
|
*
|
|
|
|
* If positive, each record is required to have the given number of fields.
|
2023-08-25 09:04:43 +00:00
|
|
|
* If === 0, it will be set to the number of fields in the first row, so that
|
2023-03-24 03:07:30 +00:00
|
|
|
* future rows must have the same field count.
|
|
|
|
* If negative, no check is made and records may have a variable number of
|
|
|
|
* fields.
|
|
|
|
*
|
2024-08-02 04:24:15 +00:00
|
|
|
* If the wrong number of fields is in a row, a {@linkcode SyntaxError} is
|
|
|
|
* thrown.
|
2022-11-25 11:40:23 +00:00
|
|
|
*/
|
2022-04-06 04:46:45 +00:00
|
|
|
fieldsPerRecord?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface LineReader {
|
|
|
|
readLine(): Promise<string | null>;
|
2023-05-12 02:45:25 +00:00
|
|
|
isEOF(): boolean;
|
2022-04-06 04:46:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function parseRecord(
|
2024-07-04 11:15:24 +00:00
|
|
|
fullLine: string,
|
2022-04-06 04:46:45 +00:00
|
|
|
reader: LineReader,
|
2024-07-04 11:15:24 +00:00
|
|
|
options: ReadOptions,
|
2024-08-01 15:07:52 +00:00
|
|
|
zeroBasedRecordStartLine: number,
|
|
|
|
zeroBasedLine: number = zeroBasedRecordStartLine,
|
2024-06-27 03:48:19 +00:00
|
|
|
): Promise<Array<string>> {
|
2022-04-06 04:46:45 +00:00
|
|
|
// line starting with comment character is ignored
|
2024-07-04 11:15:24 +00:00
|
|
|
if (options.comment && fullLine[0] === options.comment) {
|
2022-04-06 04:46:45 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-07-04 11:15:24 +00:00
|
|
|
if (options.separator === undefined) {
|
2024-08-26 05:11:55 +00:00
|
|
|
throw new TypeError("Cannot parse record: separator is required");
|
2024-07-04 11:15:24 +00:00
|
|
|
}
|
2022-04-06 04:46:45 +00:00
|
|
|
|
2024-07-04 11:15:24 +00:00
|
|
|
let line = fullLine;
|
2022-04-06 04:46:45 +00:00
|
|
|
const quote = '"';
|
|
|
|
const quoteLen = quote.length;
|
2024-07-04 11:15:24 +00:00
|
|
|
const separatorLen = options.separator.length;
|
2022-04-06 04:46:45 +00:00
|
|
|
let recordBuffer = "";
|
|
|
|
const fieldIndexes = [] as number[];
|
2024-07-02 03:17:58 +00:00
|
|
|
parseField: while (true) {
|
2024-07-04 11:15:24 +00:00
|
|
|
if (options.trimLeadingSpace) {
|
2022-04-06 04:46:45 +00:00
|
|
|
line = line.trimStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.length === 0 || !line.startsWith(quote)) {
|
|
|
|
// Non-quoted string field
|
2024-07-04 11:15:24 +00:00
|
|
|
const i = line.indexOf(options.separator);
|
2022-04-06 04:46:45 +00:00
|
|
|
let field = line;
|
|
|
|
if (i >= 0) {
|
|
|
|
field = field.substring(0, i);
|
|
|
|
}
|
|
|
|
// Check to make sure a quote does not appear in field.
|
2024-07-04 11:15:24 +00:00
|
|
|
if (!options.lazyQuotes) {
|
2022-04-06 04:46:45 +00:00
|
|
|
const j = field.indexOf(quote);
|
|
|
|
if (j >= 0) {
|
2024-07-12 06:23:13 +00:00
|
|
|
const col = codePointLength(
|
2024-07-04 08:34:22 +00:00
|
|
|
fullLine.slice(0, fullLine.length - line.slice(j).length),
|
|
|
|
);
|
2024-07-16 07:35:40 +00:00
|
|
|
throw new SyntaxError(
|
2024-08-01 15:07:52 +00:00
|
|
|
createBareQuoteErrorMessage(
|
|
|
|
zeroBasedRecordStartLine,
|
|
|
|
zeroBasedLine,
|
|
|
|
col,
|
|
|
|
),
|
2024-07-16 07:35:40 +00:00
|
|
|
);
|
2022-04-06 04:46:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
recordBuffer += field;
|
|
|
|
fieldIndexes.push(recordBuffer.length);
|
|
|
|
if (i >= 0) {
|
|
|
|
line = line.substring(i + separatorLen);
|
|
|
|
continue parseField;
|
|
|
|
}
|
|
|
|
break parseField;
|
|
|
|
} else {
|
|
|
|
// Quoted string field
|
|
|
|
line = line.substring(quoteLen);
|
2024-07-02 03:17:58 +00:00
|
|
|
while (true) {
|
2022-04-06 04:46:45 +00:00
|
|
|
const i = line.indexOf(quote);
|
|
|
|
if (i >= 0) {
|
|
|
|
// Hit next quote.
|
|
|
|
recordBuffer += line.substring(0, i);
|
|
|
|
line = line.substring(i + quoteLen);
|
|
|
|
if (line.startsWith(quote)) {
|
|
|
|
// `""` sequence (append quote).
|
|
|
|
recordBuffer += quote;
|
|
|
|
line = line.substring(quoteLen);
|
2024-07-04 11:15:24 +00:00
|
|
|
} else if (line.startsWith(options.separator)) {
|
2022-04-06 04:46:45 +00:00
|
|
|
// `","` sequence (end of field).
|
|
|
|
line = line.substring(separatorLen);
|
|
|
|
fieldIndexes.push(recordBuffer.length);
|
|
|
|
continue parseField;
|
|
|
|
} else if (0 === line.length) {
|
|
|
|
// `"\n` sequence (end of line).
|
|
|
|
fieldIndexes.push(recordBuffer.length);
|
|
|
|
break parseField;
|
2024-07-04 11:15:24 +00:00
|
|
|
} else if (options.lazyQuotes) {
|
2022-04-06 04:46:45 +00:00
|
|
|
// `"` sequence (bare quote).
|
|
|
|
recordBuffer += quote;
|
|
|
|
} else {
|
|
|
|
// `"*` sequence (invalid non-escaped quote).
|
2024-07-12 06:23:13 +00:00
|
|
|
const col = codePointLength(
|
2024-07-04 08:34:22 +00:00
|
|
|
fullLine.slice(0, fullLine.length - line.length - quoteLen),
|
|
|
|
);
|
2024-07-16 07:35:40 +00:00
|
|
|
throw new SyntaxError(
|
2024-08-01 15:07:52 +00:00
|
|
|
createQuoteErrorMessage(
|
|
|
|
zeroBasedRecordStartLine,
|
|
|
|
zeroBasedLine,
|
|
|
|
col,
|
|
|
|
),
|
2024-07-16 07:35:40 +00:00
|
|
|
);
|
2022-04-06 04:46:45 +00:00
|
|
|
}
|
2023-05-12 02:45:25 +00:00
|
|
|
} else if (line.length > 0 || !reader.isEOF()) {
|
2022-04-06 04:46:45 +00:00
|
|
|
// Hit end of line (copy all data so far).
|
|
|
|
recordBuffer += line;
|
|
|
|
const r = await reader.readLine();
|
|
|
|
line = r ?? ""; // This is a workaround for making this module behave similarly to the encoding/csv/reader.go.
|
|
|
|
fullLine = line;
|
|
|
|
if (r === null) {
|
|
|
|
// Abrupt end of file (EOF or error).
|
2024-07-04 11:15:24 +00:00
|
|
|
if (!options.lazyQuotes) {
|
2024-07-12 06:23:13 +00:00
|
|
|
const col = codePointLength(fullLine);
|
2024-07-16 07:35:40 +00:00
|
|
|
throw new SyntaxError(
|
2024-08-01 15:07:52 +00:00
|
|
|
createQuoteErrorMessage(
|
|
|
|
zeroBasedRecordStartLine,
|
|
|
|
zeroBasedLine,
|
|
|
|
col,
|
|
|
|
),
|
2024-07-16 07:35:40 +00:00
|
|
|
);
|
2022-04-06 04:46:45 +00:00
|
|
|
}
|
|
|
|
fieldIndexes.push(recordBuffer.length);
|
|
|
|
break parseField;
|
|
|
|
}
|
2024-08-01 15:07:52 +00:00
|
|
|
zeroBasedLine++;
|
2022-04-06 04:46:45 +00:00
|
|
|
recordBuffer += "\n"; // preserve line feed (This is because TextProtoReader removes it.)
|
|
|
|
} else {
|
|
|
|
// Abrupt end of file (EOF on error).
|
2024-07-04 11:15:24 +00:00
|
|
|
if (!options.lazyQuotes) {
|
2024-07-12 06:23:13 +00:00
|
|
|
const col = codePointLength(fullLine);
|
2024-07-16 07:35:40 +00:00
|
|
|
throw new SyntaxError(
|
2024-08-01 15:07:52 +00:00
|
|
|
createQuoteErrorMessage(
|
|
|
|
zeroBasedRecordStartLine,
|
|
|
|
zeroBasedLine,
|
|
|
|
col,
|
|
|
|
),
|
2024-07-16 07:35:40 +00:00
|
|
|
);
|
2022-04-06 04:46:45 +00:00
|
|
|
}
|
|
|
|
fieldIndexes.push(recordBuffer.length);
|
|
|
|
break parseField;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const result = [] as string[];
|
|
|
|
let preIdx = 0;
|
|
|
|
for (const i of fieldIndexes) {
|
|
|
|
result.push(recordBuffer.slice(preIdx, i));
|
|
|
|
preIdx = i;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-07-16 07:35:40 +00:00
|
|
|
export function createBareQuoteErrorMessage(
|
2024-08-01 15:07:52 +00:00
|
|
|
zeroBasedRecordStartLine: number,
|
|
|
|
zeroBasedLine: number,
|
|
|
|
zeroBasedColumn: number,
|
2024-07-16 07:35:40 +00:00
|
|
|
) {
|
2024-08-26 05:11:55 +00:00
|
|
|
return `Syntax error on line ${
|
|
|
|
zeroBasedRecordStartLine + 1
|
|
|
|
}; parse error on line ${zeroBasedLine + 1}, column ${
|
|
|
|
zeroBasedColumn + 1
|
|
|
|
}: bare " in non-quoted-field`;
|
2024-07-16 07:35:40 +00:00
|
|
|
}
|
|
|
|
export function createQuoteErrorMessage(
|
2024-08-01 15:07:52 +00:00
|
|
|
zeroBasedRecordStartLine: number,
|
|
|
|
zeroBasedLine: number,
|
|
|
|
zeroBasedColumn: number,
|
2024-07-16 07:35:40 +00:00
|
|
|
) {
|
2024-08-26 05:11:55 +00:00
|
|
|
return `Syntax error on line ${
|
|
|
|
zeroBasedRecordStartLine + 1
|
|
|
|
}; parse error on line ${zeroBasedLine + 1}, column ${
|
|
|
|
zeroBasedColumn + 1
|
|
|
|
}: extraneous or missing " in quoted-field`;
|
2022-04-06 04:46:45 +00:00
|
|
|
}
|
2023-02-17 07:57:34 +00:00
|
|
|
|
|
|
|
export function convertRowToObject(
|
2024-08-20 03:57:52 +00:00
|
|
|
row: readonly string[],
|
2023-04-04 04:53:16 +00:00
|
|
|
headers: readonly string[],
|
2024-08-02 04:24:15 +00:00
|
|
|
zeroBasedLine: number,
|
2023-02-17 07:57:34 +00:00
|
|
|
) {
|
|
|
|
if (row.length !== headers.length) {
|
|
|
|
throw new Error(
|
2024-08-26 05:11:55 +00:00
|
|
|
`Syntax error on line ${
|
2024-08-02 04:24:15 +00:00
|
|
|
zeroBasedLine + 1
|
2024-08-26 05:11:55 +00:00
|
|
|
}: The record has ${row.length} fields, but the header has ${headers.length} fields`,
|
2023-02-17 07:57:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
const out: Record<string, unknown> = {};
|
2024-01-10 21:35:50 +00:00
|
|
|
for (const [index, header] of headers.entries()) {
|
|
|
|
out[header] = row[index];
|
2023-02-17 07:57:34 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2024-06-27 07:24:30 +00:00
|
|
|
/** Parse result type for {@linkcode parse} and {@linkcode CsvParseStream}. */
|
2023-04-04 04:53:16 +00:00
|
|
|
export type ParseResult<ParseOptions, T> =
|
|
|
|
// If `columns` option is specified, the return type is Record type.
|
|
|
|
T extends ParseOptions & { columns: readonly (infer C extends string)[] }
|
|
|
|
? RecordWithColumn<C>[]
|
2024-01-11 06:02:30 +00:00
|
|
|
// If `skipFirstRow` option is specified, the return type is Record type.
|
2024-08-02 05:07:10 +00:00
|
|
|
: T extends ParseOptions & { skipFirstRow: true } ? Record<string, string>[]
|
2024-01-11 06:02:30 +00:00
|
|
|
// If `columns` and `skipFirstRow` option is _not_ specified, the return type is string[][].
|
|
|
|
: T extends
|
|
|
|
ParseOptions & { columns?: undefined; skipFirstRow?: false | undefined }
|
|
|
|
? string[][]
|
|
|
|
// else, the return type is Record type or string[][].
|
2024-08-02 05:07:10 +00:00
|
|
|
: Record<string, string>[] | string[][];
|
2023-04-04 04:53:16 +00:00
|
|
|
|
2024-01-11 06:02:30 +00:00
|
|
|
/**
|
|
|
|
* Record type with column type.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```
|
|
|
|
* type RecordWithColumn<"aaa"|"bbb"> => Record<"aaa"|"bbb", string>
|
|
|
|
* type RecordWithColumn<string> => Record<string, string | undefined>
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export type RecordWithColumn<C extends string> = string extends C
|
2024-08-02 05:07:10 +00:00
|
|
|
? Record<string, string>
|
2023-04-04 04:53:16 +00:00
|
|
|
: Record<C, string>;
|