fast-csv icon indicating copy to clipboard operation
fast-csv copied to clipboard

[BUG] The `|` character causes unnecessary quotation of the value

Open pepkin88 opened this issue 3 years ago • 1 comments

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.

pepkin88 avatar Dec 02 '22 14:12 pepkin88

Any Solution so far achieved yet? I need a quick fix to overcome this.

haddyo avatar Feb 13 '23 11:02 haddyo