import { BufReader } from "../io/bufio.ts";import { TextProtoReader } from "../textproto/mod.ts";import { StringReader } from "../io/readers.ts";import { assert } from "../testing/asserts.ts";
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; }}
export interface ReadOptions { comma?: string; comment?: string; trimLeadingSpace?: boolean; lazyQuotes?: boolean; fieldsPerRecord?: number;}
function chkOptions(opt: ReadOptions): void { if (!opt.comma) { opt.comma = ","; } if (!opt.trimLeadingSpace) { opt.trimLeadingSpace = false; } if ( INVALID_RUNE.includes(opt.comma) || (typeof opt.comment === "string" && INVALID_RUNE.includes(opt.comment)) || opt.comma === opt.comment ) { throw new Error("Invalid Delimiter"); }}
async function read( Startline: number, reader: BufReader, opt: ReadOptions = { comma: ",", trimLeadingSpace: false }): Promise<string[] | Deno.EOF> { const tp = new TextProtoReader(reader); let line: string; let result: string[] = []; const lineIndex = Startline;
const r = await tp.readLine(); if (r === Deno.EOF) return Deno.EOF; line = r; 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) { return []; }
if (opt.comment && trimmedLine[0] === opt.comment) { return []; }
assert(opt.comma != null); result = line.split(opt.comma);
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) { throw new ParseError(Startline, lineIndex, 'bare " in non-quoted-field'); } return result;}
export async function readMatrix( reader: BufReader, opt: ReadOptions = { comma: ",", trimLeadingSpace: false, lazyQuotes: false }): Promise<string[][]> { const result: string[][] = []; let _nbFields: number | undefined; let lineResult: string[]; let first = true; let lineIndex = 0; chkOptions(opt);
for (;;) { const r = await read(lineIndex, reader, opt); if (r === Deno.EOF) break; lineResult = r; lineIndex++; if (first) { first = false; if (opt.fieldsPerRecord !== undefined) { if (opt.fieldsPerRecord === 0) { _nbFields = lineResult.length; } else { _nbFields = opt.fieldsPerRecord; } } }
if (lineResult.length > 0) { if (_nbFields && _nbFields !== lineResult.length) { throw new ParseError(lineIndex, lineIndex, "wrong number of fields"); } result.push(lineResult); } } return result;}
export interface HeaderOptions { name: string; parse?: (input: string) => unknown;}
export interface ParseOptions extends ReadOptions { header: boolean | string[] | HeaderOptions[]; parse?: (input: unknown) => unknown;}
export async function parse( input: string | BufReader, opt: ParseOptions = { header: false }): Promise<unknown[]> { let r: string[][]; if (input instanceof BufReader) { r = await readMatrix(input, opt); } else { r = await readMatrix(new BufReader(new StringReader(input)), opt); } if (opt.header) { let headers: HeaderOptions[] = []; let i = 0; if (Array.isArray(opt.header)) { if (typeof opt.header[0] !== "string") { headers = opt.header as HeaderOptions[]; } else { const h = opt.header as string[]; headers = h.map( (e): HeaderOptions => { return { name: e }; } ); } } else { const head = r.shift(); assert(head != null); headers = head.map( (e): HeaderOptions => { return { name: e }; } ); i++; } return r.map((e): unknown => { if (e.length !== headers.length) { throw `Error number of fields line:${i}`; } i++; const 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 => { assert(opt.parse, "opt.parse must be set"); return opt.parse(e); }); } return r;}