MCPcopy
hub / github.com/vitejs/vite / stringifyConsoleArg

Function stringifyConsoleArg

packages/vite/src/shared/forwardConsole.ts:189–238  ·  view source on GitHub ↗
(value: unknown)

Source from the content-addressed store, hash-verified

187}
188
189function stringifyConsoleArg(value: unknown): string {
190 if (typeof value === 'string') {
191 return value
192 }
193 if (
194 typeof value === 'number' ||
195 typeof value === 'boolean' ||
196 typeof value === 'undefined'
197 ) {
198 return String(value)
199 }
200 if (typeof value === 'symbol') {
201 return value.toString()
202 }
203 if (typeof value === 'function') {
204 return value.name ? `[Function: ${value.name}]` : '[Function]'
205 }
206 if (value instanceof Error) {
207 return value.stack || `${value.name}: ${value.message}`
208 }
209 if (typeof value === 'bigint') {
210 return `${value}n`
211 }
212
213 const seen = new WeakSet<object>()
214 try {
215 const serialized = JSON.stringify(value, (_, nested) => {
216 if (typeof nested === 'bigint') {
217 return `${nested}n`
218 }
219 if (nested instanceof Error) {
220 return {
221 name: nested.name,
222 message: nested.message,
223 stack: nested.stack,
224 }
225 }
226 if (nested && typeof nested === 'object') {
227 if (seen.has(nested)) {
228 return '[Circular]'
229 }
230 seen.add(nested)
231 }
232 return nested
233 })
234 return serialized ?? String(value)
235 } catch {
236 return String(value)
237 }
238}

Callers 1

formatConsoleArgsFunction · 0.85

Calls 2

hasMethod · 0.80
addMethod · 0.80

Tested by

no test coverage detected