fix: different brothers node in array is not work correctly

This commit is contained in:
victorbrambati 2022-11-03 19:42:01 -03:00
parent 76bcad4e1d
commit e772153cd3

View File

@ -77,6 +77,7 @@ export const parser = (jsonStr: string, isFolded = false) => {
let parentName: string = ""; let parentName: string = "";
let bracketOpen: { id: string; type: string }[] = []; let bracketOpen: { id: string; type: string }[] = [];
let objectsFromArray: number[] = []; let objectsFromArray: number[] = [];
let objectsFromArrayId = 0;
let notHaveParent: string[] = []; let notHaveParent: string[] = [];
let brothersNode: [string, string][] = []; let brothersNode: [string, string][] = [];
let brothersParentId: string | undefined = ""; let brothersParentId: string | undefined = "";
@ -127,15 +128,16 @@ export const parser = (jsonStr: string, isFolded = false) => {
if (type !== "property" && parentName !== "") { if (type !== "property" && parentName !== "") {
// add last brothers node and add parent node // add last brothers node and add parent node
if (brothersNode.length > 0) { if (brothersNode.length > 0) {
// add or concat brothers node of same parent // add or concat brothers node of same parent
let findBrothersNode = brothersNodeProps.find( let findBrothersNode = brothersNodeProps.find(
e => e =>
e.parentId === brothersParentId && e.parentId === brothersParentId &&
e.objectsFromArrayId === e.objectsFromArrayId ===
objectsFromArray[objectsFromArray.length - 2] objectsFromArray[objectsFromArray.length - 1]
); );
if (findBrothersNode && parentType !== "array") { if (findBrothersNode) {
let ModifyNodes = [...nodes]; let ModifyNodes = [...nodes];
let findNode = nodes.findIndex(e => e.id === findBrothersNode?.id); let findNode = nodes.findIndex(e => e.id === findBrothersNode?.id);
@ -199,9 +201,8 @@ export const parser = (jsonStr: string, isFolded = false) => {
notHaveParent = [...notHaveParent, parentId]; notHaveParent = [...notHaveParent, parentId];
} }
} else if (parentType === "array") { } else if (parentType === "array") {
objectsFromArray = [...objectsFromArray, objectsFromArray.length + 1]; objectsFromArray = [...objectsFromArray, objectsFromArrayId++];
} }
children.forEach((branch, index, array) => { children.forEach((branch, index, array) => {
if (array[index + 1]) { if (array[index + 1]) {
traverse( traverse(
@ -225,19 +226,6 @@ export const parser = (jsonStr: string, isFolded = false) => {
if (type !== "property") { if (type !== "property") {
// when children end // when children end
if (parentType !== "array") {
if (bracketOpen.length > 0) {
let newBracketOpen = [...bracketOpen];
newBracketOpen.splice(newBracketOpen.length - 1);
bracketOpen = [...newBracketOpen];
}
} else if (parentType === "array") {
if (objectsFromArray.length > 0) {
let newobjectsFromArray = [...objectsFromArray];
newobjectsFromArray.splice(newobjectsFromArray.length - 1);
objectsFromArray = [...newobjectsFromArray];
}
}
// add or concat brothers node when it is the last parent node // add or concat brothers node when it is the last parent node
if (brothersNode.length > 0) { if (brothersNode.length > 0) {
@ -245,9 +233,9 @@ export const parser = (jsonStr: string, isFolded = false) => {
e => e =>
e.parentId === brothersParentId && e.parentId === brothersParentId &&
e.objectsFromArrayId === e.objectsFromArrayId ===
objectsFromArray[objectsFromArray.length - 2] objectsFromArray[objectsFromArray.length - 1]
); );
if (findBrothersNode && parentType !== "array") { if (findBrothersNode) {
let ModifyNodes = [...nodes]; let ModifyNodes = [...nodes];
let findNode = nodes.findIndex(e => e.id === findBrothersNode?.id); let findNode = nodes.findIndex(e => e.id === findBrothersNode?.id);
@ -286,6 +274,21 @@ export const parser = (jsonStr: string, isFolded = false) => {
} }
} }
// close brackets
if (parentType !== "array") {
if (bracketOpen.length > 0) {
let newBracketOpen = [...bracketOpen];
newBracketOpen.splice(newBracketOpen.length - 1);
bracketOpen = [...newBracketOpen];
}
} else if (parentType === "array") {
if (objectsFromArray.length > 0) {
let newobjectsFromArray = [...objectsFromArray];
newobjectsFromArray.splice(newobjectsFromArray.length - 1);
objectsFromArray = [...newobjectsFromArray];
}
}
if (parentId) { if (parentId) {
let myChildrens = edges.filter(e => e.from === parentId); let myChildrens = edges.filter(e => e.from === parentId);
let myIndex = nodes.findIndex(e => e.id === parentId); let myIndex = nodes.findIndex(e => e.id === parentId);
@ -322,6 +325,7 @@ export const parser = (jsonStr: string, isFolded = false) => {
} }
} }
console.log(nodes);
return { nodes, edges }; return { nodes, edges };
} catch (error) { } catch (error) {
console.error(error); console.error(error);