Skip to content

Commit f88707c

Browse files
authored
处理循环引用的序列化
1 parent 4f35868 commit f88707c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

packages/utils/src/fun/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,20 @@ export const copyToClipboard = (text: string): Promise<void> => {
134134
}
135135
return navigator.clipboard.writeText(text);
136136
};
137+
138+
/*
139+
@description: 处理循环引用的序列化
140+
@param {obj} obj 需要序列化的对象
141+
@return {string}
142+
*/
143+
export const safeStringify = (obj: any) {
144+
const seen = new WeakSet();
145+
return JSON.stringify(obj, (k, v) => {
146+
if (typeof v === 'object' && v !== null) {
147+
if (seen.has(v)) return '[Circular]';
148+
seen.add(v);
149+
}
150+
return v;
151+
});
152+
}
153+

0 commit comments

Comments
 (0)