-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add graphParser to parse graph Nodes into JSON - add functionality to update JSON on node edit References Issue #147 Changes to be committed: modified: src/containers/Modals/NodeModal/index.tsx new file: src/utils/graphParser.ts
- Loading branch information
1 parent
62b2666
commit 0555858
Showing
2 changed files
with
53 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
const nodeToValue = (node:NodeData) => Array.isArray(node.text) ? Object.fromEntries(node.text) : node.text; | ||
|
||
const hasEdgeLeaving = (node:NodeData, edges:EdgeData[]) => edges.some((edge)=> node.id === edge.from); | ||
const hasChild = (node:NodeData) => !!node.data.hasChild; | ||
|
||
const getChildNode = (edge: EdgeData, nodes: NodeData[]) => nodes.find((node)=> node.id === edge.to) as NodeData; | ||
const moveNodeToFront = (node: NodeData, nodes: NodeData[]) => [node, ...removeNode(node, nodes)]; | ||
|
||
const removeNode = (node: NodeData, nodes: NodeData[]) => nodes.filter((n)=> n.id !== node.id); | ||
const removeEdge = (edge: EdgeData, edges: EdgeData[]) => edges.filter((e)=> e.id !== edge.id); | ||
|
||
const getEdge = (node:NodeData, edges:EdgeData[]) => edges.find((edge)=> node.id === edge.from) as EdgeData; | ||
|
||
export const graphParser = (nodes:NodeData[], edges:EdgeData[]) =>{ | ||
const currentNode = nodes[0]; | ||
if((!hasChild(currentNode) && !hasEdgeLeaving(currentNode, edges))){ | ||
return nodeToValue(currentNode); | ||
} | ||
if(hasChild(currentNode)) { | ||
const childEdges = edges.filter((edge)=> edge.from === currentNode.id); | ||
const remainingEdges = edges.filter((edge)=> edge.from !== currentNode.id); | ||
const remainingNodes = removeNode(currentNode, nodes); | ||
const key = nodeToValue(currentNode); | ||
const obj = {}; | ||
obj[key] = childEdges.map((edge)=>{ | ||
const childNode = getChildNode(edge, remainingNodes); | ||
const nextNodes = moveNodeToFront(childNode, remainingNodes); | ||
return typeof nodeToValue(childNode) === 'string' | ||
? nodeToValue(childNode) | ||
: Object.assign({}, nodeToValue(childNode), graphParser(nextNodes, remainingEdges)); | ||
}); | ||
return obj; | ||
} | ||
const remainingNodes = removeNode(currentNode, nodes); | ||
const nextEdge = getEdge(currentNode, edges); | ||
const remainingEdges = removeEdge(nextEdge, edges); | ||
const childNode = getChildNode(nextEdge, remainingNodes); | ||
const nextNodes = moveNodeToFront(childNode, remainingNodes); | ||
|
||
return Object.assign({}, nodeToValue(currentNode), graphParser(nextNodes, remainingEdges)); | ||
} | ||
|