Skip to content

Commit

Permalink
Merge pull request #57 from mvaneerde/carriage-return
Browse files Browse the repository at this point in the history
Add quotes if a field contains a carriage return \r even if it doesn't contain a line feed \n
  • Loading branch information
ryu1kn committed Nov 14, 2020
2 parents c635d2b + d71f3f7 commit 907e616
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/field-stringifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DefaultFieldStringifier extends FieldStringifier {
}

private needsQuote(str: string): boolean {
return str.includes(this.fieldDelimiter) || str.includes('\n') || str.includes('"')
return str.includes(this.fieldDelimiter) || str.includes('\r') || str.includes('\n') || str.includes('"')
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/test/field-stringifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ describe('DefaultFieldStringifier', () => {
strictEqual(stringifier.stringify(`VALUE${delim}A`), `"VALUE${delim}A"`)
})

it('wraps a field value with double quotes if the field contains newline', () => {
it('wraps a field value with double quotes if the field contains carriage return', () => {
strictEqual(stringifier.stringify('VALUE\rA'), '"VALUE\rA"')
})

it('wraps a field value with double quotes if the field contains line feed', () => {
strictEqual(stringifier.stringify('VALUE\nA'), '"VALUE\nA"')
})

it('wraps a field value with double quotes if the field contains carriage return and line feed', () => {
strictEqual(stringifier.stringify('VALUE\r\nA'), '"VALUE\r\nA"')
})

it('wraps a field value with double quotes and escape the double quotes if they are used in the field', () => {
strictEqual(stringifier.stringify('VALUE"A'), '"VALUE""A"')
})
Expand Down

0 comments on commit 907e616

Please sign in to comment.