getOutgoers.ts 862 B

12345678910111213141516171819202122232425262728
  1. export const getOutgoers = (
  2. nodeId: string,
  3. nodes: NodeData[],
  4. edges: EdgeData[],
  5. parent: string[] = []
  6. ): [NodeData[], string[]] => {
  7. const outgoerNodes: NodeData[] = [];
  8. const matchingNodes: string[] = [];
  9. if (parent.includes(nodeId)) {
  10. const initialParentNode = nodes.find(n => n.id === nodeId);
  11. if (initialParentNode) outgoerNodes.push(initialParentNode);
  12. }
  13. const runner = (nodeId: string) => {
  14. const outgoerIds = edges.filter(e => e.from === nodeId).map(e => e.to);
  15. const nodeList = nodes.filter(n => {
  16. if (parent.includes(n.id) && !matchingNodes.includes(n.id)) matchingNodes.push(n.id);
  17. return outgoerIds.includes(n.id) && !parent.includes(n.id);
  18. });
  19. outgoerNodes.push(...nodeList);
  20. nodeList.forEach(node => runner(node.id));
  21. };
  22. runner(nodeId);
  23. return [outgoerNodes, matchingNodes];
  24. };