Skip to content

Commit

Permalink
Fix issue with quotes followed by word boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
donatj committed Oct 10, 2022
1 parent 24f8cf0 commit 93516de
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/CsvToMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function csvToMarkdown(csvContent, delimiter, hasHeader) {
if (typeof tabularData[i] == "undefined") {
tabularData[i] = [];
}
var regex = new RegExp(delimiter + '(?![^"]*"\\B)');
var regex = new RegExp(delimiter + "(?![^\"]*\"(?:$|" + delimiter + "))");
var row = e.split(regex);
row.forEach(function (ee, ii) {
if (typeof maxRowLen[ii] == "undefined") {
Expand Down
2 changes: 1 addition & 1 deletion src/CsvToMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function csvToMarkdown(csvContent: string, delimiter: string = "\t", hasH
if (typeof tabularData[i] == "undefined") {
tabularData[i] = [];
}
const regex = new RegExp(delimiter + '(?![^"]*"\\B)');
const regex = new RegExp(`${delimiter}(?![^"]*"(?:$|${delimiter}))`);
const row = e.split(regex);
row.forEach((ee, ii) => {
if (typeof maxRowLen[ii] == "undefined") {
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,14 @@ describe('csvToMarkdown', function () {
var result = csvToMarkdown('"a|b|c|d",e\\f\\g', ",", false);
assert.strictEqual(result, '| | | \n|--------------|---------| \n| "a\\|b\\|c\\|d" | e\\\\f\\\\g | \n');
});

it('should handle single values ending in the delimiter', function(){
var result = csvToMarkdown('"assd;"', ";", false);
assert.strictEqual(result, '| | \n|---------| \n| "assd;" | \n');
});

it('should handle items that begin with word boundaries', function(){
var result = csvToMarkdown('"foo";"bar";"baz"\n"1";"2";"[foo -;- bar baz]"', ";", true);
assert.strictEqual(result, '| "foo" | "bar" | "baz" | \n|-------|-------|---------------------| \n| "1" | "2" | "[foo -;- bar baz]" | \n');
});
});

0 comments on commit 93516de

Please sign in to comment.