Skip to content

Commit

Permalink
Merge pull request #325 from C2FO/issue320
Browse files Browse the repository at this point in the history
Fix for #320
  • Loading branch information
doug-martin committed Feb 14, 2020
2 parents 6569c8b + 2a7e5d5 commit b4ec62c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* [FIXED] Issue where invalid rows were not accounted for when skipRows was set [#317](https://github.com/C2FO/fast-csv/issues/317)
* [FIXED] Issue where readableObjectMode was not set to false when formatting [#319](https://github.com/C2FO/fast-csv/issues/319)
* [FIXED] Issue where carriage returns and line feeds were not always quoted when formatting [#320](https://github.com/C2FO/fast-csv/issues/320)

# v4.0.2

Expand Down
18 changes: 18 additions & 0 deletions packages/format/__tests__/formatter/FieldFormatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ describe('FieldFormatter', () => {
expect(formatter.format('hea\r\nder', 0, true)).toEqual('"hea\r\nder"');
});

it('should quote the field and if it contains a CR', () => {
// set row delimiter to something else to ensure it will still quote
const formatter = createFormatter({ rowDelimiter: '#' });
expect(formatter.format('hea\rder', 0, true)).toEqual('"hea\rder"');
});

it('should quote the field and if it contains a LF', () => {
// set row delimiter to something else to ensure it will still quote
const formatter = createFormatter({ rowDelimiter: '#' });
expect(formatter.format('hea\nder', 0, true)).toEqual('"hea\nder"');
});

it('should quote the field and if it contains a CRLF', () => {
// set row delimiter to something else to ensure it will still quote
const formatter = createFormatter({ rowDelimiter: '#' });
expect(formatter.format('hea\r\nder', 0, true)).toEqual('"hea\r\nder"');
});

it('should quote the field if quoteHeaders is true', () => {
const formatter = createFormatter({ quoteHeaders: true });
expect(formatter.format('header', 0, true)).toEqual('"header"');
Expand Down
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)}']`;
const escapePattern = `[${formatterOptions.delimiter}${escapeRegExp(formatterOptions.rowDelimiter)}|\r|\n']`;
this.ESCAPE_REGEXP = new RegExp(escapePattern);
}

Expand Down

0 comments on commit b4ec62c

Please sign in to comment.