fix: align object's children to keep the consistency of graph illustration

This commit is contained in:
hanbin9775 2022-11-25 01:09:19 +09:00 committed by 김한빈/FE서비스팀
parent debe13fef5
commit e2765666b7

View File

@ -1,4 +1,4 @@
import { Node, parseTree } from "jsonc-parser";
import { Node, NodeType, parseTree } from "jsonc-parser";
const calculateSize = (
text: string | [string, string][],
@ -74,6 +74,25 @@ export const parser = (jsonStr: string, isFolded = false) => {
];
};
const isPrimitiveOrNullType = (type?: NodeType) => {
return (
type === "boolean" ||
type === "string" ||
type === "number" ||
type === "null"
);
};
const alignChildren = (a: Node, b: Node) => {
if (
isPrimitiveOrNullType(a?.children?.[1]?.type) &&
!isPrimitiveOrNullType(b?.children?.[1]?.type)
) {
return -1;
}
return 0;
};
let parentName: string = "";
let bracketOpen: { id: string; type: string }[] = [];
let objectsFromArray: number[] = [];
@ -203,7 +222,8 @@ export const parser = (jsonStr: string, isFolded = false) => {
} else if (parentType === "array") {
objectsFromArray = [...objectsFromArray, objectsFromArrayId++];
}
children.forEach((branch, index, array) => {
(type === "object" ? children.sort(alignChildren) : children).forEach(
(branch, index, array) => {
if (array[index + 1]) {
traverse(
branch,
@ -222,7 +242,8 @@ export const parser = (jsonStr: string, isFolded = false) => {
: undefined
);
}
});
}
);
if (type !== "property") {
// when children end