Last active
June 30, 2024 06:02
-
-
Save No-Eul/ce773f48eeaff5e8207d9926b78639b4 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 find(object, predicate, excludes, thresholdDepth) { | |
let result = new Map; | |
function find0(path, object, result, depth) { | |
if (depth >= thresholdDepth) return; | |
for (let key of Object.getOwnPropertyNames(object)) { | |
let value, childPath = [...path, key]; | |
try { | |
value = object[key]; | |
} catch (key) { | |
return; | |
} | |
if (excludes.some(test => test(key, value))) continue; | |
if (predicate(key, value)) result.set(childPath, value); | |
if (value instanceof Object) | |
find0(childPath, value, result, depth + 1); | |
} | |
} | |
find0("", object, result, 0); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment