MCPcopy Create free account
hub / github.com/datalust/seq-logging / removeCirculars

Function removeCirculars

seq_logger.js:300–330  ·  view source on GitHub ↗
(obj, branch = new Map(), path = "root")

Source from the content-addressed store, hash-verified

298};
299
300const removeCirculars = (obj, branch = new Map(), path = "root") => {
301 if (isValue(obj)) return obj;
302 if (branch.has(obj)) {
303 // In seq it is more clear if we remove the root.Properties object path
304 const circularPath = branch.get(obj).replace("root.Properties.", "");
305 return "== Circular structure: '" + circularPath + "' ==";
306 } else {
307 branch.set(obj, path);
308 }
309
310 if (obj instanceof Array) {
311 return obj.map((value, i) =>
312 isValue(value) ? value : removeCirculars(value, new Map(branch), path + `[${i}]`)
313 );
314 }
315 const keys = Object.keys(obj);
316 // Will rescue Date and other classes.
317 if (keys.length === 0) {
318 return obj;
319 }
320 const replaced = {};
321 keys.forEach((key) => {
322 const value = obj[key];
323 if (isValue(value)) {
324 replaced[key] = value;
325 return;
326 }
327 replaced[key] = removeCirculars(value, new Map(branch), path + "." + key);
328 });
329 return replaced;
330};
331
332export { DefineLogger };

Callers 1

_dequeBatchMethod · 0.85

Calls 1

isValueFunction · 0.85

Tested by

no test coverage detected