Skip to content

Repository files navigation

decycle

NPM

NPM versionbuildcodecov

JSON decycle replaces circular references with {"$ref": PATH}, where PATH is the JSONPath of the first occurrence.

Inspired by cycle.js.

Quick Start

import{decycle}from'decycle';constobj={a: 1};obj.self=obj;JSON.stringify(decycle(obj));// '{"a":1,"self":{"$ref":"$"}}'

Install

NPM:

npm install decycle

Yarn:

yarn add decycle

CDN:

<scriptsrc="https://unpkg.com/decycle@latest/dist/index.umd.js"></script>

Usage

ES Modules:

import{decycle}from'decycle';

CommonJS:

const{ decycle }=require('decycle');

UMD:

<scriptsrc="https://unpkg.com/decycle@latest/dist/index.umd.js"></script><script>const{ decycle }=window.decycle;</script>

decycle(value, replacer?)

Returns a deep copy of object or array with circular references replaced by {"$ref": PATH}.

value

Type: unknown

The object or array to decycle.

replacer

Type: (value: unknown) => unknown

Optional replacer function called for each value. It receives a value and returns a replacement value.

Examples

Circular array:

constarray=[];array[0]=array;JSON.stringify(decycle(array));// '[{"$ref":"$"}]'

With replacer:

constobj={date: newDate('2026-02-07')};constresult=decycle(obj,(value)=>valueinstanceofDate ? value.toISOString() : value,);// { date: '2026-02-07T00:00:00.000Z' }

Release

Release is automated with Release Please.

License

MIT

About

🗂️ JSON decycle replaces circular references with JSONPath

Topics

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages