| 2 | // SPDX-License-Identifier: GPL-3.0 |
| 3 | |
| 4 | function truncateObject(value: any, currentDepth: number, maxDepth: number, seen: Set<any> = new Set()): any { |
| 5 | if (currentDepth >= maxDepth) { |
| 6 | if (Array.isArray(value)) return `[Array(${value.length})]`; |
| 7 | if (value && typeof value === 'object') return `[Object(${Object.keys(value).length})]`; |
| 8 | } |
| 9 | |
| 10 | if (value && typeof value === 'object') { |
| 11 | if (seen.has(value)) { |
| 12 | throw new Error('Converting circular structure to JSON'); |
| 13 | } |
| 14 | seen.add(value); |
| 15 | |
| 16 | const truncated: any = Array.isArray(value) ? [] : {}; |
| 17 | for (const key in value) { |
| 18 | if (Object.prototype.hasOwnProperty.call(value, key)) { |
| 19 | truncated[key] = truncateObject(value[key], currentDepth + 1, maxDepth, seen); |
| 20 | } |
| 21 | } |
| 22 | seen.delete(value); |
| 23 | return truncated; |
| 24 | } |
| 25 | |
| 26 | return value; |
| 27 | } |
| 28 | |
| 29 | export function handledStringify(obj: any, depth = 0): string { |
| 30 | try { |