Last active
June 8, 2018 14:57
-
-
Save neopunisher/c2e636ed2321a355815f7af87ae19525 to your computer and use it in GitHub Desktop.
recursive search the window like deep(window, 3500, 'window')
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
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