Skip to content

Commit

Permalink
Alpha 3
Browse files Browse the repository at this point in the history
  • Loading branch information
David Mesquita-Morris committed Nov 24, 2021
1 parent 9dc6022 commit c59c643
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 16 deletions.
16 changes: 10 additions & 6 deletions lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ exports.parse = void 0;
* @returns An array of objects.
*/
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(?=(?:(?:[^"]*"){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
// trim byte order mark from beginning and any trailing EOL if present
const tokens = text.replace(/^\uFEFF|[\r\n]+$/, '')
// split text into rows at EOL
.split(/[\r\n]+(?=(?:(?:[^"]*"){2})*[^"]*$)/).map(row =>
// 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
row.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/)
// dequote tokens if needed
.map(token => token.replace(/(^"|"$)/g, '')
// replace double double quotes with double quotes
.replace(/\"\"/g, '"')));
// extract the header row and use for the property names
const header = tokens.shift();
// convert subsiquent rows into objects
Expand Down
2 changes: 1 addition & 1 deletion lib/node/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ var __importStar = (this && this.__importStar) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const csv = __importStar(require(".."));
const data = '\uFEFFa,b,c\r"1",2,3\n4,"5",6\r\n7,"The number eight: ""8""",9\r"a\r\na",b,"c, d"\n12';
const data = 'a,b,c\r"1",2,3\n4,"5",6\r\n7,"The number eight: ""8""",9\r"a\r\na",b,"c, d"\n12';
console.log(csv.parse(data));
2 changes: 1 addition & 1 deletion lib/web/csv.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.2",
"version": "1.0.0-alpha.3",
"description": "Tools for reading and writnig files formatted as CSV",
"main": "lib/node/index.js",
"module": "lib/node/index.js",
Expand Down
16 changes: 10 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
* @returns An array of objects.
*/
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(?=(?:(?:[^"]*"){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
// trim byte order mark from beginning and any trailing EOL if present
const tokens = text.replace(/^\uFEFF|[\r\n]+$/, '')
// split text into rows at EOL (unless in quotes)
.split(/\r?\n(?=(?:(?:[^"]*"){2})*[^"]*$)|\r(?=(?:(?:[^"]*"){2})*[^"]*$)/).map(row =>
// split row into tokens based on comma delimiter (unless in quotes)
row.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/)
// dequote tokens if needed
.map(token => token.replace(/(^"|"$)/g, '')
// replace double double quotes with double quotes
.replace(/\"\"/g, '"')));

// extract the header row and use for the property names
const header = tokens.shift();
Expand Down
3 changes: 2 additions & 1 deletion src/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as csv from '..';

const data = '\uFEFFa,b,c\r"1",2,3\n4,"5",6\r\n7,"The number eight: ""8""",9\r"a\r\na",b,"c, d"\n12';
const data = 'a,b,c\r"1",2,3\n4,"5",6\r\n7,"The number eight: ""8""",9\r"a\r\na",b,"c, d"\n12';

console.log(csv.parse(data));

0 comments on commit c59c643

Please sign in to comment.