forked from vuejs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shared): support Map and Set in toDisplayString
close vuejs#1067, close vuejs#1100
- Loading branch information
Showing
3 changed files
with
136 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { toDisplayString } from '../src' | ||
|
||
describe('toDisplayString', () => { | ||
test('nullish values', () => { | ||
expect(toDisplayString(null)).toBe('') | ||
expect(toDisplayString(undefined)).toBe('') | ||
}) | ||
|
||
test('primitive values', () => { | ||
expect(toDisplayString(1)).toBe('1') | ||
expect(toDisplayString(true)).toBe('true') | ||
expect(toDisplayString(false)).toBe('false') | ||
expect(toDisplayString('hello')).toBe('hello') | ||
}) | ||
|
||
test('Object and Arrays', () => { | ||
const obj = { foo: 123 } | ||
expect(toDisplayString(obj)).toBe(JSON.stringify(obj, null, 2)) | ||
const arr = [obj] | ||
expect(toDisplayString(arr)).toBe(JSON.stringify(arr, null, 2)) | ||
}) | ||
|
||
test('native objects', () => { | ||
const div = document.createElement('div') | ||
expect(toDisplayString(div)).toBe(`"[object HTMLDivElement]"`) | ||
expect(toDisplayString({ div })).toMatchInlineSnapshot(` | ||
"{ | ||
\\"div\\": \\"[object HTMLDivElement]\\" | ||
}" | ||
`) | ||
}) | ||
|
||
test('Map and Set', () => { | ||
const m = new Map<any, any>([ | ||
[1, 'foo'], | ||
[{ baz: 1 }, { foo: 'bar', qux: 2 }] | ||
]) | ||
const s = new Set<any>([1, { foo: 'bar' }, m]) | ||
|
||
expect(toDisplayString(m)).toMatchInlineSnapshot(` | ||
"{ | ||
\\"Map(2)\\": { | ||
\\"1 =>\\": \\"foo\\", | ||
\\"[object Object] =>\\": { | ||
\\"foo\\": \\"bar\\", | ||
\\"qux\\": 2 | ||
} | ||
} | ||
}" | ||
`) | ||
expect(toDisplayString(s)).toMatchInlineSnapshot(` | ||
"{ | ||
\\"Set(3)\\": [ | ||
1, | ||
{ | ||
\\"foo\\": \\"bar\\" | ||
}, | ||
{ | ||
\\"Map(2)\\": { | ||
\\"1 =>\\": \\"foo\\", | ||
\\"[object Object] =>\\": { | ||
\\"foo\\": \\"bar\\", | ||
\\"qux\\": 2 | ||
} | ||
} | ||
} | ||
] | ||
}" | ||
`) | ||
|
||
expect( | ||
toDisplayString({ | ||
m, | ||
s | ||
}) | ||
).toMatchInlineSnapshot(` | ||
"{ | ||
\\"m\\": { | ||
\\"Map(2)\\": { | ||
\\"1 =>\\": \\"foo\\", | ||
\\"[object Object] =>\\": { | ||
\\"foo\\": \\"bar\\", | ||
\\"qux\\": 2 | ||
} | ||
} | ||
}, | ||
\\"s\\": { | ||
\\"Set(3)\\": [ | ||
1, | ||
{ | ||
\\"foo\\": \\"bar\\" | ||
}, | ||
{ | ||
\\"Map(2)\\": { | ||
\\"1 =>\\": \\"foo\\", | ||
\\"[object Object] =>\\": { | ||
\\"foo\\": \\"bar\\", | ||
\\"qux\\": 2 | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}" | ||
`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { isArray, isObject, isPlainObject } from './index' | ||
|
||
// For converting {{ interpolation }} values to displayed strings. | ||
export const toDisplayString = (val: unknown): string => { | ||
return val == null | ||
? '' | ||
: isObject(val) | ||
? JSON.stringify(val, replacer, 2) | ||
: String(val) | ||
} | ||
|
||
const replacer = (_key: string, val: any) => { | ||
if (val instanceof Map) { | ||
return { | ||
[`Map(${val.size})`]: [...val.entries()].reduce((entries, [key, val]) => { | ||
;(entries as any)[`${key} =>`] = val | ||
return entries | ||
}, {}) | ||
} | ||
} else if (val instanceof Set) { | ||
return { | ||
[`Set(${val.size})`]: [...val.values()] | ||
} | ||
} else if (isObject(val) && !isArray(val) && !isPlainObject(val)) { | ||
return String(val) | ||
} | ||
return val | ||
} |