Skip to content

Instantly share code, notes, and snippets.

@tuor4eg
Last active April 14, 2018 20:13
Show Gist options
  • Save tuor4eg/b454e8f8910440f7dafc0923c72e562f to your computer and use it in GitHub Desktop.
Save tuor4eg/b454e8f8910440f7dafc0923c72e562f to your computer and use it in GitHub Desktop.
export const findFile = (node, fn, pathToFile) => {
const pathElement = node.name;
if (node.type === 'directory') {
return node.children.reduce((res, element) => [...res, ...findFile(element, fn, path.join(pathToFile, pathElement))], []);
}
if (fn(node)) {
return [path.join(pathToFile, node.name)];
}
return [];
};
export default (tree, str) => findFile(tree, node => node.type === 'file' && node.name.includes(str), tree.name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment