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

Introduce trace flag to mark circular reference keys (#1) #3

Closed
wants to merge 1 commit into from

Conversation

GeorgeGkas
Copy link

@GeorgeGkas GeorgeGkas commented Apr 23, 2022

Fixes #1

@sindresorhus
Copy link
Owner

Can you add a test where the trace refers to a nested property?

@@ -23,6 +23,9 @@ console.log(safeStringify(foo));

console.log(JSON.stringify(foo));
//=> TypeError: Converting circular structure to JSON

console.log(safeStringify(foo, {trace: true}));
//=> '{ "a": true, "b": "[Circular *]" }'
Copy link
Owner

Choose a reason for hiding this comment

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

I think the example should be moved to the trace option docs.

@@ -23,6 +23,9 @@ console.log(safeStringify(foo));

console.log(JSON.stringify(foo));
//=> TypeError: Converting circular structure to JSON

console.log(safeStringify(foo, {trace: true}));
//=> '{ "a": true, "b": "[Circular *]" }'
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
//=> '{ "a": true, "b": "[Circular *]" }'
//=> '{"a":true,"b":"[Circular *]"}'

Copy link
Owner

Choose a reason for hiding this comment

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

It would be better to use an example that shows a named pointer *c.

@@ -51,6 +54,14 @@ By default, the JSON is not indented.

Set it to `'\t'` for tab indentation or the number of spaces you want.

##### trace

Type: `boolean`
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
Type: `boolean`
Type: `boolean`\
Default: `false`

/**
Enable key tracing for circular references.

By default, this option is set to false.
Copy link
Owner

Choose a reason for hiding this comment

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

@@ -7,6 +7,13 @@ export type Options = {
Set it to `'\t'` for tab indentation or the number of spaces you want.
*/
readonly indentation?: string | number;

/**
Enable key tracing for circular references.
Copy link
Owner

Choose a reason for hiding this comment

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

Incorrect indentation.


Type: `boolean`

Enable key tracing for circular references.
Copy link
Owner

Choose a reason for hiding this comment

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

I think the description should be clearer about what it does.

@sindresorhus
Copy link
Owner

@GeorgeGkas Still interested in finishing this? :)

@GeorgeGkas
Copy link
Author

Hey @sindresorhus. I'm looking into this a bit more and I believe we still have not covered all the cases. The current implementation has a bug. Consider the following example:

const foo = {a: true};
foo.b = {};
foo.c = foo.b;

If we run safeStringify we get the following result:

{
        "a": true,
        "b": {},
        "c": "[Circular *b]"
}

where the correct output should be:

{
        "a": true,
        "b": {},
        "c": {}
}

Even if there is no circular dependency, the algorithm thinks otherwise. This results from the way we check for circular dependencies. Unfortunately, I don't have time to look into this in more depth now. Should I close this PR and come back again with a better

@sindresorhus
Copy link
Owner

I'm aware of that issue and intend to look into it, but I think that issue needs to block this pull request.

@sindresorhus
Copy link
Owner

Closing for lack of activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optionally, replace circular references with the key it points to
2 participants