From e6b442c6c195dfd1d3da6743084dca4db7f40436 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Thu, 3 Sep 2026 00:38:44 +0100 Subject: [PATCH] fix: array-form replacer should not filter Array elements --- cjs/index.js | 2 +- esm/index.js | 2 +- test/index.js | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cjs/index.js b/cjs/index.js index a4833a5..c70b024 100644 --- a/cjs/index.js +++ b/cjs/index.js @@ -86,7 +86,7 @@ exports.parse = parse; */ const stringify = (value, replacer, space) => { const $ = replacer && typeof replacer === object ? - (k, v) => (k === '' || -1 < replacer.indexOf(k) ? v : void 0) : + function (k, v) { return k === '' || Array.isArray(this) || -1 < replacer.indexOf(k) ? v : void 0; } : (replacer || noop); const known = new Map; const input = []; diff --git a/esm/index.js b/esm/index.js index eb2da2d..2ea6a5f 100644 --- a/esm/index.js +++ b/esm/index.js @@ -84,7 +84,7 @@ export const parse = (text, reviver) => { */ export const stringify = (value, replacer, space) => { const $ = replacer && typeof replacer === object ? - (k, v) => (k === '' || -1 < replacer.indexOf(k) ? v : void 0) : + function (k, v) { return k === '' || Array.isArray(this) || -1 < replacer.indexOf(k) ? v : void 0; } : (replacer || noop); const known = new Map; const input = []; diff --git a/test/index.js b/test/index.js index 85c18fd..6a1a52e 100644 --- a/test/index.js +++ b/test/index.js @@ -390,6 +390,16 @@ if (typeof Symbol !== 'undefined') { ); }()); +(function () { + var a = ['a', 'b', 'c']; + var json = JSON.stringify(a, ['0']); + var restored = Flatted.parse(Flatted.stringify(a, ['0'])); + console.assert( + JSON.stringify(restored) === json, + 'whitelisted replacer does not filter Array values: '+ json + ); +}()); + (function () { var a = { b: { '': { c: { d: 1 } } } }; a._circular = a.b[''];