2019-05-24 09:33:42 -04:00
|
|
|
// Ported from Go:
|
|
|
|
// https://github.com/golang/go/blob/go1.12.5/src/encoding/csv/
|
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
import { BufReader, EOF } from "../io/bufio.ts";
|
2019-05-24 09:33:42 -04:00
|
|
|
import { TextProtoReader } from "../textproto/mod.ts";
|
2019-05-30 09:50:29 -04:00
|
|
|
import { StringReader } from "../io/readers.ts";
|
2019-05-24 09:33:42 -04:00
|
|
|
|
|
|
|
const INVALID_RUNE = ["\r", "\n", '"'];
|
|
|
|
|
|
|
|
export class ParseError extends Error {
|
|
|
|
StartLine: number;
|
|
|
|
Line: number;
|
|
|
|
constructor(start: number, line: number, message: string) {
|
|
|
|
super(message);
|
|
|
|
this.StartLine = start;
|
|
|
|
this.Line = line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
/**
|
|
|
|
* @property comma - Character which separates values. Default: ','
|
|
|
|
* @property comment - Character to start a comment. Default: '#'
|
2019-06-19 00:22:01 -04:00
|
|
|
* @property trimLeadingSpace - Flag to trim the leading space of the value.
|
|
|
|
* Default: 'false'
|
2019-05-30 09:50:29 -04:00
|
|
|
* @property lazyQuotes - Allow unquoted quote in a quoted field or non double
|
2019-06-19 00:22:01 -04:00
|
|
|
* quoted quotes in quoted field Default: 'false'
|
|
|
|
* @property fieldsPerRecord - Enabling the check of fields for each row.
|
|
|
|
* If == 0, first row is used as referal for the number of fields.
|
2019-05-30 09:50:29 -04:00
|
|
|
*/
|
2019-05-24 09:33:42 -04:00
|
|
|
export interface ParseOptions {
|
2019-05-30 09:50:29 -04:00
|
|
|
comma?: string;
|
2019-05-24 09:33:42 -04:00
|
|
|
comment?: string;
|
2019-05-30 09:50:29 -04:00
|
|
|
trimLeadingSpace?: boolean;
|
2019-05-24 09:33:42 -04:00
|
|
|
lazyQuotes?: boolean;
|
|
|
|
fieldsPerRecord?: number;
|
|
|
|
}
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
function chkOptions(opt: ParseOptions): void {
|
2019-05-30 09:50:29 -04:00
|
|
|
if (!opt.comma) opt.comma = ",";
|
|
|
|
if (!opt.trimLeadingSpace) opt.trimLeadingSpace = false;
|
2019-05-24 09:33:42 -04:00
|
|
|
if (
|
2019-05-30 09:50:29 -04:00
|
|
|
INVALID_RUNE.includes(opt.comma!) ||
|
|
|
|
INVALID_RUNE.includes(opt.comment!) ||
|
2019-05-24 09:33:42 -04:00
|
|
|
opt.comma === opt.comment
|
|
|
|
) {
|
2019-05-23 22:04:06 -04:00
|
|
|
throw new Error("Invalid Delimiter");
|
2019-05-24 09:33:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
async function read(
|
2019-05-24 09:33:42 -04:00
|
|
|
Startline: number,
|
|
|
|
reader: BufReader,
|
2019-05-30 09:50:29 -04:00
|
|
|
opt: ParseOptions = { comma: ",", trimLeadingSpace: false }
|
2019-05-23 22:04:06 -04:00
|
|
|
): Promise<string[] | EOF> {
|
2019-05-24 09:33:42 -04:00
|
|
|
const tp = new TextProtoReader(reader);
|
|
|
|
let line: string;
|
|
|
|
let result: string[] = [];
|
|
|
|
let lineIndex = Startline;
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
const r = await tp.readLine();
|
|
|
|
if (r === EOF) return EOF;
|
|
|
|
line = r;
|
2019-05-24 09:33:42 -04:00
|
|
|
// Normalize \r\n to \n on all input lines.
|
|
|
|
if (
|
|
|
|
line.length >= 2 &&
|
|
|
|
line[line.length - 2] === "\r" &&
|
|
|
|
line[line.length - 1] === "\n"
|
|
|
|
) {
|
|
|
|
line = line.substring(0, line.length - 2);
|
|
|
|
line = line + "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
const trimmedLine = line.trimLeft();
|
|
|
|
if (trimmedLine.length === 0) {
|
2019-05-23 22:04:06 -04:00
|
|
|
return [];
|
2019-05-24 09:33:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// line starting with comment character is ignored
|
|
|
|
if (opt.comment && trimmedLine[0] === opt.comment) {
|
2019-05-23 22:04:06 -04:00
|
|
|
return [];
|
2019-05-24 09:33:42 -04:00
|
|
|
}
|
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
result = line.split(opt.comma!);
|
2019-05-24 09:33:42 -04:00
|
|
|
|
|
|
|
let quoteError = false;
|
|
|
|
result = result.map(
|
|
|
|
(r): string => {
|
|
|
|
if (opt.trimLeadingSpace) {
|
|
|
|
r = r.trimLeft();
|
|
|
|
}
|
|
|
|
if (r[0] === '"' && r[r.length - 1] === '"') {
|
|
|
|
r = r.substring(1, r.length - 1);
|
|
|
|
} else if (r[0] === '"') {
|
|
|
|
r = r.substring(1, r.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opt.lazyQuotes) {
|
|
|
|
if (r[0] !== '"' && r.indexOf('"') !== -1) {
|
|
|
|
quoteError = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (quoteError) {
|
2019-05-23 22:04:06 -04:00
|
|
|
throw new ParseError(Startline, lineIndex, 'bare " in non-quoted-field');
|
2019-05-24 09:33:42 -04:00
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
return result;
|
2019-05-24 09:33:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function readAll(
|
|
|
|
reader: BufReader,
|
|
|
|
opt: ParseOptions = {
|
|
|
|
comma: ",",
|
|
|
|
trimLeadingSpace: false,
|
|
|
|
lazyQuotes: false
|
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
): Promise<string[][]> {
|
2019-05-24 09:33:42 -04:00
|
|
|
const result: string[][] = [];
|
|
|
|
let _nbFields: number;
|
|
|
|
let lineResult: string[];
|
|
|
|
let first = true;
|
|
|
|
let lineIndex = 0;
|
2019-05-23 22:04:06 -04:00
|
|
|
chkOptions(opt);
|
2019-05-24 09:33:42 -04:00
|
|
|
|
|
|
|
for (;;) {
|
2019-05-23 22:04:06 -04:00
|
|
|
const r = await read(lineIndex, reader, opt);
|
|
|
|
if (r === EOF) break;
|
|
|
|
lineResult = r;
|
2019-05-24 09:33:42 -04:00
|
|
|
lineIndex++;
|
|
|
|
// If fieldsPerRecord is 0, Read sets it to
|
|
|
|
// the number of fields in the first record
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
if (opt.fieldsPerRecord !== undefined) {
|
|
|
|
if (opt.fieldsPerRecord === 0) {
|
|
|
|
_nbFields = lineResult.length;
|
|
|
|
} else {
|
|
|
|
_nbFields = opt.fieldsPerRecord;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineResult.length > 0) {
|
2019-05-30 08:59:30 -04:00
|
|
|
if (_nbFields! && _nbFields! !== lineResult.length) {
|
2019-05-23 22:04:06 -04:00
|
|
|
throw new ParseError(lineIndex, lineIndex, "wrong number of fields");
|
2019-05-24 09:33:42 -04:00
|
|
|
}
|
|
|
|
result.push(lineResult);
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
return result;
|
2019-05-24 09:33:42 -04:00
|
|
|
}
|
2019-05-30 09:50:29 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HeaderOption provides the column definition
|
|
|
|
* and the parse function for each entry of the
|
|
|
|
* column.
|
|
|
|
*/
|
|
|
|
export interface HeaderOption {
|
|
|
|
name: string;
|
|
|
|
parse?: (input: string) => unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ExtendedParseOptions extends ParseOptions {
|
|
|
|
header: boolean | string[] | HeaderOption[];
|
|
|
|
parse?: (input: unknown) => unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Csv parse helper to manipulate data.
|
|
|
|
* Provides an auto/custom mapper for columns and parse function
|
|
|
|
* for columns and rows.
|
|
|
|
* @param input Input to parse. Can be a string or BufReader.
|
|
|
|
* @param opt options of the parser.
|
|
|
|
* @param [opt.header=false] HeaderOptions
|
|
|
|
* @param [opt.parse=null] Parse function for rows.
|
|
|
|
* Example:
|
|
|
|
* const r = await parseFile('a,b,c\ne,f,g\n', {
|
|
|
|
* header: ["this", "is", "sparta"],
|
|
|
|
* parse: (e: Record<string, unknown>) => {
|
|
|
|
* return { super: e.this, street: e.is, fighter: e.sparta };
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
* // output
|
|
|
|
* [
|
|
|
|
* { super: "a", street: "b", fighter: "c" },
|
|
|
|
* { super: "e", street: "f", fighter: "g" }
|
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
export async function parse(
|
|
|
|
input: string | BufReader,
|
|
|
|
opt: ExtendedParseOptions = {
|
|
|
|
header: false
|
|
|
|
}
|
|
|
|
): Promise<unknown[]> {
|
|
|
|
let r: string[][];
|
|
|
|
if (input instanceof BufReader) {
|
|
|
|
r = await readAll(input, opt);
|
|
|
|
} else {
|
|
|
|
r = await readAll(new BufReader(new StringReader(input)), opt);
|
|
|
|
}
|
|
|
|
if (opt.header) {
|
|
|
|
let headers: HeaderOption[] = [];
|
|
|
|
let i = 0;
|
|
|
|
if (Array.isArray(opt.header)) {
|
|
|
|
if (typeof opt.header[0] !== "string") {
|
|
|
|
headers = opt.header as HeaderOption[];
|
|
|
|
} else {
|
|
|
|
const h = opt.header as string[];
|
|
|
|
headers = h.map(
|
|
|
|
(e): HeaderOption => {
|
|
|
|
return {
|
|
|
|
name: e
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
headers = r.shift()!.map(
|
|
|
|
(e): HeaderOption => {
|
|
|
|
return {
|
|
|
|
name: e
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return r.map(
|
|
|
|
(e): unknown => {
|
|
|
|
if (e.length !== headers.length) {
|
|
|
|
throw `Error number of fields line:${i}`;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
let out: Record<string, unknown> = {};
|
|
|
|
for (let j = 0; j < e.length; j++) {
|
|
|
|
const h = headers[j];
|
|
|
|
if (h.parse) {
|
|
|
|
out[h.name] = h.parse(e[j]);
|
|
|
|
} else {
|
|
|
|
out[h.name] = e[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (opt.parse) {
|
|
|
|
return opt.parse(out);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (opt.parse) {
|
|
|
|
return r.map((e: string[]): unknown => opt.parse!(e));
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|