Skip to content

Commit

Permalink
fix(formatting,#446): Do not quote fields that do not contain a quote
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Nov 3, 2020
1 parent 6969d3e commit 13e688c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/format/__tests__/issues/issue446.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as csv from '../../src';
import { RecordingStream } from '../__fixtures__';

describe('Issue #446 - https://github.com/C2FO/fast-csv/issues/446', () => {
it('should not quote a field that contains a single quote if it is not the quote character', () => {
return new Promise((res, rej) => {
const rs = new RecordingStream();
const data: csv.RowArray[] = [["a quick' brown fox", 'jumped', 'over the lazy brown "dog"']];

csv.write(data, {
headers: ['header1', 'header2', 'header3'],
})
.pipe(rs)
.on('error', rej)
.on('finish', () => {
expect(rs.data).toEqual([
'header1,header2,header3',
`\na quick' brown fox,jumped,"over the lazy brown ""dog"""`,
]);
res();
});
});
});
});
2 changes: 1 addition & 1 deletion packages/format/src/formatter/FieldFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class FieldFormatter<I extends Row, O extends Row> {
this.headers = formatterOptions.headers;
}
this.REPLACE_REGEXP = new RegExp(formatterOptions.quote, 'g');
const escapePattern = `[${formatterOptions.delimiter}${escapeRegExp(formatterOptions.rowDelimiter)}|\r|\n']`;
const escapePattern = `[${formatterOptions.delimiter}${escapeRegExp(formatterOptions.rowDelimiter)}|\r|\n]`;
this.ESCAPE_REGEXP = new RegExp(escapePattern);
}

Expand Down

0 comments on commit 13e688c

Please sign in to comment.