CSVParse for Node.js

IssuesGitHub

Option to_line

The to_line option handles records until a requested line number is reached. It is disabled by default with the value -1.

  • Type: number
  • Coercion: string to number
  • Optional
  • Default: -1
  • Validation: positive integer
  • Since: 4.0.0
  • Related: from_line, from, to — see Available Options

The line matching the value is parsed and the record is including the output. If the record does not end on the line, for example when the new line character is escaped or quoted, it will be disregarded.

Example

This example return the first two lines and skip all the following records.

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

const records = parse(`
a,1
b,1
x,x
`.trim(), {
  to_line: 2
});
assert.deepStrictEqual(
  records, [
    [ 'a', '1' ],
    [ 'b', '1' ]
  ]
);

About

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