[BUG] The `|` character causes unnecessary quotation of the value
Describe the bug
If the cell value includes the | character, the cell is unnecessarily quoted.
Parsing or Formatting?
- [x] Formatting
- [ ] Parsing
To Reproduce
import { format } from '@fast-csv/format';
const stream = format();
stream.pipe(process.stdout);
stream.write(['|']);
stream.write(['a']);
stream.end();
Expected behavior Printed in the console:
|
a
What I get instead is:
"|"
a
Desktop (please complete the following information):
- OS: macOS
- OS Version: Ventura
- Node Version: 18.6.0
Additional context
The reason for such behavior:
In the file packages/format/src/formatter/FieldFormatter.ts there is a line:
const escapePattern = `[${formatterOptions.delimiter}${escapeRegExp(formatterOptions.rowDelimiter)}|\r|\n]`;
The problem is with the ending characters of the regexp: |\r|\n]. Because it is inside the square brackets [], the | characters don't have the meaning of alternation, but instead are treated as a single character in a character set.
The resulting regexp from the default settings would be /[,\n|\r|\n]/, which would match an occurrence of any of the following characters: ,, \n, |, \r.
So to fix it, it should be enough to just delete those | characters from the set, as they are not needed there:
const escapePattern = `[${formatterOptions.delimiter}${escapeRegExp(formatterOptions.rowDelimiter)}\r\n]`;
I wanted to post this as a pull request, but I failed at building the project and running the tests. There are some errors, but honestly, I don't have the time or maybe even competence to track and fix them. I hope this issue is good enough.
Any Solution so far achieved yet? I need a quick fix to overcome this.