csv-parser

repository·master·Indexed 23 days ago

https://github.com/mafintosh/csv-parser

A high-performance streaming CSV parser for Node.js that converts CSV data into JSON objects. It aims for maximum speed and compatibility with the csv-spectrum test suite. The package provides a Transform stream for programmatic use and a CLI for converting CSV to newline-delimited JSON (NDJSON).

Tokens
2.5K
Snippets
8
Records
17
Agent score
79%

What's inside csv-parser

  1. Handle Byte Order Marks (BOM)

    master

    If your CSV file contains a leading Byte Order Mark (BOM), it may interfere with header parsing. Use strip-bom-stream in your pipeline to remove it.

    const fs = require('fs');
    const csv = require('csv-parser');
    const stripBom = require('strip-bom-stream');
    
    fs.createReadStream('data.csv')
      .pipe(stripBom())
      .pipe(csv())
      ...
  2. Basic usage of csv-parser

    master

    To parse a CSV file, create a readable stream to the file, instantiate csv(), and pipe the stream into it. You can listen to the data event to process each row and the end event to know when parsing is complete.

    const csv = require('csv-parser')
    const fs = require('fs')
    const results = [];
    
    fs.createReadStream('data.csv')
      .pipe(csv())
      .on('data', (data) => results.push(data))
      .on('end', () => {
        console.log(results);
        // [ { NAME: 'Daffy Duck', AGE: '24' }, { NAME: 'Bugs Bunny', AGE: '22' } ]
      });
  3. Listen to the headers event

    master

    The headers event is emitted after the header row is parsed. The callback receives an Array[String] containing the header names.

    fs.createReadStream('data.csv')
      .pipe(csv())
      .on('headers', (headers) => {
        console.log(`First header: ${headers[0]}`)
      })
  4. Configure CsvParser options

    master

    When calling csv(opts), you can provide several configuration options to control parsing behavior:

    OptionTypeDefaultDescription
    headersArray or booleannullAn array of header names. If false, rows are returned as arrays of values indexed by number.
    mapHeadersFunction({ header }) => headerA function to transform header names. Receives { header, index }.
    mapValuesFunction({ value }) => valueA function to transform cell values. Receives { header, index, value }.
    separatorstring','The character used to separate cells.
    quotestring'"'The character used for quoting cells.
    escapestring(same as quote)The character used to escape quotes inside a quoted cell.
    newlinestring'\n'The character used for newlines.
    skipCommentsstring or booleanfalseIf a string, lines starting with this character are skipped. If true, defaults to #.
    skipLinesnumbernullThe number of lines to skip at the beginning of the file.
    strictbooleanfalseIf true, emits an error if a row's column count doesn't match the header count.
    rawbooleanfalseIf true, values are returned as Buffer objects instead of strings.
    maxRowBytesnumberNumber.MAX_SAFE_INTEGERMaximum allowed size for a single row in bytes.
    outputByteOffsetbooleanfalseIf true, each emitted object includes a byteOffset property.
  5. Configure csv-parser options

    master

    The csv() function accepts an options object with the following properties:

    • escape (String, default: "): Character used to escape strings.
    • headers (Array[String] | Boolean): Specifies headers. If false, uses column indices. If no headers are provided, the first line is used.
    • mapHeaders (Function): Modifies header values. Receives { header, index }.
    • mapValues (Function): Modifies column values. Receives { header, index, value }.
    • newline (String, default: \n): Character denoting the end of a line.
    • quote (String, default: "): Character denoting a quoted string.
    • raw (Boolean): If true, does not decode UTF-8 strings.
    • separator (String, default: ,): Column separator character.
    • skipComments (Boolean | String, default: false): If true, skips lines starting with #. If a string, uses that string as the comment prefix.
    • skipLines (Number, default: 0): Number of lines to skip at the start of the file before parsing headers.
    • maxRowBytes (Number, default: Number.MAX_SAFE_INTEGER): Max bytes per row before throwing an error.
    • strict (Boolean, default: false): If true, throws an error if row column count doesn't match headers.
    • outputByteOffset (Boolean, default: false): If true, emits rows as { byteOffset, row } where byteOffset is the start of the row in the stream.
  6. Use outputByteOffset to track position

    master

    If you need to know the exact byte position in the source stream where a specific row starts, enable the outputByteOffset option. When enabled, the data event will emit an object containing both the row and the byteOffset.

    const csv = require('csv-parser');
    const fs = require('fs');
    
    fs.createReadStream('data.csv')
      .pipe(csv({ outputByteOffset: true }))
      .on('data', ({ row, byteOffset }) => {
        console.log(`Row at ${byteOffset}:`, row);
      });