|
@@ -1,88 +1,6 @@
|
|
-export function transform(source = [], result = [], nodeInfo = {}) {
|
|
|
|
- let { id = 0, parent = null, pAggregate = null, edgeItems = [] } = nodeInfo;
|
|
|
|
-
|
|
|
|
- const createEdgeItem = (id, pid) => ({
|
|
|
|
- id: `e${id}-${pid}`,
|
|
|
|
- source: id.toString(),
|
|
|
|
- target: pid.toString(),
|
|
|
|
- });
|
|
|
|
-
|
|
|
|
- if (Array.isArray(source)) {
|
|
|
|
- result.push(
|
|
|
|
- ...source.flatMap((item, idx) =>
|
|
|
|
- parser(item, [], {
|
|
|
|
- id: (id + idx).toString(),
|
|
|
|
- parent,
|
|
|
|
- pAggregate,
|
|
|
|
- edgeItems,
|
|
|
|
- })
|
|
|
|
- )
|
|
|
|
- );
|
|
|
|
- } else {
|
|
|
|
- Object.entries(source).forEach(([key, value], idx) => {
|
|
|
|
- const dataNode = {};
|
|
|
|
-
|
|
|
|
- if (value && (Array.isArray(value) || typeof value === "object")) {
|
|
|
|
- // every object's iterable property is supposed
|
|
|
|
- // to be created as an own parent entity.
|
|
|
|
-
|
|
|
|
- result.push(
|
|
|
|
- Object.assign(dataNode, {
|
|
|
|
- id: (++id).toString(),
|
|
|
|
- data: { label: key },
|
|
|
|
- position: { x: 0, y: 0 },
|
|
|
|
- type: "special",
|
|
|
|
- })
|
|
|
|
- );
|
|
|
|
- if (parent !== null) {
|
|
|
|
- edgeItems.push(createEdgeItem(id, parent.id));
|
|
|
|
- }
|
|
|
|
- parent = dataNode;
|
|
|
|
-
|
|
|
|
- result.push(
|
|
|
|
- ...parser(value, [], {
|
|
|
|
- id,
|
|
|
|
- parent,
|
|
|
|
- pAggregate,
|
|
|
|
- edgeItems,
|
|
|
|
- })
|
|
|
|
- );
|
|
|
|
- } else {
|
|
|
|
- if (idx === 0) {
|
|
|
|
- // every object's first property (though not iterable)
|
|
|
|
- // is supposed to be created as an own parent entity.
|
|
|
|
-
|
|
|
|
- result.push(
|
|
|
|
- Object.assign(dataNode, {
|
|
|
|
- id: (++id).toString(),
|
|
|
|
- data: { label: { [key]: value } },
|
|
|
|
- position: { x: 0, y: 0 },
|
|
|
|
- type: "special",
|
|
|
|
- })
|
|
|
|
- );
|
|
|
|
- if (parent !== null) {
|
|
|
|
- edgeItems.push(createEdgeItem(id, parent.id));
|
|
|
|
- }
|
|
|
|
- pAggregate = parent = dataNode;
|
|
|
|
- } else {
|
|
|
|
- // aggreagat all non interable properties at
|
|
|
|
- // most recent, recursion-free parent level.
|
|
|
|
-
|
|
|
|
- pAggregate.data.label[key] = value;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- if (parent === null) {
|
|
|
|
- // append all additionally collected edge items
|
|
|
|
- // in the end of all the recursion.
|
|
|
|
-
|
|
|
|
- result.push(...edgeItems);
|
|
|
|
- }
|
|
|
|
- return result;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
export const parser = (input) => {
|
|
export const parser = (input) => {
|
|
|
|
+ if (!Array.isArray(input)) input = [input];
|
|
|
|
+
|
|
const extract = (
|
|
const extract = (
|
|
os,
|
|
os,
|
|
nextId = (
|
|
nextId = (
|
|
@@ -90,7 +8,7 @@ export const parser = (input) => {
|
|
String(++id)
|
|
String(++id)
|
|
)(0)
|
|
)(0)
|
|
) =>
|
|
) =>
|
|
- os.map((o) => ({
|
|
|
|
|
|
+ [os].flat().map((o) => ({
|
|
id: nextId(),
|
|
id: nextId(),
|
|
data: {
|
|
data: {
|
|
label: Object.fromEntries(
|
|
label: Object.fromEntries(
|
|
@@ -100,7 +18,7 @@ export const parser = (input) => {
|
|
position: { x: 0, y: 0 },
|
|
position: { x: 0, y: 0 },
|
|
type: "special",
|
|
type: "special",
|
|
children: Object.entries(o)
|
|
children: Object.entries(o)
|
|
- .filter(([k, v]) => Array.isArray(v))
|
|
|
|
|
|
+ .filter(([k, v]) => Array.isArray(v) || typeof v === "object")
|
|
.flatMap(([k, v]) => [
|
|
.flatMap(([k, v]) => [
|
|
{
|
|
{
|
|
id: nextId(),
|
|
id: nextId(),
|