Skip to content

Commit f994b97

Browse files
committed
feat: unwrap refs in toDisplayString
1 parent ee4cbae commit f994b97

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Diff for: packages/shared/__tests__/toDisplayString.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { computed, ref } from '@vue/reactivity'
12
import { toDisplayString } from '../src'
23

34
describe('toDisplayString', () => {
@@ -20,6 +21,17 @@ describe('toDisplayString', () => {
2021
expect(toDisplayString(arr)).toBe(JSON.stringify(arr, null, 2))
2122
})
2223

24+
test('refs', () => {
25+
const n = ref(1)
26+
const np = computed(() => n.value + 1)
27+
expect(
28+
toDisplayString({
29+
n,
30+
np
31+
})
32+
).toBe(JSON.stringify({ n: 1, np: 2 }, null, 2))
33+
})
34+
2335
test('native objects', () => {
2436
const div = document.createElement('div')
2537
expect(toDisplayString(div)).toBe(`"[object HTMLDivElement]"`)

Diff for: packages/shared/src/toDisplayString.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ export const toDisplayString = (val: unknown): string => {
1212
: String(val)
1313
}
1414

15-
const replacer = (_key: string, val: any) => {
16-
if (isMap(val)) {
15+
const replacer = (_key: string, val: any): any => {
16+
// can't use isRef here since @vue/shared has no deps
17+
if (val && val.__v_isRef) {
18+
return replacer(_key, val.value)
19+
} else if (isMap(val)) {
1720
return {
1821
[`Map(${val.size})`]: [...val.entries()].reduce((entries, [key, val]) => {
1922
;(entries as any)[`${key} =>`] = val

0 commit comments

Comments
 (0)