CSVStringify for Node.js

IssuesGitHub

Option quoted_match

Quote all fields matching a regular expression. Value may equal a string, a RegExp, and an array with any of those.

Note, several options are available to control when to quote fields under certain conditions. Make sure to review the alternatives.

Example with a string

In the quoted_match_string example, fields containing the string "." are quoted.

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

stringify(
  [["a value", ".", "value.with.dot"]],
  {
    quoted_match: ".",
  },
  function (err, records) {
    assert.equal(records, 'a value,".","value.with.dot"\n');
  },
);

Example with a regular expression

In the quoted_match_regexp example, fields matching the regular expression /\./ are quoted.

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

stringify(
  [["a value", ".", "value.with.dot"]],
  {
    quoted_match: /\./,
  },
  function (err, records) {
    assert.equal(records, 'a value,".","value.with.dot"\n');
  },
);

About

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