|
@@ -1,3 +1,6 @@
|
|
|
|
+/**
|
|
|
|
+ * Copyright (C) 2022 Aykut Saraç - All Rights Reserved
|
|
|
|
+ */
|
|
import toast from "react-hot-toast";
|
|
import toast from "react-hot-toast";
|
|
|
|
|
|
const filterChild = ([k, v]) => {
|
|
const filterChild = ([k, v]) => {
|
|
@@ -15,16 +18,33 @@ const filterValues = ([k, v]) => {
|
|
};
|
|
};
|
|
|
|
|
|
function generateChildren(object: Object, nextId: () => string) {
|
|
function generateChildren(object: Object, nextId: () => string) {
|
|
|
|
+ if (!(object instanceof Object)) object = [object];
|
|
|
|
+
|
|
return Object.entries(object)
|
|
return Object.entries(object)
|
|
.filter(filterChild)
|
|
.filter(filterChild)
|
|
- .flatMap(([k, v]) => [
|
|
|
|
- {
|
|
|
|
- id: nextId(),
|
|
|
|
- text: k,
|
|
|
|
- parent: true,
|
|
|
|
- children: extract(v, nextId),
|
|
|
|
- },
|
|
|
|
- ]);
|
|
|
|
|
|
+ .flatMap(([k, v]) => {
|
|
|
|
+ // const isObject = v instanceof Object && !Array.isArray(v);
|
|
|
|
+
|
|
|
|
+ // if (isObject) {
|
|
|
|
+ // return [
|
|
|
|
+ // {
|
|
|
|
+ // id: nextId(),
|
|
|
|
+ // text: k,
|
|
|
|
+ // parent: true,
|
|
|
|
+ // children: generateChildren(v, nextId),
|
|
|
|
+ // },
|
|
|
|
+ // ];
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+ return [
|
|
|
|
+ {
|
|
|
|
+ id: nextId(),
|
|
|
|
+ text: k,
|
|
|
|
+ parent: true,
|
|
|
|
+ children: extract(v, nextId),
|
|
|
|
+ },
|
|
|
|
+ ];
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
function generateNodeData(object: Object | number) {
|
|
function generateNodeData(object: Object | number) {
|
|
@@ -35,7 +55,7 @@ function generateNodeData(object: Object | number) {
|
|
return Object.fromEntries(entries);
|
|
return Object.fromEntries(entries);
|
|
}
|
|
}
|
|
|
|
|
|
- return object.toString();
|
|
|
|
|
|
+ return String(object);
|
|
}
|
|
}
|
|
|
|
|
|
const extract = (
|
|
const extract = (
|
|
@@ -46,6 +66,7 @@ const extract = (
|
|
)(0)
|
|
)(0)
|
|
) => {
|
|
) => {
|
|
if (!os) return [];
|
|
if (!os) return [];
|
|
|
|
+
|
|
return [os].flat().map((o) => ({
|
|
return [os].flat().map((o) => ({
|
|
id: nextId(),
|
|
id: nextId(),
|
|
text: generateNodeData(o),
|
|
text: generateNodeData(o),
|
|
@@ -70,7 +91,6 @@ const relationships = (xs: { id: string; children: never[] }[]) => {
|
|
|
|
|
|
export const parser = (input: string | string[]) => {
|
|
export const parser = (input: string | string[]) => {
|
|
try {
|
|
try {
|
|
- // if (typeof input !== "object") input = JSON.parse(input);
|
|
|
|
if (!Array.isArray(input)) input = [input];
|
|
if (!Array.isArray(input)) input = [input];
|
|
|
|
|
|
const mappedElements = extract(input);
|
|
const mappedElements = extract(input);
|
|
@@ -79,7 +99,6 @@ export const parser = (input: string | string[]) => {
|
|
return res;
|
|
return res;
|
|
} catch (error) {
|
|
} catch (error) {
|
|
console.error(error);
|
|
console.error(error);
|
|
-
|
|
|
|
toast.error("An error occured while parsing JSON data!");
|
|
toast.error("An error occured while parsing JSON data!");
|
|
return [];
|
|
return [];
|
|
}
|
|
}
|