Skip to content

Commit fa0374c

Browse files
committed
Improve display
1 parent e7ee0ff commit fa0374c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

demo/getTreeItem.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const getTreeItem = (value) => {
4141

4242
nodes.append(
4343
typeof value === 'symbol'
44-
? value.description
44+
? escapeSymbol(value.description)
4545
: typeof value === 'string'
4646
? `"${value}"`
4747
: value,
@@ -66,15 +66,21 @@ const printListItem = (list, depth = -1) =>
6666
? ''
6767
: !Array.isArray(list)
6868
? '. ' + print(list, depth)
69-
: print(list[0], depth) + ' ' + printListItem(list[1], depth).trim()
69+
: (print(list[0], depth) + ' ' + printListItem(list[1], depth)).trim()
7070

7171
const print = (value, depth = -1) =>
7272
typeof value === 'symbol'
73-
? value.description
73+
? escapeSymbol(value.description)
7474
: typeof value === 'string'
7575
? `"${value}"`
7676
: Array.isArray(value)
7777
? depth === 0
7878
? '(...)'
7979
: '(' + printListItem(value, depth - 1) + ')'
8080
: value
81+
82+
const symbolRequiresEscape = (name) =>
83+
name.match(/[ '"]|^([+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?)$/) !== null
84+
85+
const escapeSymbol = (name) =>
86+
symbolRequiresEscape(name) ? `|${name.replaceAll('|', '\\|')}|` : name

0 commit comments

Comments
 (0)