Skip to content

Commit

Permalink
Don't split rows on newlines within quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
David Mesquita-Morris committed Nov 21, 2021
1 parent 51ed99d commit 208980a
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports.parse = void 0;
function parse(text) {
// convert the csv formatted test into a table of tokens
const tokens = text.replace(/^\uFEFF|\r\n$|\n$|\r$/g, '') // trim byte order mark from beginning and trailing EOL if needed
.split(/\r\n|\n|\r/).map(row => // split text into rows at EOL
.split(/\r\n(?=(?:(?:[^"]*"){2})*[^"]*$)|\n(?=(?:(?:[^"]*"){2})*[^"]*$)|\r(?=(?:(?:[^"]*"){2})*[^"]*$)/).map(row => // split text into rows at EOL
row.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/) // split row into tokens based on comma delimiter (unless in quotes); see answer here: https://stackoverflow.com/questions/23582276/split-string-by-comma-but-ignore-commas-inside-quotes/23582323#23582323
.map(token => token.replace(/(^"|"$)/g, '') // dequote tokens if needed
.replace(/\"\"/g, '"'))); // replace double double quotes with double quotes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@steelbreeze/csv",
"version": "1.0.0-alpha",
"version": "1.0.0-alpha.1",
"description": "Tools for reading and writnig files formatted as CSV",
"main": "lib/node/index.js",
"module": "lib/node/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
export function parse(text: string): Array<any> {
// convert the csv formatted test into a table of tokens
const tokens = text.replace(/^\uFEFF|\r\n$|\n$|\r$/g, '') // trim byte order mark from beginning and trailing EOL if needed
.split(/\r\n|\n|\r/).map(row => // split text into rows at EOL
.split(/\r\n(?=(?:(?:[^"]*"){2})*[^"]*$)|\n(?=(?:(?:[^"]*"){2})*[^"]*$)|\r(?=(?:(?:[^"]*"){2})*[^"]*$)/).map(row => // split text into rows at EOL
row.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/) // split row into tokens based on comma delimiter (unless in quotes); see answer here: https://stackoverflow.com/questions/23582276/split-string-by-comma-but-ignore-commas-inside-quotes/23582323#23582323
.map(token => token.replace(/(^"|"$)/g, '') // dequote tokens if needed
.replace(/\"\"/g, '"'))); // replace double double quotes with double quotes
Expand Down

0 comments on commit 208980a

Please sign in to comment.