Skip to content

Commit

Permalink
fix(shared): fix toDisplayString on object with null prototype (#4335)
Browse files Browse the repository at this point in the history
fix #4334
  • Loading branch information
edison1105 committed Aug 16, 2021
1 parent 6db15a2 commit 42a334e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/shared/__tests__/toDisplayString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ describe('toDisplayString', () => {
expect(toDisplayString(obj)).toBe(JSON.stringify(obj, null, 2))
const arr = [obj]
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', () => {
Expand All @@ -31,7 +34,7 @@ describe('toDisplayString', () => {
})
).toBe(JSON.stringify({ n: 1, np: 2 }, null, 2))
})

test('objects with custom toString', () => {
class TestClass {
toString() {
Expand Down
5 changes: 4 additions & 1 deletion packages/shared/src/toDisplayString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
isArray,
isMap,
isObject,
isFunction,
isPlainObject,
isSet,
objectToString
Expand All @@ -14,7 +15,9 @@ import {
export const toDisplayString = (val: unknown): string => {
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)
: String(val)
}
Expand Down

0 comments on commit 42a334e

Please sign in to comment.