Skip to content
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

Add quotes if a field contains a carriage return \r even if it doesn't contain a line feed \n #57

Merged
merged 1 commit into from
Nov 14, 2020
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/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('"')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth unrolling these four substring searches into a single loop

}
}

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