CSVParse for Node.js

IssuesGitHub

Option ignore_last_delimiters

Table of Contents

The ignore_last_delimiters option disregard any delimiters present in the last field of the record. It requires the presence of the column option to know how many fields are expected when it is defined as true.

Some formats which claim to be partially compatible with csv formats assume that it's ok to have unescaped commas in the last field because the number of fields was registered when the header line was parsed.

For example, Advanced SubStation Alpha (ASS), technically SSA v4+, is a subtitle file format. You can see such behavior in its specification:

The format line specifies how SSA will interpret all following Event lines. The field names must be spelled correctly, and are as follows: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text. The last field will always be the Text field, so that it can contain commas.

and here:

The information fields in each line are separated by a commas. This makes it illegal to use commas in character names and style names (SSA prevents you putting commas in these). It also makes it quite easy to load chunks of an SSA script into a spreadsheet as a CSV file, and chop out columns of information you need for another subtitling program.

Example

In this example, the CSV data is made of 2 fields, format and description. Fields are separated by commans, the default delimiter. The last field, description, can contains any number of commas without breaking the record.

const parse = require('csv-parse')
const assert = require('assert')

parse(`
format,description
CSV,CSV delimited text file that uses a comma, by default, to separate values.
SSA,SSA is a subtitle file format that allows for more advanced subtitles than the conventional SRT and similar formats.
ASS,Advanced SubStation Alpha (ASS), technically SSA v4+, is a script for more advanced subtitles than SSA.
`.trim(), {
  columns: true,
  ignore_last_delimiters: 10
}, function(err, records){
  assert.deepEqual(records, [{
      format: 'CSV',
      description: 'CSV delimited text file that uses a comma, by default, to separate values.'
    },{
      format: 'SSA',
      description: 'SSA is a subtitle file format that allows for more advanced subtitles than the conventional SRT and similar formats.'
    },{
      format: 'ASS',
      description: 'Advanced SubStation Alpha (ASS), technically SSA v4+, is a script for more advanced subtitles than SSA.'
    }
  ])
})