mirror of https://github.com/vuejs/core.git
fix(shared): fix toDisplayString on object with null prototype (#4335)
fix #4334
This commit is contained in:
parent
6db15a27cf
commit
42a334e12e
|
@ -19,6 +19,9 @@ describe('toDisplayString', () => {
|
||||||
expect(toDisplayString(obj)).toBe(JSON.stringify(obj, null, 2))
|
expect(toDisplayString(obj)).toBe(JSON.stringify(obj, null, 2))
|
||||||
const arr = [obj]
|
const arr = [obj]
|
||||||
expect(toDisplayString(arr)).toBe(JSON.stringify(arr, null, 2))
|
expect(toDisplayString(arr)).toBe(JSON.stringify(arr, null, 2))
|
||||||
|
const foo = Object.create(null)
|
||||||
|
foo.bar = 1
|
||||||
|
expect(toDisplayString(foo)).toBe(JSON.stringify(foo, null, 2))
|
||||||
})
|
})
|
||||||
|
|
||||||
test('refs', () => {
|
test('refs', () => {
|
||||||
|
@ -31,7 +34,7 @@ describe('toDisplayString', () => {
|
||||||
})
|
})
|
||||||
).toBe(JSON.stringify({ n: 1, np: 2 }, null, 2))
|
).toBe(JSON.stringify({ n: 1, np: 2 }, null, 2))
|
||||||
})
|
})
|
||||||
|
|
||||||
test('objects with custom toString', () => {
|
test('objects with custom toString', () => {
|
||||||
class TestClass {
|
class TestClass {
|
||||||
toString() {
|
toString() {
|
||||||
|
|
|
@ -2,6 +2,7 @@ import {
|
||||||
isArray,
|
isArray,
|
||||||
isMap,
|
isMap,
|
||||||
isObject,
|
isObject,
|
||||||
|
isFunction,
|
||||||
isPlainObject,
|
isPlainObject,
|
||||||
isSet,
|
isSet,
|
||||||
objectToString
|
objectToString
|
||||||
|
@ -14,7 +15,9 @@ import {
|
||||||
export const toDisplayString = (val: unknown): string => {
|
export const toDisplayString = (val: unknown): string => {
|
||||||
return val == null
|
return val == null
|
||||||
? ''
|
? ''
|
||||||
: isArray(val) || (isObject(val) && val.toString === objectToString)
|
: isArray(val) ||
|
||||||
|
(isObject(val) &&
|
||||||
|
(val.toString === objectToString || !isFunction(val.toString)))
|
||||||
? JSON.stringify(val, replacer, 2)
|
? JSON.stringify(val, replacer, 2)
|
||||||
: String(val)
|
: String(val)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue