mirror of
https://github.com/AykutSarac/jsoncrack.com.git
synced 2025-01-27 15:22:56 +08:00
fix: align object's children to keep the consistency of graph illustration
This commit is contained in:
parent
debe13fef5
commit
e2765666b7
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user