Skip to content

Commit

Permalink
refactor(csv): update parseLine()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 18, 2020
1 parent 9d682a0 commit 4ae49e6
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions packages/csv/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const DEFAULT_OPTS: Partial<CSVOpts> = {

/**
* Configurable CSV parsing transducer, operating on line-based string iterables
* and yielding tuple objects of CSV records. If called with input, returns ES6
* iterator instead.
* and yielding tuple objects of CSV row records. If called with input, returns
* ES6 iterator instead.
*
* @remarks
* Parsing behavior can be customized via given {@link CSVOpts}. The default
Expand All @@ -39,7 +39,10 @@ const DEFAULT_OPTS: Partial<CSVOpts> = {
* coerced/transformed. Additionally, if `all` option is `false`, then the
* result objects will only contain values of the columns specified in `cols`.
*
* Also see {@link parseCSVString}.
* Also see:
* - thi.ng/transducers
* - {@link CSVOpts}
* - {@link parseCSVString}.
*
* @example
* ```ts
Expand Down Expand Up @@ -185,10 +188,8 @@ export const parseCSVString = (opts: Partial<CSVOpts>, src: string) =>
* @param isQuoted
* @param delim
* @param quote
*
* @internal
*/
export const parseLine = (
const parseLine = (
line: string,
acc: string[],
isQuoted: boolean,
Expand Down Expand Up @@ -218,8 +219,7 @@ export const parseLine = (
}
// field delimiter
else if (!isQuoted && c === delim) {
if (openQuote) acc[acc.length - 1] += "\n" + curr;
else acc.push(curr);
collectCell(acc, curr, openQuote);
openQuote = false;
curr = "";
}
Expand All @@ -229,13 +229,13 @@ export const parseLine = (
}
p = c;
}
if (curr !== "") {
if (openQuote) acc[acc.length - 1] += "\n" + curr;
else acc.push(curr);
}
curr !== "" && collectCell(acc, curr, openQuote);
return isQuoted;
};

const collectCell = (acc: string[], curr: string, openQuote: boolean) =>
openQuote ? (acc[acc.length - 1] += "\n" + curr) : acc.push(curr);

const initIndex = (
line: string[],
cols: Nullable<ColumnSpec>[] | Record<string, ColumnSpec>
Expand Down

0 comments on commit 4ae49e6

Please sign in to comment.