-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle hash symbol and use
papaparse
to parse csv (#767)
- Loading branch information
1 parent
56fef60
commit 1d9dacb
Showing
7 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// escape the string containing commas and newlines by wrapping it in double quotes. | ||
// See: https://stackoverflow.com/a/4617967 | ||
export const escapeCSVFieldWithSpecialCharacters = (str: string) => { | ||
if (str.includes(',') || str.includes('\n')) { | ||
return `"${str}"` // wrap in double quotes | ||
} else { | ||
return str | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {escapeCSVFieldWithSpecialCharacters} from './escapeCSVFieldWithSpecialCharacters' | ||
|
||
describe('escapeSpecialCharacters', () => { | ||
it('should escape a string with commas', function() { | ||
const stringWithCommas = 'this is a string, with comma, and another one' | ||
|
||
expect(escapeCSVFieldWithSpecialCharacters(stringWithCommas)).toEqual( | ||
`"${stringWithCommas}"` | ||
) | ||
}) | ||
|
||
it('should escape a string with newLines', function() { | ||
const stringWithNewline = `this is a string | ||
with a newline | ||
and another one` | ||
|
||
expect(escapeCSVFieldWithSpecialCharacters(stringWithNewline)).toEqual( | ||
`"${stringWithNewline}"` | ||
) | ||
}) | ||
|
||
it('should escape a string with both commas and newLines', function() { | ||
const stringWithNewlineAndComma = `this is a string | ||
with a newline, a comma, | ||
and another newline and comma` | ||
|
||
expect( | ||
escapeCSVFieldWithSpecialCharacters(stringWithNewlineAndComma) | ||
).toEqual(`"${stringWithNewlineAndComma}"`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters