From 567d94e220533de9206c821a168ccca8ee244acf Mon Sep 17 00:00:00 2001 From: youdie006 Date: Wed, 9 Sep 2026 12:19:22 +0900 Subject: [PATCH 1/2] fix(csv-stringify): do not alter the source record when columns is set When a record is an array and the `columns` option is shorter than that array, the stringifier called `chunk.splice(columns.length)`, which truncates the array owned by the caller instead of only limiting what is written. Bound the cast loop instead, leaving the input untouched. The object branch already reads through `get()` into a fresh local record. Assisted-by: Claude Code:claude-opus-5 --- packages/csv-stringify/lib/api/index.js | 8 +++---- packages/csv-stringify/test/option.columns.ts | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/packages/csv-stringify/lib/api/index.js b/packages/csv-stringify/lib/api/index.js index 821d096a..2c5bc4bb 100644 --- a/packages/csv-stringify/lib/api/index.js +++ b/packages/csv-stringify/lib/api/index.js @@ -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, diff --git a/packages/csv-stringify/test/option.columns.ts b/packages/csv-stringify/test/option.columns.ts index 37bf8fd4..c6d1fd1a 100644 --- a/packages/csv-stringify/test/option.columns.ts +++ b/packages/csv-stringify/test/option.columns.ts @@ -128,6 +128,29 @@ describe("Option `columns`", function () { ); }); + it("is an array, should not be altered", function (next) { + const records = [ + ["20322051544", "1979", "8.8017226E7"], + ["28392898392", "1974", "8.8392926E7"], + ]; + stringify( + records, + { + columns: ["FIELD_1", "FIELD_2"], + }, + (err, data) => { + if (!err) { + data.should.eql("20322051544,1979\n28392898392,1974\n"); + records.should.eql([ + ["20322051544", "1979", "8.8017226E7"], + ["28392898392", "1974", "8.8392926E7"], + ]); + } + next(err); + }, + ); + }); + it("is a readable stream", function (next) { const ws = stringify( { From 2fb399d259873ac9d903a866b2857a41e8a74b00 Mon Sep 17 00:00:00 2001 From: David Worms Date: Wed, 23 Sep 2026 09:59:10 +0200 Subject: [PATCH 2/2] test(csv-stringify): rename vars --- packages/csv-stringify/test/option.columns.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/csv-stringify/test/option.columns.ts b/packages/csv-stringify/test/option.columns.ts index c6d1fd1a..be65b572 100644 --- a/packages/csv-stringify/test/option.columns.ts +++ b/packages/csv-stringify/test/option.columns.ts @@ -128,10 +128,10 @@ describe("Option `columns`", function () { ); }); - it("is an array, should not be altered", function (next) { + it("input array is not altered", function (next) { const records = [ - ["20322051544", "1979", "8.8017226E7"], - ["28392898392", "1974", "8.8392926E7"], + ["a", "b", "c"], + ["d", "e", "f"], ]; stringify( records, @@ -140,10 +140,10 @@ describe("Option `columns`", function () { }, (err, data) => { if (!err) { - data.should.eql("20322051544,1979\n28392898392,1974\n"); + data.should.eql("a,b\nd,e\n"); records.should.eql([ - ["20322051544", "1979", "8.8017226E7"], - ["28392898392", "1974", "8.8392926E7"], + ["a", "b", "c"], + ["d", "e", "f"], ]); } next(err);