# decircularize

> Remove circular dependencies

Latest version **1.0.0** (published 2017-01-04) · WTFPL license · 0 weekly downloads

## Install

```sh
npm install decircularize
pnpm add decircularize
yarn add decircularize
bun add decircularize
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2017-01-04 |
| First published | 2017-01-04 |
| Weekly downloads | 0 |
| License | WTFPL |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Benjamin Horsleben |
| Maintainers | fizker |
| Keywords | circular, json |

## Links

- npm: https://www.npmjs.com/package/decircularize
- Repository: https://github.com/fizker/decircularize
- Homepage: https://github.com/fizker/decircularize#readme
- Issues: https://github.com/fizker/decircularize/issues
- npm.io page: https://npm.io/package/decircularize

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.0 (latest) — 2017-01-04

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

JSON.stringify(obj) // explodes

JSON.stringify(decircularize(obj), null, 2)
/* 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:

```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 [ '<root>', 'b' ]
		// and offendingPath equals [ '<root>', 'b', 'entities', 1 ]

		return { id: object.id }
	}
})
/* returns
{
	a: 1,
	b: {
		id: 'abc',
		entities: [
			2,
			{ id: 'abc' }
		]
	}
}
*/
```

---
_Source: https://npm.io/package/decircularize · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
