Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: insert "symbols" instead of null for deduped objects #251

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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('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 *]",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this say [Pruned <path-where-the-node-is>] or something?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're adding this for the output to be more human-readable, I think we should take a string that's searchable + self-explanatory. I don't think Pruned cuts it. What do you think about something like[SuperJSON: Equal to <path.where.node>]?

}
`);

Expand Down
4 changes: 2 additions & 2 deletions src/plainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const walker = (
// short-circuit result if we've seen this object before
return dedupe
? {
transformedValue: null,
transformedValue: '[Pruned *]',
}
: seen;
}
Expand All @@ -195,7 +195,7 @@ export const walker = (
if (includes(objectsInThisPath, object)) {
// prevent circular references
return {
transformedValue: null,
transformedValue: '[Circular *]',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also put the path to the object that's circular into here.

};
}

Expand Down