Skip to content

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?

//=> 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.

//=> 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.


##### 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.

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