Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils): respect all flags in format function #3695

Merged
merged 2 commits into from Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions packages/utils/src/display.ts
Expand Up @@ -18,13 +18,13 @@ interface LoupeOptions {
truncate?: number
}

const formatRegExp = /%[sdj%]/g
const formatRegExp = /%[sdjifoOcj%]/g

export function format(...args: unknown[]) {
if (typeof args[0] !== 'string') {
const objects = []
for (let i = 0; i < args.length; i++)
objects.push(inspect(args[i]))
objects.push(inspect(args[i], { depth: 0, colors: false, compact: 3 }))
return objects.join(' ')
}

Expand Down Expand Up @@ -62,13 +62,26 @@ export function format(...args: unknown[]) {
case '%f': return Number.parseFloat(String(args[i++])).toString()
case '%o': return inspect(args[i++], { showHidden: true, showProxy: true })
case '%O': return inspect(args[i++])
case '%c': return ''
case '%c': {
i++
return ''
}
case '%j':
try {
return JSON.stringify(args[i++])
}
catch (_) {
return '[Circular]'
catch (err: any) {
const m = err.message
if (
// chromium
m.includes('circular structure')
// safari
|| m.includes('cyclic structures')
// firefox
|| m.includes('cyclic object')
)
return '[Circular]'
throw err
}
default:
return x
Expand Down