Skip to content

Instantly share code, notes, and snippets.

@cahnory
Created March 31, 2020 10:51
Show Gist options
  • Save cahnory/773a457bd6bbcf81a579eff6ccbf3e54 to your computer and use it in GitHub Desktop.
Save cahnory/773a457bd6bbcf81a579eff6ccbf3e54 to your computer and use it in GitHub Desktop.
Debug utils
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