Skip to content

Instantly share code, notes, and snippets.

@neopunisher
Last active Jun 8, 2018
Embed
What would you like to do?
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