mirror of https://github.com/vuejs/core.git
fix(shared): handle more Symbol cases in toDisplayString
This commit is contained in:
parent
364821d6bd
commit
983d45d4f8
|
@ -181,10 +181,10 @@ describe('toDisplayString', () => {
|
||||||
])
|
])
|
||||||
expect(toDisplayString(m)).toMatchInlineSnapshot(`
|
expect(toDisplayString(m)).toMatchInlineSnapshot(`
|
||||||
"{
|
"{
|
||||||
\\"Map(3)\\": {
|
"Map(3)": {
|
||||||
\\"Symbol(0) =>\\": \\"foo\\",
|
"Symbol(0) =>": "foo",
|
||||||
\\"Symbol(1) =>\\": \\"bar\\",
|
"Symbol(1) =>": "bar",
|
||||||
\\"Symbol(baz) =>\\": \\"baz\\"
|
"Symbol(baz) =>": "baz"
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
`)
|
`)
|
||||||
|
@ -193,4 +193,27 @@ describe('toDisplayString', () => {
|
||||||
String(Symbol('foo'))
|
String(Symbol('foo'))
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('Set with Symbol values', () => {
|
||||||
|
const s = new Set([Symbol('foo'), Symbol('bar'), Symbol()])
|
||||||
|
expect(toDisplayString(s)).toMatchInlineSnapshot(`
|
||||||
|
"{
|
||||||
|
"Set(3)": [
|
||||||
|
"Symbol(foo)",
|
||||||
|
"Symbol(bar)",
|
||||||
|
"Symbol()"
|
||||||
|
]
|
||||||
|
}"
|
||||||
|
`)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Object with Symbol values', () => {
|
||||||
|
expect(toDisplayString({ foo: Symbol('x'), bar: Symbol() }))
|
||||||
|
.toMatchInlineSnapshot(`
|
||||||
|
"{
|
||||||
|
"foo": "Symbol(x)",
|
||||||
|
"bar": "Symbol()"
|
||||||
|
}"
|
||||||
|
`)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -34,9 +34,7 @@ const replacer = (_key: string, val: any): any => {
|
||||||
return {
|
return {
|
||||||
[`Map(${val.size})`]: [...val.entries()].reduce(
|
[`Map(${val.size})`]: [...val.entries()].reduce(
|
||||||
(entries, [key, val], i) => {
|
(entries, [key, val], i) => {
|
||||||
entries[
|
entries[stringiySymbol(key, i) + ' =>'] = val
|
||||||
`${isSymbol(key) ? `Symbol(${key.description ?? i})` : key} =>`
|
|
||||||
] = val
|
|
||||||
return entries
|
return entries
|
||||||
},
|
},
|
||||||
{} as Record<string, any>
|
{} as Record<string, any>
|
||||||
|
@ -44,10 +42,16 @@ const replacer = (_key: string, val: any): any => {
|
||||||
}
|
}
|
||||||
} else if (isSet(val)) {
|
} else if (isSet(val)) {
|
||||||
return {
|
return {
|
||||||
[`Set(${val.size})`]: [...val.values()]
|
[`Set(${val.size})`]: [...val.values()].map(v => stringiySymbol(v))
|
||||||
}
|
}
|
||||||
|
} else if (isSymbol(val)) {
|
||||||
|
return stringiySymbol(val)
|
||||||
} else if (isObject(val) && !isArray(val) && !isPlainObject(val)) {
|
} else if (isObject(val) && !isArray(val) && !isPlainObject(val)) {
|
||||||
|
// native elements
|
||||||
return String(val)
|
return String(val)
|
||||||
}
|
}
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const stringiySymbol = (v: unknown, i: number | string = ''): any =>
|
||||||
|
isSymbol(v) ? `Symbol(${v.description ?? i})` : v
|
||||||
|
|
Loading…
Reference in New Issue