-
-
Save jankapunkt/f3ca8aa7ea99a7878e87a485ee5134bb to your computer and use it in GitHub Desktop.
Detect circular references in objects
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
export const isCircular = target => { | |
const map = new WeakMap() | |
const detect = obj => { | |
if (obj === null || typeof obj !== 'object') return false | |
if (map.get(obj)) return true | |
map.set(obj, true) | |
return Object.values(obj).some(objProp => detect(objProp)) | |
} | |
return detect(target) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment