Last active
January 3, 2023 16:16
-
-
Save friendlyanon/071a61c6e8adb1d232349b720571dadc to your computer and use it in GitHub Desktop.
This file contains 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(visited, path, object, keys, values) { | |
for (const key of Reflect.ownKeys(object)) { | |
const value = object[key]; | |
if (keys.has(key) || values.has(value)) { | |
yield [path.slice(), value]; | |
} | |
if (typeof value === "object" && value != null && !visited.has(value)) { | |
visited.add(value); | |
path.push(key); | |
yield* search(visited, path, value, keys, values); | |
path.pop(); | |
} | |
} | |
} | |
export function deepSearch(root, keys = [], values = []) { | |
const visited = new WeakSet().add(root); | |
const path = []; | |
return search(visited, path, root, new Set(keys), new Set(values)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment