From 930df889a8324108ea5ef6480d6e13033175d9cd Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Mon, 17 Jul 2023 13:28:02 +0200 Subject: [PATCH 1/2] fix: insert "symbols" instead of `null` for deduped objects --- src/index.test.ts | 40 +++++++++++++++++++++++++++++++++++++--- src/plainer.ts | 4 ++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index 566d80f..070d3e6 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1158,7 +1158,7 @@ test('regression #245: superjson referential equalities only use the top-most pa expect(parsed).toEqual(input); }); -test('dedupe=true', () => { +test('dedupe=true, pruned', () => { const instance = new SuperJSON({ dedupe: true, }); @@ -1180,14 +1180,48 @@ test('dedupe=true', () => { expect(json.a); // This has already been seen and should be deduped - expect(json.b).toBeNull(); + expect(json.b).toEqual('[Pruned *]'); expect(json).toMatchInlineSnapshot(` Object { "a": Object { "children": Array [], }, - "b": null, + "b": "[Pruned *]", + } + `); + + expect(instance.deserialize(output)).toEqual(input); +}); + +test.only('dedupe=true, circular', () => { + const instance = new SuperJSON({ + dedupe: true, + }); + + type Node = { + children: Node[]; + }; + const root: Node = { + children: [], + }; + root.children.push(root); + const input = { + a: root, + b: root, + }; + const output = instance.serialize(input); + + const json = output.json as any; + + expect(json).toMatchInlineSnapshot(` + Object { + "a": Object { + "children": Array [ + "[Circular *]", + ], + }, + "b": "[Pruned *]", } `); diff --git a/src/plainer.ts b/src/plainer.ts index 1a568df..e369bd4 100644 --- a/src/plainer.ts +++ b/src/plainer.ts @@ -169,7 +169,7 @@ export const walker = ( // short-circuit result if we've seen this object before return dedupe ? { - transformedValue: null, + transformedValue: '[Pruned *]', } : seen; } @@ -195,7 +195,7 @@ export const walker = ( if (includes(objectsInThisPath, object)) { // prevent circular references return { - transformedValue: null, + transformedValue: '[Circular *]', }; } From 925cfd122190075dd556a31665c42f90d845d61a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 17 Jul 2023 16:35:53 +0200 Subject: [PATCH 2/2] Update src/index.test.ts Co-authored-by: Alex / KATT --- src/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.test.ts b/src/index.test.ts index 070d3e6..7f719fc 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1194,7 +1194,7 @@ test('dedupe=true, pruned', () => { expect(instance.deserialize(output)).toEqual(input); }); -test.only('dedupe=true, circular', () => { +test('dedupe=true, circular', () => { const instance = new SuperJSON({ dedupe: true, });