| 298 | }; |
| 299 | |
| 300 | const 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 | |
| 332 | export { DefineLogger }; |