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( [