From cc6e4653335e733d783d87237ce7ba3f43139b6c Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Thu, 27 Jun 2024 13:46:09 +0900 Subject: [PATCH] chore: Revert "refactor(csv): minor cleanups (#5158)" (#5163) --- csv/_io.ts | 5 +++-- csv/csv_stringify_stream.ts | 25 ++++++++++++++++++------- csv/parse.ts | 29 +++++++++++++++-------------- csv/stringify.ts | 11 +++-------- 4 files changed, 39 insertions(+), 31 deletions(-) diff --git a/csv/_io.ts b/csv/_io.ts index 43fcf2202..2a6742edf 100644 --- a/csv/_io.ts +++ b/csv/_io.ts @@ -79,7 +79,8 @@ export async function parseRecord( const separatorLen = opt.separator.length; let recordBuffer = ""; const fieldIndexes = [] as number[]; - parseField: while (true) { + parseField: + for (;;) { if (opt.trimLeadingSpace) { line = line.trimStart(); } @@ -117,7 +118,7 @@ export async function parseRecord( } else { // Quoted string field line = line.substring(quoteLen); - while (true) { + for (;;) { const i = line.indexOf(quote); if (i >= 0) { // Hit next quote. diff --git a/csv/csv_stringify_stream.ts b/csv/csv_stringify_stream.ts index 9ce76a0fc..c327ee8af 100644 --- a/csv/csv_stringify_stream.ts +++ b/csv/csv_stringify_stream.ts @@ -74,21 +74,32 @@ export class CsvStringifyStream * @param options Options for the stream. */ constructor(options?: TOptions) { - const { separator, columns = [] } = options ?? {}; + const { + separator, + columns = [], + } = options ?? {}; super( { start(controller) { if (columns && columns.length > 0) { - controller.enqueue( - stringify([columns], { separator, headers: false }), - ); + try { + controller.enqueue( + stringify([columns], { separator, headers: false }), + ); + } catch (error) { + controller.error(error); + } } }, transform(chunk, controller) { - controller.enqueue( - stringify([chunk], { separator, headers: false, columns }), - ); + try { + controller.enqueue( + stringify([chunk], { separator, headers: false, columns }), + ); + } catch (error) { + controller.error(error); + } }, }, ); diff --git a/csv/parse.ts b/csv/parse.ts index c30517f70..ca29437fe 100644 --- a/csv/parse.ts +++ b/csv/parse.ts @@ -103,7 +103,8 @@ class Parser { const separatorLen = this.#options.separator.length; let recordBuffer = ""; const fieldIndexes = [] as number[]; - parseField: while (true) { + parseField: + for (;;) { if (this.#options.trimLeadingSpace) { line = line.trimStart(); } @@ -141,7 +142,7 @@ class Parser { } else { // Quoted string field line = line.substring(quoteLen); - while (true) { + for (;;) { const i = line.indexOf(quote); if (i >= 0) { // Hit next quote. @@ -249,7 +250,7 @@ class Parser { throw new Error(ERR_INVALID_DELIM); } - while (true) { + for (;;) { const r = this.#parseRecord(lineIndex); if (r === null) break; lineResult = r; @@ -328,35 +329,35 @@ export function parse(input: string): string[][]; * * @typeParam T The options' type for parsing. * @param input The input to parse. - * @param options The options for parsing. - * @returns If you don't provide `options.skipFirstRow` and `options.columns`, it returns `string[][]`. - * If you provide `options.skipFirstRow` or `options.columns`, it returns `Record[]`. + * @param opt The options for parsing. + * @returns If you don't provide `opt.skipFirstRow` and `opt.columns`, it returns `string[][]`. + * If you provide `opt.skipFirstRow` or `opt.columns`, it returns `Record[]`. */ export function parse( input: string, - options: T, + opt: T, ): ParseResult; export function parse( input: string, - options: T = { skipFirstRow: false } as T, + opt: T = { skipFirstRow: false } as T, ): ParseResult { - const parser = new Parser(options); + const parser = new Parser(opt); const r = parser.parse(input); - if (options.skipFirstRow || options.columns) { + if (opt.skipFirstRow || opt.columns) { let headers: readonly string[] = []; - if (options.skipFirstRow) { + if (opt.skipFirstRow) { const head = r.shift(); if (head === undefined) throw new TypeError("Headers must be defined"); headers = head; } - if (options.columns) { - headers = options.columns; + if (opt.columns) { + headers = opt.columns; } - const firstLineIndex = options.skipFirstRow ? 1 : 0; + const firstLineIndex = opt.skipFirstRow ? 1 : 0; return r.map((row, i) => { return convertRowToObject(row, headers, firstLineIndex + i); }) as ParseResult; diff --git a/csv/stringify.ts b/csv/stringify.ts index a3ec99fb3..705533f3d 100644 --- a/csv/stringify.ts +++ b/csv/stringify.ts @@ -104,11 +104,9 @@ export type StringifyOptions = { */ separator?: string; /** - * A list of instructions for how to target and transform the data for each + * a list of instructions for how to target and transform the data for each * column of output. This is also where you can provide an explicit header * name for the column. - * - * @default {[]} */ columns?: Column[]; /** @@ -297,16 +295,13 @@ function getValuesFromItem( * * @param data The source data to stringify. It's an array of items which are * plain objects or arrays. - * @param options Options for the stringification. * @returns A CSV string. */ export function stringify( data: DataItem[], - options?: StringifyOptions, + { headers = true, separator: sep = ",", columns = [], bom = false }: + StringifyOptions = {}, ): string { - const { headers = true, separator: sep = ",", columns = [], bom = false } = - options ?? {}; - if (sep.includes(QUOTE) || sep.includes(CRLF)) { const message = [ "Separator cannot include the following strings:",