Skip to content

Commit 42a334e

Browse files
authored
fix(shared): fix toDisplayString on object with null prototype (#4335)
fix #4334
1 parent 6db15a2 commit 42a334e

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

packages/shared/__tests__/toDisplayString.spec.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ describe('toDisplayString', () => {
1919
expect(toDisplayString(obj)).toBe(JSON.stringify(obj, null, 2))
2020
const arr = [obj]
2121
expect(toDisplayString(arr)).toBe(JSON.stringify(arr, null, 2))
22+
const foo = Object.create(null)
23+
foo.bar = 1
24+
expect(toDisplayString(foo)).toBe(JSON.stringify(foo, null, 2))
2225
})
2326

2427
test('refs', () => {
@@ -31,7 +34,7 @@ describe('toDisplayString', () => {
3134
})
3235
).toBe(JSON.stringify({ n: 1, np: 2 }, null, 2))
3336
})
34-
37+
3538
test('objects with custom toString', () => {
3639
class TestClass {
3740
toString() {

packages/shared/src/toDisplayString.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
isArray,
33
isMap,
44
isObject,
5+
isFunction,
56
isPlainObject,
67
isSet,
78
objectToString
@@ -14,7 +15,9 @@ import {
1415
export const toDisplayString = (val: unknown): string => {
1516
return val == null
1617
? ''
17-
: isArray(val) || (isObject(val) && val.toString === objectToString)
18+
: isArray(val) ||
19+
(isObject(val) &&
20+
(val.toString === objectToString || !isFunction(val.toString)))
1821
? JSON.stringify(val, replacer, 2)
1922
: String(val)
2023
}

0 commit comments

Comments
 (0)