Skip to content

Commit 1b75a88

Browse files
authored
Merge pull request #271 from sevrai/fix/field-after-wrap
fix: comma starting a wrapped value broke the line
2 parents ffada90 + 3a8ce35 commit 1b75a88

File tree

6 files changed

+21
-1
lines changed

6 files changed

+21
-1
lines changed

src/csv2json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export const Csv2Json = function (options: FullCsv2JsonOptions) {
190190
stateVariables.insideWrapDelimiter = true;
191191
stateVariables.parsingValue = true;
192192
stateVariables.startIndex = index;
193-
} else if (character === options.delimiter.wrap && charAfter === options.delimiter.field) {
193+
} else if (character === options.delimiter.wrap && charAfter === options.delimiter.field && stateVariables.insideWrapDelimiter) {
194194
// If we reached a wrap delimiter with a field delimiter after it (ie. *",)
195195

196196
splitLine.push(csv.substring(stateVariables.startIndex, index + 1));

test/config/testCsvFilesList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const csvFileConfig = [
1414
{ key: 'nested', file: '../data/csv/nested.csv' },
1515
{ key: 'nestedMissingField', file: '../data/csv/nestedMissingField.csv' },
1616
{ key: 'comma', file: '../data/csv/comma.csv' },
17+
{ key: 'commaAfterOpeningWrap', file: '../data/csv/commaAfterOpeningWrap.csv' },
1718
{ key: 'quotes', file: '../data/csv/quotes.csv' },
1819
{ key: 'quotesHeader', file: '../data/csv/quotesHeader.csv' },
1920
{ key: 'quotesAndCommas', file: '../data/csv/quotesAndCommas.csv' },

test/config/testJsonFilesList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default {
1515
nested: require('../data/json/nested'),
1616
nestedMissingField: require('../data/json/nestedMissingField'),
1717
comma: require('../data/json/comma'),
18+
commaAfterOpeningWrap: require('../data/json/commaAfterOpeningWrap'),
1819
quotes: require('../data/json/quotes'),
1920
quotesHeader: require('../data/json/quotesHeader'),
2021
quotesAndCommas: require('../data/json/quotesAndCommas'),

test/csv2json.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ export function runTests() {
7171
assert.deepEqual(json, jsonTestData.comma);
7272
});
7373

74+
it('should convert csv containing a field with a comma after an opening wrap to json', () => {
75+
const json = csv2json(csvTestData.commaAfterOpeningWrap);
76+
assert.deepEqual(json, jsonTestData.commaAfterOpeningWrap);
77+
});
78+
7479
it('should convert csv containing a field with quotes to json', () => {
7580
const json = csv2json(csvTestData.quotes);
7681
assert.deepEqual(json, jsonTestData.quotes);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
username,comment
2+
mrodrig,",In other words, this is cool!"
3+
jsmith,"This module is really, really helpful!"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"username": "mrodrig",
4+
"comment": ",In other words, this is cool!"
5+
},
6+
{
7+
"username": "jsmith",
8+
"comment": "This module is really, really helpful!"
9+
}
10+
]

0 commit comments

Comments
 (0)