(value: unknown, max = ARG_MAX)
| 60 | |
| 61 | /** JSON for a console argument that is not a string or an Error. */ |
| 62 | export function safeStringify(value: unknown, max = ARG_MAX): string { |
| 63 | if (typeof value === 'string') { |
| 64 | return value; |
| 65 | } |
| 66 | if (typeof value === 'bigint') { |
| 67 | return `${value.toString()}n`; |
| 68 | } |
| 69 | if (typeof value === 'function') { |
| 70 | return `[function ${value.name || 'anonymous'}]`; |
| 71 | } |
| 72 | if (typeof value === 'symbol') { |
| 73 | return value.toString(); |
| 74 | } |
| 75 | if (value === undefined) { |
| 76 | return 'undefined'; |
| 77 | } |
| 78 | const seen = new WeakSet<object>(); |
| 79 | let text: string; |
| 80 | try { |
| 81 | text = JSON.stringify(value, (_key, entry: unknown) => { |
| 82 | if (typeof entry === 'bigint') { |
| 83 | return `${entry.toString()}n`; |
| 84 | } |
| 85 | if (typeof entry === 'object' && entry !== null) { |
| 86 | if (seen.has(entry)) { |
| 87 | return '[circular]'; |
| 88 | } |
| 89 | seen.add(entry); |
| 90 | } |
| 91 | return entry; |
| 92 | }) ?? String(value); |
| 93 | } catch { |
| 94 | text = String(value); |
| 95 | } |
| 96 | return text.length > max ? `${text.slice(0, max)} [cut at ${max}]` : text; |
| 97 | } |
| 98 | |
| 99 | /** One line for a console call, plus the first stack among its arguments. */ |
| 100 | export function describeArgs(args: readonly unknown[]): { message: string; stack?: string } { |
no outgoing calls
no test coverage detected