Skip to content

Instantly share code, notes, and snippets.

@jdmichaud
Created October 10, 2018 07:27
Show Gist options
  • Save jdmichaud/16a78c1d24fd204abe1d2ccb6aa9182c to your computer and use it in GitHub Desktop.
Save jdmichaud/16a78c1d24fd204abe1d2ccb6aa9182c to your computer and use it in GitHub Desktop.
Detect circular dependency in object
const circularDepDetector = (function () {
const set = new Set();
return function(key, value) {
if (set.has(value)) {
return 'CIRCULAR DEPENDENCY';
}
set.add(value);
return value;
};
})();
const o = { toto: 42 };
const p = { o };
o.p = p;
JSON.stringify(p, circularDepDetector);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment