Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

trim() doesn't remove all whitespace characters #304

Open
sedenardi opened this issue Dec 21, 2020 · 1 comment
Open

trim() doesn't remove all whitespace characters #304

sedenardi opened this issue Dec 21, 2020 · 1 comment
Labels

Comments

@sedenardi
Copy link

Describe the bug

In the documentation for trim(), it's mentioned that the function will trim all whitespace around delimiters identically to \s:

The characters interpreted as whitespaces are identical to the \s meta character in regular expressions

However, in practice, and inspecting the source of __isCharTrimable(), I can see that it only is trimming the 5 examples characters from the docs, rather than the full list of characters that \s actually matches on. In the MDN Javascript docs, as well as in practice (in the example below in NodeJS), some whitespace characters should be expected to be matched and removed, but aren't.

To Reproduce

This example shows that the values containing whitespace characters not listed in the trim() docs aren't getting removed, but they are when using the regex \s.

const csvParse = require('csv-parse');

/**
 * First whitespace is 'IDEOGRAPHIC SPACE' (U+3000)
 * Second whitespace is 'HAIR SPACE' (U+200A)
 */
const input = `ID,First,Last
1234, John,Smith
5678,Jane, Doe`;

csvParse(input, {
  columns: true,
  trim: true
}, (err, res) => {
  if (err) { throw err; }
  console.log(res);
  const trimmed = res.map((r) => {
    return {
      ...r,
      First: r.First.replace(/^\s+|\s+$/g, ''),
      Last: r.Last.replace(/^\s+|\s+$/g, '')
    };
  });
  console.log(trimmed);
});

Additional context

This isn't a case where the code is necessarily broken or incorrect, as it's correctly matching the 5 whitespace characters mentioned in the docs. However, the line in the docs

identical to the \s meta character in regular expressions

incorrectly describes what the output will be.

@sedenardi sedenardi added the bug label Dec 21, 2020
@gillyspy
Copy link

this was affecting me as well. I resolved it with my own trim but then found this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants