Skip to content

Commit

Permalink
fix(utils): respect all flags in format function (#3695)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jun 28, 2023
1 parent 726a57e commit 91e1650
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 119 deletions.
23 changes: 18 additions & 5 deletions packages/utils/src/display.ts
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 91e1650

Please sign in to comment.