Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fizker/decircularize
Remove circular dependencies
https://github.com/fizker/decircularize
Last synced: 4 days ago
JSON representation
Remove circular dependencies
- Host: GitHub
- URL: https://github.com/fizker/decircularize
- Owner: fizker
- License: wtfpl
- Created: 2017-01-04T10:41:26.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-04T10:44:05.000Z (almost 8 years ago)
- Last Synced: 2024-11-03T19:05:28.501Z (14 days ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
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.```js
import decircularize from 'decircularize'var obj = {
a: 1,
b: { something: 2 }
}
obj.b.bad = obj
obj.b.alsoBad = obj.bJSON.stringify(obj) // explodes
JSON.stringify(decircularize(obj), null, 2)
/* returns
{
a: 1,
b: {
something: 2,
bad: '[Circular to: ]',
alsoBad: '[Circular to: .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:```js
import decircularize from 'decircularize'var obj = {
id: '123',
b: {
id: 'abc',
entities: [ 2 ]
}
}
obj.b.entities.push(obj.b)decircularize(obj, {
onCircular: (object, otherPath, offendingPath) => {
assert(object === obj.b)// In this instance, otherPath equals [ '', 'b' ]
// and offendingPath equals [ '', 'b', 'entities', 1 ]return { id: object.id }
}
})
/* returns
{
a: 1,
b: {
id: 'abc',
entities: [
2,
{ id: 'abc' }
]
}
}
*/
```