From e8f599be0a059328cb1fb75f03f8f28e1948c622 Mon Sep 17 00:00:00 2001 From: ItsLemmy Date: Thu, 16 Oct 2025 15:02:35 -0400 Subject: [PATCH] Added a new helpers (Debug.js) to help stringify objects with circular refs. --- Helpers/Debug.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Helpers/Debug.js diff --git a/Helpers/Debug.js b/Helpers/Debug.js new file mode 100644 index 000000000..90dc27e73 --- /dev/null +++ b/Helpers/Debug.js @@ -0,0 +1,25 @@ +// Helper function to stringify even if there are circular refs. +function stringify(obj, replacer, spaces, cycleReplacer) { + return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces) +} + +function serializer(replacer, cycleReplacer) { + var stack = [], keys = [] + + if (cycleReplacer == null) cycleReplacer = function(key, value) { + if (stack[0] === value) return "[Circular ~]" + return "[Circular ~." + keys.slice(0, stack.indexOf(value)).join(".") + "]" + } + + return function(key, value) { + if (stack.length > 0) { + var thisPos = stack.indexOf(this) + ~thisPos ? stack.splice(thisPos + 1) : stack.push(this) + ~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key) + if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value) + } + else stack.push(value) + + return replacer == null ? value : replacer.call(this, key, value) + } +} \ No newline at end of file