mirror of https://github.com/vuejs/core.git
fix(shared): handle Map with symbol keys in toDisplayString (#9731)
close #9727
This commit is contained in:
parent
5b002869c5
commit
364821d6bd
|
@ -171,4 +171,26 @@ describe('toDisplayString', () => {
|
||||||
}"
|
}"
|
||||||
`)
|
`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
//#9727
|
||||||
|
test('Map with Symbol keys', () => {
|
||||||
|
const m = new Map<any, any>([
|
||||||
|
[Symbol(), 'foo'],
|
||||||
|
[Symbol(), 'bar'],
|
||||||
|
[Symbol('baz'), 'baz']
|
||||||
|
])
|
||||||
|
expect(toDisplayString(m)).toMatchInlineSnapshot(`
|
||||||
|
"{
|
||||||
|
\\"Map(3)\\": {
|
||||||
|
\\"Symbol(0) =>\\": \\"foo\\",
|
||||||
|
\\"Symbol(1) =>\\": \\"bar\\",
|
||||||
|
\\"Symbol(baz) =>\\": \\"baz\\"
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`)
|
||||||
|
// confirming the symbol renders Symbol(foo)
|
||||||
|
expect(toDisplayString(new Map([[Symbol('foo'), 'foo']]))).toContain(
|
||||||
|
String(Symbol('foo'))
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -6,7 +6,8 @@ import {
|
||||||
isPlainObject,
|
isPlainObject,
|
||||||
isSet,
|
isSet,
|
||||||
objectToString,
|
objectToString,
|
||||||
isString
|
isString,
|
||||||
|
isSymbol
|
||||||
} from './general'
|
} from './general'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,10 +32,15 @@ const replacer = (_key: string, val: any): any => {
|
||||||
return replacer(_key, val.value)
|
return replacer(_key, val.value)
|
||||||
} else if (isMap(val)) {
|
} else if (isMap(val)) {
|
||||||
return {
|
return {
|
||||||
[`Map(${val.size})`]: [...val.entries()].reduce((entries, [key, val]) => {
|
[`Map(${val.size})`]: [...val.entries()].reduce(
|
||||||
;(entries as any)[`${key} =>`] = val
|
(entries, [key, val], i) => {
|
||||||
return entries
|
entries[
|
||||||
}, {})
|
`${isSymbol(key) ? `Symbol(${key.description ?? i})` : key} =>`
|
||||||
|
] = val
|
||||||
|
return entries
|
||||||
|
},
|
||||||
|
{} as Record<string, any>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} else if (isSet(val)) {
|
} else if (isSet(val)) {
|
||||||
return {
|
return {
|
||||||
|
|
Loading…
Reference in New Issue