Skip to content
Merged
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
11 changes: 5 additions & 6 deletions packages/csv-stringify/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,11 @@ const stringifier = function (options, state, info) {
value !== undefined &&
value !== null
) {
if (err)
return [
Error(
`Invalid Casting Value: returned value must return a string, null or undefined, got ${JSON.stringify(value)}`,
),
];
return [
Error(
`Invalid Casting Value: returned value must return a string, null or undefined, got ${JSON.stringify(value)}`,
),
];
}
// Merge global options with the ones returned by cast
options = { ...this.options, ...options };
Expand Down
24 changes: 24 additions & 0 deletions packages/csv-stringify/test/option.cast.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "should";
import { stringify } from "../lib/index.js";
import { stringify as stringifySync } from "../lib/sync.js";

describe("Option `cast`", function () {
describe("udf", function () {
Expand All @@ -22,6 +23,29 @@ describe("Option `cast`", function () {
});

describe("info object", function () {
for (const value of [0, false]) {
const message = `Invalid Casting Value: returned value must return a string, null or undefined, got ${JSON.stringify(value)}`;

it(`reject invalid value ${JSON.stringify(value)} in the callback API`, function (next) {
stringify(
[["input"]],
{ cast: { string: () => ({ value }) } },
(err) => {
if (!err)
return next(Error("Expected an invalid casting value error"));
err.message.should.eql(message);
next();
},
);
});

it(`reject invalid value ${JSON.stringify(value)} in the sync API`, function () {
(function () {
stringifySync([["input"]], { cast: { string: () => ({ value }) } });
}).should.throw({ message });
});
}

it("validate and normalize local options", function (next) {
stringify(
[["invalid cast"]],
Expand Down
18 changes: 18 additions & 0 deletions packages/csv-stringify/test/option.cast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ describe("Option `cast`", function () {
});

describe("info object", function () {
it("accept string, null and undefined values in cast options", function (next) {
stringify(
[["empty", "null", "undefined", "text"]],
{
cast: {
string: (value, context) => ({
value: ["", null, undefined, "text"][context.index],
}),
},
},
(err, data) => {
if (err) return next(err);
data.should.eql(",,,text\n");
next();
},
);
});

it("preserves a reused cast result", function (next) {
const result = { value: "shared", quoted: true };
stringify(
Expand Down