Option trim

Table of Contents

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.

const parse = require('csv-parse/lib/sync')
const assert = require('assert')

const records = parse(`
a ,1
b, 2
 c,3
`, {
  trim: true
})
assert.deepEqual(
  records, [
    [ 'a', '1' ],
    [ 'b', '2' ],
    [ 'c', '3' ]
  ]
)