Created
October 10, 2018 07:27
-
-
Save jdmichaud/16a78c1d24fd204abe1d2ccb6aa9182c to your computer and use it in GitHub Desktop.
Detect circular dependency in object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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