forked from AykutSarac/jsoncrack.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonParser.ts
155 lines (131 loc) · 3.52 KB
/
jsonParser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import toast from "react-hot-toast";
const calculateSize = (
text: string | [string, string][],
isParent = false,
isExpanded: boolean
) => {
let value = "";
if (typeof text === "string") value = text;
else value = text.map(([k, v]) => `${k}: ${v}`).join("\n");
const lineCount = value.split("\n");
const lineLengths = lineCount.map(line => line.length);
const longestLine = lineLengths.sort((a, b) => b - a)[0];
const getWidth = () => {
if (isExpanded) return 35 + longestLine * 8 + (isParent ? 60 : 0);
if (isParent) return 150;
return 200;
};
const getHeight = () => {
if (lineCount.length * 17.8 < 30) return 40;
return (lineCount.length + 1) * 18;
};
return {
width: getWidth(),
height: getHeight(),
};
};
const filterChild = ([_, v]) => {
const isNull = v === null;
const isArray = Array.isArray(v) && v.length;
const isObject = v instanceof Object;
return !isNull && (isArray || isObject);
};
const filterValues = ([k, v]) => {
if (Array.isArray(v) || v instanceof Object) return false;
return true;
};
function generateChildren(object: Object, isExpanded = true, nextId: () => string) {
if (!(object instanceof Object)) object = [object];
return Object.entries(object)
.filter(filterChild)
.flatMap(([key, v]) => {
const { width, height } = calculateSize(key, true, isExpanded);
const children = extract(v, isExpanded, nextId);
return [
{
id: nextId(),
text: key,
children,
width,
height,
data: {
isParent: true,
hasChild: !!children.length,
},
},
];
});
}
function generateNodeData(object: Object) {
if (object instanceof Object) {
const entries = Object.entries(object).filter(filterValues);
return entries;
}
return String(object);
}
const extract = (
os: string[] | object[] | null,
isExpanded = true,
nextId = (
id => () =>
String(++id)
)(0)
) => {
if (!os) return [];
return [os].flat().map(o => {
const text = generateNodeData(o);
const { width, height } = calculateSize(text, false, isExpanded);
return {
id: nextId(),
text,
width,
height,
children: generateChildren(o, isExpanded, nextId),
data: {
isParent: false,
hasChild: false,
},
};
});
};
const flatten = (xs: { id: string; children: never[] }[]) =>
xs.flatMap(({ children, ...rest }) => [rest, ...flatten(children)]);
const relationships = (xs: { id: string; children: never[] }[]) => {
return xs.flatMap(({ id: from, children = [] }) => [
...children.map(({ id: to }) => ({
id: `e${from}-${to}`,
from,
to,
})),
...relationships(children),
]);
};
export const parser = (jsonStr: string, isExpanded = true) => {
try {
let json = JSON.parse(jsonStr);
if (!Array.isArray(json)) json = [json];
const nodes: NodeData[] = [];
const edges: EdgeData[] = [];
const mappedElements = extract(json, isExpanded);
const res = [...flatten(mappedElements), ...relationships(mappedElements)];
res.forEach(data => {
if (isNode(data)) {
nodes.push(data);
} else {
edges.push(data);
}
});
return { nodes, edges };
} catch (error) {
console.error(error);
toast.error("An error occured while parsing JSON data!");
return {
nodes: [],
edges: [],
};
}
};
function isNode(element: NodeData | EdgeData) {
if ("text" in element) return true;
return false;
}