circular

Tiny utility to safely stringify objects with circular references

5 个版本
安装
npm install circular
yarn add circular
pnpm add circular
bun add circular
README

Circular

Tiny utility to safely stringify objects with circular references.

Usage

Replace all circular references with the string [Circular];

var circular = require('circular');
var obj = {}; var child = {parent: obj}; obj.child = child;
var str = JSON.stringify(obj, circular());
// => {"child":{"parent":"[Circular]"}}

If you prefer you can pass an alternative string to use:

var str = JSON.stringify(obj, circular('#ref'));

Or a function that returns a string:

function ref(value){return '' + value};
var str = JSON.stringify(obj, circular(ref));

As of version 1.0.4 you can also pass an additional boolean that will convert function references to strings, useful for converting javascript modules to couchdb design documents.

var str = JSON.stringify(obj, circular(null, true));

License

Everything is MIT. Read the license if you feel inclined.

版本列表
1.0.5 2014-08-26
1.0.4 2014-08-24
1.0.3 2014-03-01
1.0.2 2014-02-20
1.0.1 2014-02-19