| 166 | const seen = new WeakSet(); |
| 167 | |
| 168 | const stringify = (value: any, depth: number): any => { |
| 169 | if (value === null) return null; |
| 170 | if (value === undefined) return undefined; |
| 171 | if (typeof value !== 'object') return value; |
| 172 | |
| 173 | if (depth >= maxDepth) { |
| 174 | return '[Max Depth Reached]'; |
| 175 | } |
| 176 | |
| 177 | if (seen.has(value)) { |
| 178 | return '[Circular]'; |
| 179 | } |
| 180 | |
| 181 | seen.add(value); |
| 182 | |
| 183 | if (Array.isArray(value)) { |
| 184 | const result = value.map((item) => stringify(item, depth + 1)); |
| 185 | seen.delete(value); |
| 186 | return result; |
| 187 | } |
| 188 | |
| 189 | const result: any = {}; |
| 190 | for (const key in value) { |
| 191 | if (Object.prototype.hasOwnProperty.call(value, key)) { |
| 192 | result[key] = stringify(value[key], depth + 1); |
| 193 | } |
| 194 | } |
| 195 | seen.delete(value); |
| 196 | return result; |
| 197 | }; |
| 198 | |
| 199 | return JSON.stringify(stringify(obj, 0)); |
| 200 | } |