PromucFlow_constructor/app/client/src/utils/treeUtils.ts
2019-11-19 12:44:58 +00:00

23 lines
540 B
TypeScript

type Tree = {
children?: Tree[];
[key: string]: any;
};
export const traverseTree = (tree: Tree, callback: Function) => {
callback(tree);
if (tree.children) {
tree.children.forEach(b => traverseTree(b, callback));
}
};
export const mapTree = (tree: Tree, callback: Function) => {
const mapped = callback(tree);
if (tree.children && tree.children.length) {
const children: Tree[] = tree.children.map(branch =>
mapTree(branch, callback),
);
return { ...mapped, children };
}
return { ...mapped };
};