Skip to content

Instantly share code, notes, and snippets.

@neopunisher
Last active June 8, 2018 14:57
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 neopunisher/c2e636ed2321a355815f7af87ae19525 to your computer and use it in GitHub Desktop.
Save neopunisher/c2e636ed2321a355815f7af87ae19525 to your computer and use it in GitHub Desktop.
recursive search the window like deep(window, 3500, 'window')
function deep(o, q, p = '.', d = 0, s = new Set()) {
try {
if (d > 5 || s.has(o)) {
console.log('depth met or seen', s.has(o), d)
} else if (Array.isArray(o)) {
s.add(o)
return [].concat(...o.map((a, b) => deep.apply(this, [a, q, p + ['[', ']'].join(b), d + 1, s])))
} else if (o instanceof Object) {
s.add(o)
return [].concat(...Object.keys(o).map((a, b) => deep.apply(this, [o[a], q, [p, a].join('.'), d + 1, s])))
} else {
if (o && ((typeof q == 'function') ? q(o) : (o.toString().indexOf(q) > -1))) {
return [p]
} else {
console.log(o, ' didnt match ', q)
}
}
} catch (e) {
console.log('caught err:', e)
}
return []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment