Created
January 22, 2019 11:52
-
-
Save friendlyanon/7c6037893409d1f37b34f145a0a95e56 to your computer and use it in GitHub Desktop.
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 _search(parents, path, child, results, keysToSearch, valuesToSearch) { | |
for (const [k, { value: v }] of Object.entries(Object.getOwnPropertyDescriptors(child))) { | |
path[path.length] = String(k); | |
if (typeof v === "object") { | |
if (v != null && !parents.has(v)) { | |
parents.add(v); | |
try { | |
_search(parents, path, v, results, keysToSearch, valuesToSearch); | |
} catch(err) { return console.error("Stack limit reached at: " + path.join(" -> ")); } | |
} | |
} | |
else { | |
if (keysToSearch.includes(k) || valuesToSearch.includes(v)) | |
results[results.length] = [path.join(" -> "), v]; | |
} | |
--path.length; | |
} | |
} | |
function deepSearch(root, keysToSearch = [], valuesToSearch = []) { | |
const parents = new WeakSet().add(root); | |
const path = ["root"]; | |
const results = []; | |
if (typeof root === "object" && root != null) | |
_search(parents, path, root, results, keysToSearch, valuesToSearch); | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment