Created
March 31, 2020 10:51
-
-
Save cahnory/773a457bd6bbcf81a579eff6ccbf3e54 to your computer and use it in GitHub Desktop.
Debug utils
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
export default (value, cb, path = '', res = {}, scannedValues=[]) => { | |
if (!value || typeof value !== 'object' || scannedValues.includes(value)) { | |
return res; | |
} | |
scannedValues.push(value); | |
if (Array.isArray(value)) { | |
value.forEach((next, index) => { | |
if (cb(next, index)) { | |
res[`${path}${index}`] = next; | |
} | |
findProp(next, cb, `${path}${index}.`, res, scannedValues) | |
}); | |
} else { | |
Object.entries(value).forEach(([index, next]) => { | |
if (cb(next, index)) { | |
res[`${path}${index}`] = next; | |
} | |
findProp(next, cb, `${path}${index}.`, res, scannedValues) | |
}); | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment