-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export CSV separated with tab when copied to clipboard
- Loading branch information
Showing
6 changed files
with
145 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
export type CSVSeparator = ',' | '\t'; | ||
|
||
/** | ||
* Escape a CSV field value (see https://www.ietf.org/rfc/rfc4180.txt) | ||
* | ||
* - foo -> foo | ||
* - foo,bar -> "foo,bar" | ||
* - with "quoted" text -> "with ""quoted"" text" | ||
* | ||
* @param separator - Indicates the separator used in the CSV | ||
*/ | ||
export function escapeCSVValue(value: string): string { | ||
if (/[",\n\r]/.test(value)) { | ||
return `"${value.replace(/"/g, '""')}"`; | ||
} | ||
return value; | ||
export function escapeCSVValue(value: string, separator: CSVSeparator): string { | ||
const regexp = new RegExp(`["\n\r${separator}]`); | ||
return regexp.test(value) ? `"${value.replace(/"/g, '""')}"` : value; | ||
} |
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
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