Skip to content

fix: comma starting a wrapped value broke the line #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/csv2json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export const Csv2Json = function (options: FullCsv2JsonOptions) {
stateVariables.insideWrapDelimiter = true;
stateVariables.parsingValue = true;
stateVariables.startIndex = index;
} else if (character === options.delimiter.wrap && charAfter === options.delimiter.field) {
} else if (character === options.delimiter.wrap && charAfter === options.delimiter.field && stateVariables.insideWrapDelimiter) {
// If we reached a wrap delimiter with a field delimiter after it (ie. *",)

splitLine.push(csv.substring(stateVariables.startIndex, index + 1));
Expand Down
1 change: 1 addition & 0 deletions test/config/testCsvFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const csvFileConfig = [
{ key: 'nested', file: '../data/csv/nested.csv' },
{ key: 'nestedMissingField', file: '../data/csv/nestedMissingField.csv' },
{ key: 'comma', file: '../data/csv/comma.csv' },
{ key: 'commaAfterOpeningWrap', file: '../data/csv/commaAfterOpeningWrap.csv' },
{ key: 'quotes', file: '../data/csv/quotes.csv' },
{ key: 'quotesHeader', file: '../data/csv/quotesHeader.csv' },
{ key: 'quotesAndCommas', file: '../data/csv/quotesAndCommas.csv' },
Expand Down
1 change: 1 addition & 0 deletions test/config/testJsonFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default {
nested: require('../data/json/nested'),
nestedMissingField: require('../data/json/nestedMissingField'),
comma: require('../data/json/comma'),
commaAfterOpeningWrap: require('../data/json/commaAfterOpeningWrap'),
quotes: require('../data/json/quotes'),
quotesHeader: require('../data/json/quotesHeader'),
quotesAndCommas: require('../data/json/quotesAndCommas'),
Expand Down
5 changes: 5 additions & 0 deletions test/csv2json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export function runTests() {
assert.deepEqual(json, jsonTestData.comma);
});

it('should convert csv containing a field with a comma after an opening wrap to json', () => {
const json = csv2json(csvTestData.commaAfterOpeningWrap);
assert.deepEqual(json, jsonTestData.commaAfterOpeningWrap);
});

it('should convert csv containing a field with quotes to json', () => {
const json = csv2json(csvTestData.quotes);
assert.deepEqual(json, jsonTestData.quotes);
Expand Down
3 changes: 3 additions & 0 deletions test/data/csv/commaAfterOpeningWrap.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
username,comment
mrodrig,",In other words, this is cool!"
jsmith,"This module is really, really helpful!"
10 changes: 10 additions & 0 deletions test/data/json/commaAfterOpeningWrap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"username": "mrodrig",
"comment": ",In other words, this is cool!"
},
{
"username": "jsmith",
"comment": "This module is really, really helpful!"
}
]
Loading