Skip to content

Instantly share code, notes, and snippets.

@friendlyanon
Last active January 3, 2023 16:16
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 friendlyanon/071a61c6e8adb1d232349b720571dadc to your computer and use it in GitHub Desktop.
Save friendlyanon/071a61c6e8adb1d232349b720571dadc to your computer and use it in GitHub Desktop.
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