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
Circular data & cross references #110 #148
Conversation
if (0 !== index) { | ||
_result += ', '; | ||
if (!duplicate || !state.usedDuplicates[duplicateIndex]) { | ||
state.usedDuplicates[duplicateIndex] = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you duplicate this code across all of the writers? Can it be placed in writeNode function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll study the possibility.
In conclusion, please merge all these commits together. |
|
||
if (null !== object && 'object' === typeof object) { | ||
index = objects.indexOf(object); | ||
if (index !== -1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, by the way, if you swap the operands here, it'll be great.
Sorry about the small remainders, should have seen them. Anyway, here you go. |
https://github.com/nodeca/js-yaml/pull/148/commits @loamhoof squash to single commit please. See 'git rebase --interactive' in google |
Catch circular and cross references. The object is first scanned to get these references before starting the writing algorithm. Aliases are then added during it.
@puzrin sorry about that, it's done. |
Circular data & cross references #110
Perfect! Thank you for contribution! |
Adding an option (checkDuplicates) to catch circular and cross
references, disabled by default so as not to slow down any existing code (only a few bits of code are still executed when the option is disabled).
Implemented following @puzrin suggestion that is: do a first pass to get the duplicates, and then put the aliases inside the algorithm. Slower than @larkox 's implementation, but perhaps easier to maintain.
Also added a related test.
NB: There may be some unused aliases if a duplicate is found inside a custom type, which must be a pretty rare case.