mirror of
https://github.com/AykutSarac/jsoncrack.com.git
synced 2025-02-04 01:32:54 +08:00
convert parser into TS
This commit is contained in:
parent
80c97ba317
commit
8b2822221f
@ -1,16 +1,12 @@
|
|||||||
import { FlowElement } from "react-flow-renderer";
|
import { FlowElement } from "react-flow-renderer";
|
||||||
|
|
||||||
/**
|
export const parser = (input: string | string[]): FlowElement[] => {
|
||||||
* @param {never[] | Object} input
|
|
||||||
* @returns {FlowElement[]}
|
|
||||||
*/
|
|
||||||
export const parser = (input) => {
|
|
||||||
try {
|
try {
|
||||||
input = JSON.parse(input);
|
if (typeof input !== "object") input = JSON.parse(input);
|
||||||
if (!Array.isArray(input)) input = [input];
|
if (!Array.isArray(input)) input = [input];
|
||||||
|
|
||||||
const extract = (
|
const extract = (
|
||||||
os,
|
os: string[] | object[] | null,
|
||||||
nextId = (
|
nextId = (
|
||||||
(id) => () =>
|
(id) => () =>
|
||||||
String(++id)
|
String(++id)
|
||||||
@ -43,8 +39,8 @@ export const parser = (input) => {
|
|||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
const relationships = (xs) =>
|
const relationships = (xs: { id: string; children: never[] }[]) => {
|
||||||
xs.flatMap(({ id: target, children = [] }) => [
|
return xs.flatMap(({ id: target, children = [] }) => [
|
||||||
...children.map(({ id: source }) => ({
|
...children.map(({ id: source }) => ({
|
||||||
id: `e${source}-${target}`,
|
id: `e${source}-${target}`,
|
||||||
source,
|
source,
|
||||||
@ -52,15 +48,16 @@ export const parser = (input) => {
|
|||||||
})),
|
})),
|
||||||
...relationships(children),
|
...relationships(children),
|
||||||
]);
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
const flatten = (xs) =>
|
const flatten = (xs: { id: string; children: never[] }[]) =>
|
||||||
xs.flatMap(({ children, ...rest }) => [rest, ...flatten(children)]);
|
xs.flatMap(({ children, ...rest }) => [rest, ...flatten(children)]);
|
||||||
|
|
||||||
const res = extract(input);
|
const res = extract(input);
|
||||||
|
|
||||||
return [...flatten(res), ...relationships(res)];
|
return [...flatten(res), ...relationships(res)];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("An error occured while parsin JSON data!", error.stack);
|
console.error("An error occured while parsin JSON data!");
|
||||||
return Array;
|
return [];
|
||||||
}
|
}
|
||||||
};
|
};
|
@ -17,6 +17,6 @@
|
|||||||
"incremental": true,
|
"incremental": true,
|
||||||
"noImplicitAny": false
|
"noImplicitAny": false
|
||||||
},
|
},
|
||||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "src/utils/json-editor-parser.js"],
|
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
|
||||||
"exclude": ["node_modules"]
|
"exclude": ["node_modules"]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user