Skip to content

Returns a copy of the given object without circular references.

License

Notifications You must be signed in to change notification settings

sngular/replace-circular-object

Repository files navigation

NPM version

Summary

Retrieve a duplicate of the given object, eliminating any circular references.

This function replaces circular references with a specified default value when the number of circular references exceeds a defined depth limit. Both the default value and depth limit are customizable and optional.

If no default value is specified, an empty object or array will be used. If no depth limit is specified, the function will replace circular references at the first occurrence with a depth limit of 0.

CommonJS & ESM supported

Requirements

Node >= 12

Installation

npm i replace-circular-object

Importing

ESM import

import replaceCircularObject from 'replace-circular-object';

CommonJS require

const { replaceCircularObject } = require('replace-circular-object');

Usage

const circularObject = {};
circularObject.property = circularObject;

const replacedObject = replaceCircularObject(circularObject, 'Default Value!', 3);

console.log(replacedObject); // { property: { property: { property: 'Default Value!' } } }

More examples at test/index.test.js

About

Returns a copy of the given object without circular references.

Resources

License

Stars

Watchers

Forks

Packages

No packages published