From 25924292fbe3a804507d0acd6d11c2a4fdff4df3 Mon Sep 17 00:00:00 2001 From: lux-liang <249971141+lux-liang@users.noreply.github.com> Date: Mon, 21 Sep 2026 15:35:37 +0800 Subject: [PATCH] fix(csv-stringify): preserve negative numeric values --- packages/csv-stringify/lib/api/index.js | 7 ++++++- .../csv-stringify/test/option.escape_formulas.ts | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/csv-stringify/lib/api/index.js b/packages/csv-stringify/lib/api/index.js index 2f5a5abf..a2a04536 100644 --- a/packages/csv-stringify/lib/api/index.js +++ b/packages/csv-stringify/lib/api/index.js @@ -228,9 +228,14 @@ const stringifier = function (options, state, info) { // Apple Numbers unicode normalization is empirical from testing if (escape_formulas) { switch (value[0]) { + case "-": + if (typeof field === "number" || typeof field === "bigint") { + break; + } + value = `'${value}`; + break; case "=": case "+": - case "-": case "@": case "\t": case "\r": diff --git a/packages/csv-stringify/test/option.escape_formulas.ts b/packages/csv-stringify/test/option.escape_formulas.ts index 9963c653..eb462adc 100644 --- a/packages/csv-stringify/test/option.escape_formulas.ts +++ b/packages/csv-stringify/test/option.escape_formulas.ts @@ -53,6 +53,21 @@ describe("Option `escape_formulas`", function () { ); }); + it("preserves negative numeric values", function (next) { + stringify( + [[-1.5, -2n, "-3.5"]], + { + escape_formulas: true, + eof: false, + }, + (err, data) => { + if (err) return next(err); + data.should.eql("-1.5,-2,'-3.5"); + next(); + }, + ); + }); + it("with `quoted` option", function (next) { stringify( [