Skip to content

Instantly share code, notes, and snippets.

@jankapunkt
Forked from lukas-zech-software/circular.js
Last active July 10, 2019 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jankapunkt/f3ca8aa7ea99a7878e87a485ee5134bb to your computer and use it in GitHub Desktop.
Save jankapunkt/f3ca8aa7ea99a7878e87a485ee5134bb to your computer and use it in GitHub Desktop.
Detect circular references in objects
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