Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

decircularize

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

decircularize

Remove circular dependencies

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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.

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:

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' }
		]
	}
}
*/

Keywords

FAQs

Package last updated on 04 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc