Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/csv-stringify/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,14 @@ const stringifier = function (options, state, info) {
quote_record_delimiter,
escape_formulas,
} = options;
if ("" === value && "" === field) {
if ("" === value) {
const quotedMatch = matches_quoted_match(value, quoted_match);
const shouldQuote =
quotedMatch ||
true === quoted_empty ||
(true === quoted_string && false !== quoted_empty);
(true === quoted_string &&
typeof field === "string" &&
false !== quoted_empty);
if (shouldQuote === true) {
value = quote + value + quote;
}
Expand Down
25 changes: 25 additions & 0 deletions packages/csv-stringify/test/option.quoted_match.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
import "should";
import should from "should";
import { stringify } from "../lib/index.js";
import { stringify as stringifySync } from "../lib/sync.js";

describe("Option `quoted_match`", function () {
it("matches an empty string returned by a string cast", function (next) {
stringify(
[[" ", "", null, undefined, "text"]],
{ quoted_match: /^$/, cast: { string: (value) => value.trim() } },
(err, data) => {
if (err) return next(err);
data.should.eql('"","",,,text\n');
next();
},
);
});

it("matches empty cast values in the sync API and local options", function () {
const data = stringifySync([[" ", 0, {}, null, undefined]], {
quoted_match: /^$/,
cast: {
string: (value) => value.trim(),
number: () => "",
object: () => ({ value: "", quoted_match: "" }),
},
});
data.should.eql('"","","",,\n');
});

it("default to `null`", function (next) {
const stringifier = stringify([["abc", "def"]], () => {
should(stringifier.options.quoted_match).be.null();
Expand Down
33 changes: 33 additions & 0 deletions packages/csv-stringify/test/option.quoted_string.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
import "should";
import { stringify } from "../lib/index.js";
import { stringify as stringifySync } from "../lib/sync.js";

describe("Option `quoted_string`", function () {
it("quotes a string cast to an empty string", function (next) {
stringify(
[[" ", "", null, undefined, "text"]],
{ quoted_string: true, cast: { string: (value) => value.trim() } },
(err, data) => {
if (err) return next(err);
data.should.eql('"","",,,"text"\n');
next();
},
);
});

it("only quotes string fields when casts return empty strings", function () {
const data = stringifySync([[" ", "", 0, false, null, undefined]], {
quoted_string: true,
cast: {
string: (value) => value.trim(),
number: () => "",
},
});
data.should.eql('"","",,,,\n');
});

it("respects quoted_empty when strings are cast to empty strings", function () {
const data = stringifySync([[" ", ""]], {
quoted_string: true,
quoted_empty: false,
cast: { string: (value) => value.trim() },
});
data.should.eql(",\n");
});

it("quotes string fields", function (next) {
stringify(
[[undefined, null, "", " ", "x", 0, false]],
Expand Down