✨ Innovative and open-source visualization application that transforms various data formats, such as JSON, YAML, XML and CSV into interactive graphs.
optimize parsing algorithm
AykutSarac9d3549b88b98
diff --git a/src/utils/json-editor-parser.ts b/src/utils/json-editor-parser.tsindex 7c50781f..366d1e8d 100644--- a/src/utils/json-editor-parser.ts+++ b/src/utils/json-editor-parser.ts@@ -1,63 +1,65 @@import toast from "react-hot-toast";-export const parser = (input: string | string[]) => {- try {- if (typeof input !== "object") input = JSON.parse(input);- if (!Array.isArray(input)) input = [input];+const extract = (+ os: string[] | object[] | null,+ nextId = (+ (id) => () =>+ String(++id)+ )(0)+) => {+ if (!os) return {};+ return [os].flat().map((o) => {+ const isObject = o instanceof Object;- const extract = (- os: string[] | object[] | null,- nextId = (- (id) => () =>- String(++id)- )(0)- ) => {- if (!os) return [];+ return {+ id: nextId(),+ text: !isObject+ ? o.toString()+ : Object.fromEntries(+ Object.entries(o).filter(+ ([k, v]) => !Array.isArray(v) && !(v instanceof Object)+ )+ ),+ parent: false,+ children: Object.entries(o)+ .filter(([k, v]) => Array.isArray(v) || typeof v === "object")+ .flatMap(([k, v]) => [+ {+ id: nextId(),+ text: k,+ parent: true,+ children: extract(v, nextId),+ },+ ]),+ };+ });+};- return [os].flat().map((o) => {- const isObject = o instanceof Object;+const flatten = (xs: { id: string; children: never[] }[]) =>+ xs.flatMap(({ children, ...rest }) => [rest, ...flatten(children)]);- return {- id: nextId(),- text: !isObject- ? o.toString()- : Object.fromEntries(- Object.entries(o).filter(- ([k, v]) => !Array.isArray(v) && !(v instanceof Object)- )- ),- parent: false,- children: Object.entries(o)- .filter(([k, v]) => Array.isArray(v) || typeof v === "object")- .flatMap(([k, v]) => [- {- id: nextId(),- text: k,- parent: true,- children: extract(v, nextId),- },- ]),- };- });- };+const relationships = (xs: { id: string; children: never[] }[]) => {+ return xs.flatMap(({ id: to, children = [] }) => [+ ...children.map(({ id: from }) => ({+ id: `e${from}-${to}`,+ from,+ to,+ })),+ ...relationships(children),+ ]);+};- const relationships = (xs: { id: string; children: never[] }[]) => {- return xs.flatMap(({ id: to, children = [] }) => [- ...children.map(({ id: from }) => ({- id: `e${from}-${to}`,- from,- to,- })),- ...relationships(children),- ]);- };+export const parser = (input: string | string[]) => {+ try {+ if (typeof input !== "object") input = JSON.parse(input);+ if (!Array.isArray(input)) input = [input];- const flatten = (xs: { id: string; children: never[] }[]) =>- xs.flatMap(({ children, ...rest }) => [rest, ...flatten(children)]);+ const mappedElements = extract(input);+ const res = [...flatten(mappedElements), ...relationships(mappedElements)];- const res = extract(input);+ console.log(res);- return [...flatten(res), ...relationships(res)];+ return res;} catch (error) {toast.error("An error occured while parsing JSON data!");return [];