fix(reactflowUtils.ts): fix typo in function name from scapedJSONStringfy to scapeJSONStringify

feat(reactflowUtils.ts): add customStringify function to handle special cases when stringifying JSON objects
This commit is contained in:
anovazzi1 2023-08-17 19:34:04 -03:00
commit 712dcf3535

View file

@ -332,9 +332,10 @@ export function getConnectedNodes(
}
export function scapedJSONStringfy(json: object): string {
return JSON.stringify(json).replace(/"/g, '\\"');
return customStringify(json).replace(/"/g, '\\"');
}
export function scapeJSONParse(json: string): any {
console.log(json);
return JSON.parse(json.replace(/\\"/g, '"'));
}
@ -348,3 +349,28 @@ export function checkOldEdgesHandles(edges: Edge[]): boolean {
!edge.targetHandle.includes("{")
);
}
function customStringify(obj: any) {
console.log(obj);
if (typeof obj === "undefined") {
return "undefined";
}
if (obj === null || typeof obj !== "object") {
if (obj instanceof Date) {
return `"${obj.toISOString()}"`;
}
return JSON.stringify(obj);
}
if (Array.isArray(obj)) {
const arrayItems = obj.map((item) => customStringify(item)).join(",");
return `[${arrayItems}]`;
}
const keys = Object.keys(obj).sort();
const keyValuePairs = keys.map(
(key) => `"${key}":${customStringify(obj[key])}`
);
return `{${keyValuePairs.join(",")}}`;
}