fix(core): Remove circular refs from Code and push msg (#5741)

* remove circular refs from code items (and lint fixes)

* cleanup

---------

* add some tests

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
Michael Auerswald
2023-03-21 15:34:30 +01:00
committed by GitHub
parent 199a91b398
commit b6d8a0f985
6 changed files with 64 additions and 12 deletions

View File

@@ -62,6 +62,31 @@ export const jsonParse = <T>(jsonString: string, options?: JSONParseOptions<T>):
}
};
type JSONStringifyOptions = {
replaceCircularRefs?: boolean;
circularRefReplacement?: string;
};
const getReplaceCircularReferencesFn = (options: JSONStringifyOptions) => {
const knownObjects = new WeakSet();
return (key: any, value: any) => {
if (typeof value === 'object' && value !== null) {
if (knownObjects.has(value)) {
return options?.circularRefReplacement ?? '[Circular Reference]';
}
knownObjects.add(value);
}
return value;
};
};
export const jsonStringify = (obj: unknown, options: JSONStringifyOptions = {}): string => {
const replacer = options?.replaceCircularRefs
? getReplaceCircularReferencesFn(options)
: undefined;
return JSON.stringify(obj, replacer);
};
export const sleep = async (ms: number): Promise<void> =>
new Promise((resolve) => {
setTimeout(resolve, ms);