Skip to content

Commit 42b5370

Browse files
committed
Fix to align large indices
Closes GH-18.
1 parent 56f6980 commit 42b5370

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,22 @@ export function inspectColor(tree, options = {}) {
8686
* @returns {string}
8787
*/
8888
function inspectNodes(nodes) {
89+
const size = String(nodes.length - 1).length
8990
/** @type {Array<string>} */
9091
const result = []
9192
let index = -1
9293

9394
while (++index < nodes.length) {
9495
result.push(
95-
dim((index < nodes.length - 1 ? '├' : '└') + '─' + index) +
96+
dim(
97+
(index < nodes.length - 1 ? '├' : '└') +
98+
'─' +
99+
String(index).padEnd(size)
100+
) +
96101
' ' +
97102
indent(
98103
inspectValue(nodes[index]),
99-
(index < nodes.length - 1 ? dim('│') : ' ') + ' ',
104+
(index < nodes.length - 1 ? dim('│') : ' ') + ' '.repeat(size + 2),
100105
true
101106
)
102107
)

test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,43 @@ test('inspect()', (t) => {
6464
st.end()
6565
})
6666

67+
t.equal(
68+
strip(
69+
inspect(
70+
Array.from({length: 11}).map((/** @type {undefined} */ d, i) => ({
71+
type: 'text',
72+
value: String(i),
73+
data: {id: String.fromCodePoint(97 + i)}
74+
}))
75+
)
76+
),
77+
[
78+
'├─0 text "0"',
79+
'│ data: {"id":"a"}',
80+
'├─1 text "1"',
81+
'│ data: {"id":"b"}',
82+
'├─2 text "2"',
83+
'│ data: {"id":"c"}',
84+
'├─3 text "3"',
85+
'│ data: {"id":"d"}',
86+
'├─4 text "4"',
87+
'│ data: {"id":"e"}',
88+
'├─5 text "5"',
89+
'│ data: {"id":"f"}',
90+
'├─6 text "6"',
91+
'│ data: {"id":"g"}',
92+
'├─7 text "7"',
93+
'│ data: {"id":"h"}',
94+
'├─8 text "8"',
95+
'│ data: {"id":"i"}',
96+
'├─9 text "9"',
97+
'│ data: {"id":"j"}',
98+
'└─10 text "10"',
99+
' data: {"id":"k"}'
100+
].join('\n'),
101+
'should align and indent large numbers'
102+
)
103+
67104
t.equal(
68105
strip(
69106
inspect({

0 commit comments

Comments
 (0)