CSVStringify for Node.js

IssuesGitHub

Option quote_record_delimiter

The quote_record_delimiter option controls whether fields containing \r (carriage return) or \n (line feed) are automatically quoted, beyond the configured record_delimiter characters which are always quoted.

When record_delimiter is not explicitly configured, quote_record_delimiter defaults to true. This preserves round-trip safety: while stringify only treats \n as a record separator by default, parse treats all three sequences \r, \n, and \r\n as record boundaries. An unquoted \r inside a field would therefore be misread as a record separator on the next parse.

When record_delimiter is explicitly set, quote_record_delimiter defaults to false. The assumption is that the user has taken control of their format, so the extra protection for \r and \n is not applied automatically. Only fields containing the configured record_delimiter characters are quoted.

Default behavior

With the default configuration (no record_delimiter set), only a field containing a carriage return \r or a line feed \n is quoted.

import { stringify } from "csv-stringify/sync";
import assert from "node:assert";

// A carriage return inside a field is quoted by default
const records = stringify([["a\rb"], ["c\nd"], ["e::f"]], { eof: false });

assert.equal(records, '"a\rb"\n"c\nd"\ne::f');

With a custom record_delimiter

When record_delimiter is explicitly configured, quote_record_delimiter defaults to false. Fields containing \r or \n are not quoted unless those characters are part of the configured delimiter.

import { stringify } from "csv-stringify/sync";
import assert from "node:assert";

// When record_delimiter is set, quote_record_delimiter defaults to false.
// A carriage return is not quoted because it does not match the custom delimiter.
const cr = stringify([["a\rb"]], {
  record_delimiter: "::",
  eof: false,
});
assert.equal(cr, "a\rb");

// The custom delimiter itself is always quoted.
const delim = stringify([["a::b"]], {
  record_delimiter: "::",
  eof: false,
});
assert.equal(delim, '"a::b"');

Disabling the option

When quote_record_delimiter is explicitly set to false with the default record_delimiter, a carriage return \r is not quoted. A line feed \n is still quoted because it matches the default record_delimiter.

import { stringify } from "csv-stringify/sync";
import assert from "node:assert";

// With quote_record_delimiter disabled, a carriage return is not quoted
const cr = stringify([["a\rb"]], {
  quote_record_delimiter: false,
  eof: false,
});
assert.equal(cr, "a\rb");

// A line feed is still quoted because it matches the default record_delimiter
const lf = stringify([["a\nb"]], {
  quote_record_delimiter: false,
  eof: false,
});
assert.equal(lf, '"a\nb"');

About

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