Skip to content

Commit 0583f06

Browse files
authored
fix: handle Symbol values in format utility (#9658)
1 parent 79520d8 commit 0583f06

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

packages/utils/src/display.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ function baseFormat(args: unknown[], options: FormatOptions = {}): string {
177177
if (typeof value === 'bigint') {
178178
return `${value.toString()}n`
179179
}
180+
if (typeof value === 'symbol') {
181+
return 'NaN'
182+
}
180183
return Number(value).toString()
181184
}
182185
case '%i': {
@@ -221,7 +224,7 @@ function baseFormat(args: unknown[], options: FormatOptions = {}): string {
221224

222225
for (let x = args[i]; i < len; x = args[++i]) {
223226
if (x === null || typeof x !== 'object') {
224-
str += ` ${x}`
227+
str += ` ${typeof x === 'symbol' ? x.toString() : x}`
225228
}
226229
else {
227230
str += ` ${formatArg(x)}`

test/core/test/utils-display.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,39 @@ describe('format', () => {
2727
}
2828
})('string value'),
2929
],
30+
['%s', Symbol('test')],
3031
['%d', 100],
3132
['%d', 100n],
3233
['%d', null],
3334
['%d', {}],
3435
['%d', {}, 'next'],
36+
['%d', Symbol('test')],
3537
['%i', 100],
3638
['%i', 100n],
3739
['%i', null],
3840
['%i', {}],
3941
['%i', {}, 'next'],
42+
['%i', Symbol('test')],
4043
['%f', 100],
4144
['%f', 100n],
4245
['%f', null],
4346
['%f', {}],
4447
['%f', {}, 'next'],
48+
['%f', Symbol('test')],
4549
['%o', 'string'],
4650
['%o', 100],
4751
['%o', 100n],
4852
['%o', null],
4953
['%o', {}],
5054
['%o', {}, 'next'],
55+
['%o', Symbol('test')],
5156
['%O', 'string'],
5257
['%O', 100],
5358
['%O', 100n],
5459
['%O', null],
5560
['%O', {}],
5661
['%O', {}, 'next'],
62+
['%O', Symbol('test')],
5763
['%c', 'css value'],
5864
['%c', 'css value', 'some other value'],
5965
['%c %f', 'css value', '100.00'],
@@ -64,7 +70,9 @@ describe('format', () => {
6470
['%j', {}, 'next'],
6571
['%j', { obj }],
6672
['%j', { fn: () => {} }],
73+
['%j', Symbol('test')],
6774
['%%', 'string'],
75+
['prefix', Symbol('test')],
6876
])('format(%s)', (formatString, ...args) => {
6977
expect(format(formatString, ...args), `failed ${formatString}`).toBe(util.format(formatString, ...args))
7078
})

0 commit comments

Comments
 (0)