decircularize
Remove circular dependencies. It also functions as a deep-copy, to ensure that the input is never mutated and that the output can be trusted, even if there are no issues with circular structures.
Usage
The default behavior replaces the dependencies with a string suitable for debugging.
var obj = a: 1 b: something: 2 objbbad = objobjbalsoBad = objb JSON // explodes JSON/* returns{ a: 1, b: { something: 2, bad: '[Circular to: <root>]', alsoBad: '[Circular to: <root>.b]' }}*/
This can easily be overridden for any custom need. For example, imagine a REST
API where each entity have an id
prop that uniquely identifies it:
var obj = id: '123' b: id: 'abc' entities: 2 objbentities /* returns{ a: 1, b: { id: 'abc', entities: [ 2, { id: 'abc' } ] }}*/