Commit 2b92198d authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Extract escapeCSVValue util to its own module

parent 96a89f3f
/**
* 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"
*/
export function escapeCSVValue(value: string): string {
if (/[",\n\r]/.test(value)) {
return `"${value.replace(/"/g, '""')}"`;
}
return value;
}
import { escapeCSVValue } from '../csv';
describe('escapeCSVValue', () => {
[
{ value: 'foo', expected: 'foo' },
{ value: 'foo,bar', expected: '"foo,bar"' },
{ value: 'with \r carriage return', expected: '"with \r carriage return"' },
{
value: `multiple
lines`,
expected: `"multiple
lines"`,
},
{ value: 'with "quotes"', expected: '"with ""quotes"""' },
].forEach(({ value, expected }) => {
it('escapes values', () => {
assert.equal(escapeCSVValue(value), expected);
});
});
});
import { escapeCSVValue } from '../../shared/csv';
import { trimAndDedent } from '../../shared/trim-and-dedent'; import { trimAndDedent } from '../../shared/trim-and-dedent';
import type { APIAnnotationData, Profile } from '../../types/api'; import type { APIAnnotationData, Profile } from '../../types/api';
import { import {
...@@ -117,14 +118,6 @@ export class AnnotationsExporter { ...@@ -117,14 +118,6 @@ export class AnnotationsExporter {
defaultAuthority, defaultAuthority,
}); });
const escapeCSVValue = (value: string): string => {
// If the value contains a comma, newline or double quote, then wrap it in
// double quotes and escape any existing double quotes.
if (/[",\n]/.test(value)) {
return `"${value.replace(/"/g, '""')}"`;
}
return value;
};
const annotationToRow = (annotation: APIAnnotationData) => const annotationToRow = (annotation: APIAnnotationData) =>
[ [
annotation.created, annotation.created,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment