✨ Innovative and open-source visualization application that transforms various data formats, such as JSON, YAML, XML and CSV into interactive graphs.
refactor: traverse function
AykutSarac1400f755b080
diff --git a/src/utils/core/traverse.ts b/src/utils/core/traverse.tsindex 9ca409b6..4e6671ec 100644--- a/src/utils/core/traverse.ts+++ b/src/utils/core/traverse.ts@@ -22,33 +22,32 @@ const alignChildren = (nodeA: Node, nodeB: Node): number => {};function handleNoChildren(- value: any,+ value: string | undefined,states: States,graph: Graph,myParentId?: string,parentType?: string,nextType?: string) {- if (value !== undefined) {- if (parentType === "property" && nextType !== "object" && nextType !== "array") {- states.brothersParentId = myParentId;- if (nextType === undefined) {- states.brothersNode = [...states.brothersNode, [states.brotherKey, value]];- } else {- states.brotherKey = value;- }- } else if (parentType === "array") {- const nodeFromArrayId = addNodeToGraph({ graph, text: String(value) });- if (myParentId) {- addEdgeToGraph(graph, myParentId, nodeFromArrayId);- }+ if (value === undefined) return;++ if (parentType === "property" && nextType !== "object" && nextType !== "array") {+ states.brothersParentId = myParentId;+ if (nextType === undefined) {+ states.brothersNode.push([states.brotherKey, value]);+ } else {+ states.brotherKey = value;}- if (nextType && parentType !== "array") {- if (nextType === "object" || nextType === "array") {- states.parentName = value;- }+ } else if (parentType === "array") {+ const nodeFromArrayId = addNodeToGraph({ graph, text: String(value) });+ if (myParentId) {+ addEdgeToGraph(graph, myParentId, nodeFromArrayId);}}++ if (nextType && parentType !== "array" && (nextType === "object" || nextType === "array")) {+ states.parentName = value;+ }}function handleHasChildren(@@ -65,22 +64,25 @@ function handleHasChildren(// add last brothers node and add parent nodeif (states.brothersNode.length > 0) {- // add or concat brothers node of same parent- let findBrothersNode = states.brothersNodeProps.find(+ const findBrothersNode = states.brothersNodeProps.find(e =>e.parentId === states.brothersParentId &&e.objectsFromArrayId === states.objectsFromArray[states.objectsFromArray.length - 1]);+if (findBrothersNode) {- let ModifyNodes = [...graph.nodes];- let findNode = graph.nodes.findIndex(e => e.id === findBrothersNode?.id);-- if (ModifyNodes[findNode]) {- ModifyNodes[findNode].text = ModifyNodes[findNode].text.concat(states.brothersNode);- const { width, height } = calculateNodeSize(ModifyNodes[findNode].text, false);- ModifyNodes[findNode].width = width;- ModifyNodes[findNode].height = height;- graph.nodes = [...ModifyNodes];+ const findNodeIndex = graph.nodes.findIndex(e => e.id === findBrothersNode?.id);++ if (findNodeIndex !== -1) {+ const modifyNodes = [...graph.nodes];+ const foundNode = modifyNodes[findNodeIndex];++ foundNode.text = foundNode.text.concat(states.brothersNode);+ const { width, height } = calculateNodeSize(foundNode.text, false);+ foundNode.width = width;+ foundNode.height = height;++ graph.nodes = modifyNodes;states.brothersNode = [];}} else {@@ -90,91 +92,88 @@ function handleHasChildren(if (states.brothersParentId) {addEdgeToGraph(graph, states.brothersParentId, brothersNodeId);} else {- states.notHaveParent = [...states.notHaveParent, brothersNodeId];+ states.notHaveParent.push(brothersNodeId);}- states.brothersNodeProps = [- ...states.brothersNodeProps,- {- id: brothersNodeId,- parentId: states.brothersParentId,- objectsFromArrayId: states.objectsFromArray[states.objectsFromArray.length - 1],- },- ];+ states.brothersNodeProps.push({+ id: brothersNodeId,+ parentId: states.brothersParentId,+ objectsFromArrayId: states.objectsFromArray[states.objectsFromArray.length - 1],+ });}}- // add parent node+ // Add parent nodeparentId = addNodeToGraph({ graph, type, text: states.parentName });- states.bracketOpen = [...states.bracketOpen, { id: parentId, type }];+ states.bracketOpen.push({ id: parentId, type });states.parentName = "";- // add edges from parent node- let brothersProps = states.brothersNodeProps.filter(+ // Add edges from parent node+ const brothersProps = states.brothersNodeProps.filter(e =>e.parentId === myParentId &&e.objectsFromArrayId === states.objectsFromArray[states.objectsFromArray.length - 1]);+if ((brothersProps.length > 0 &&- states.bracketOpen[states.bracketOpen.length - 2] &&- states.bracketOpen[states.bracketOpen.length - 2].type !== "object") ||+ states.bracketOpen[states.bracketOpen.length - 2]?.type !== "object") ||(brothersProps.length > 0 && states.bracketOpen.length === 1)) {addEdgeToGraph(graph, brothersProps[brothersProps.length - 1].id, parentId);} else if (myParentId) {addEdgeToGraph(graph, myParentId, parentId);} else {- states.notHaveParent = [...states.notHaveParent, parentId];+ states.notHaveParent.push(parentId);}} else if (parentType === "array") {states.objectsFromArray = [...states.objectsFromArray, states.objectsFromArrayId++];}- (type === "object" ? children.sort(alignChildren) : children).forEach(- (objectToTraverse, index, array) => {- if (array[index + 1]) {- traverse({- states,- objectToTraverse,- parentType: type,- myParentId: states.bracketOpen[states.bracketOpen.length - 1]- ? states.bracketOpen[states.bracketOpen.length - 1].id- : undefined,- nextType: array[index + 1].type,- });- } else {- traverse({- states,- objectToTraverse,- parentType: type,- myParentId: states.bracketOpen[states.bracketOpen.length - 1]- ? states.bracketOpen[states.bracketOpen.length - 1].id- : undefined,- nextType: undefined,- });- }- }- );+ const traverseObject = (objectToTraverse: Node, nextType: string) => {+ traverse({+ states,+ objectToTraverse,+ parentType: type,+ myParentId: states.bracketOpen[states.bracketOpen.length - 1]?.id,+ nextType,+ });+ };++ const traverseArray = () => {+ children.forEach((objectToTraverse, index, array) => {+ const nextType = array[index + 1]?.type;+ traverseObject(objectToTraverse, nextType);+ });+ };++ if (type === "object") {+ children.sort(alignChildren);+ traverseArray();+ } else {+ traverseArray();+ }if (type !== "property") {- // when children end- // add or concat brothers node when it is the last parent node+ // Add or concatenate brothers node when it is the last parent nodeif (states.brothersNode.length > 0) {- let findBrothersNode = states.brothersNodeProps.find(+ const findBrothersNode = states.brothersNodeProps.find(e =>e.parentId === states.brothersParentId &&e.objectsFromArrayId === states.objectsFromArray[states.objectsFromArray.length - 1]);+if (findBrothersNode) {- let ModifyNodes = [...graph.nodes];- let findNode = graph.nodes.findIndex(e => e.id === findBrothersNode?.id);-- if (ModifyNodes[findNode]) {- ModifyNodes[findNode].text = ModifyNodes[findNode].text.concat(states.brothersNode);- const { width, height } = calculateNodeSize(ModifyNodes[findNode].text, false);- ModifyNodes[findNode].width = width;- ModifyNodes[findNode].height = height;- graph.nodes = [...ModifyNodes];+ const modifyNodes = [...graph.nodes];+ const findNodeIndex = modifyNodes.findIndex(e => e.id === findBrothersNode?.id);++ if (modifyNodes[findNodeIndex]) {+ modifyNodes[findNodeIndex].text += states.brothersNode;++ const { width, height } = calculateNodeSize(modifyNodes[findNodeIndex].text, false);+ modifyNodes[findNodeIndex].width = width;+ modifyNodes[findNodeIndex].height = height;++ graph.nodes = modifyNodes;states.brothersNode = [];}} else {@@ -187,18 +186,16 @@ function handleHasChildren(states.notHaveParent = [...states.notHaveParent, brothersNodeId];}- states.brothersNodeProps = [- ...states.brothersNodeProps,- {- id: brothersNodeId,- parentId: states.brothersParentId,- objectsFromArrayId: states.objectsFromArray[states.objectsFromArray.length - 1],- },- ];+ const brothersNodeProps = {+ id: brothersNodeId,+ parentId: states.brothersParentId,+ objectsFromArrayId: states.objectsFromArray[states.objectsFromArray.length - 1],+ };+ states.brothersNodeProps = [...states.brothersNodeProps, brothersNodeProps];}}- // close brackets+ // Close bracketsif (parentType === "array") {if (states.objectsFromArray.length > 0) {states.objectsFromArray.pop();@@ -211,16 +208,15 @@ function handleHasChildren(if (parentId) {const myChildren = graph.edges.filter(edge => edge.from === parentId);- const myIndex = graph.nodes.findIndex(node => node.id === parentId);+ const parentIndex = graph.nodes.findIndex(node => node.id === parentId);- const modifiedNodes = graph.nodes.map((node, index) => {- if (index === myIndex) {- return { ...node, data: { ...node.data, childrenCount: myChildren.length } };+ graph.nodes = graph.nodes.map((node, index) => {+ if (index === parentIndex) {+ const childrenCount = myChildren.length;+ return { ...node, data: { ...node.data, childrenCount } };}return node;});-- graph.nodes = modifiedNodes;}}}