CSVParse for Node.js

IssuesGitHub

Option trim

The trim option ignore whitespace characters immediately around the delimiter. Defaults to false. It does not remove whitespace present inside the quotes of a field.

  • Type: boolean
  • Optional
  • Default: false
  • Since: early days
  • Related: ltrim, rtrim — see Available Options

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

  • Horizontal tab, String.fromCharCode(9)
  • NL line feed, new line, String.fromCharCode(10)
  • NP Form feed, new page, String.fromCharCode(12)
  • Carriage return, String.fromCharCode(13)
  • Space, String.fromCharCode(32)

Example

This example insert spaces around fields at multiple locations.

import assert from 'node:assert';
import { parse } from 'csv-parse/sync';

const records = parse('a ,1\nb, 2\n c,3', {
  trim: true
});
assert.deepStrictEqual(
  records, [
    [ 'a', '1' ],
    [ 'b', '2' ],
    [ 'c', '3' ]
  ]
);

About

The Node.js CSV project is an open source product hosted on GitHub and developed by Adaltas.