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
8 changes: 4 additions & 4 deletions packages/csv-stringify/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ const stringifier = function (options, state, info) {
if (Array.isArray(chunk)) {
// We are getting an array but the user has specified output columns. In
// this case, we respect the columns indexes
if (columns) {
chunk.splice(columns.length);
}
const length = columns
? Math.min(chunk.length, columns.length)
: chunk.length;
// Cast record elements
for (let i = 0; i < chunk.length; i++) {
for (let i = 0; i < length; i++) {
const field = chunk[i];
const [err, value] = this.__cast(field, {
index: i,
Expand Down
23 changes: 23 additions & 0 deletions packages/csv-stringify/test/option.columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ describe("Option `columns`", function () {
);
});

it("input array is not altered", function (next) {
const records = [
["a", "b", "c"],
["d", "e", "f"],
];
stringify(
records,
{
columns: ["FIELD_1", "FIELD_2"],
},
(err, data) => {
if (!err) {
data.should.eql("a,b\nd,e\n");
records.should.eql([
["a", "b", "c"],
["d", "e", "f"],
]);
}
next(err);
},
);
});

it("is a readable stream", function (next) {
const ws = stringify(
{
Expand Down