File size: 458 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
export function circularReferenceSafeJSONStringify( json, space ) {
	try {
		let cache = [];
		const str = JSON.stringify(
			json,
			function ( key, value ) {
				if ( typeof value === 'object' && value !== null ) {
					if ( cache.indexOf( value ) !== -1 ) {
						return 'Circular reference';
					}
					cache.push( value );
				}
				return value;
			},
			space
		);
		cache = null;
		return str;
	} catch ( e ) {
		return 'Error: ' + e.message;
	}
}