Skip to content

Commit

Permalink
Fix to align large indices
Browse files Browse the repository at this point in the history
Closes GH-18.
  • Loading branch information
wooorm committed Jul 23, 2022
1 parent 56f6980 commit 42b5370
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,22 @@ export function inspectColor(tree, options = {}) {
* @returns {string}
*/
function inspectNodes(nodes) {
const size = String(nodes.length - 1).length
/** @type {Array<string>} */
const result = []
let index = -1

while (++index < nodes.length) {
result.push(
dim((index < nodes.length - 1 ? '├' : '└') + '─' + index) +
dim(
(index < nodes.length - 1 ? '├' : '└') +
'─' +
String(index).padEnd(size)
) +
' ' +
indent(
inspectValue(nodes[index]),
(index < nodes.length - 1 ? dim('│') : ' ') + ' ',
(index < nodes.length - 1 ? dim('│') : ' ') + ' '.repeat(size + 2),
true
)
)
Expand Down
37 changes: 37 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,43 @@ test('inspect()', (t) => {
st.end()
})

t.equal(
strip(
inspect(
Array.from({length: 11}).map((/** @type {undefined} */ d, i) => ({
type: 'text',
value: String(i),
data: {id: String.fromCodePoint(97 + i)}
}))
)
),
[
'├─0 text "0"',
'│ data: {"id":"a"}',
'├─1 text "1"',
'│ data: {"id":"b"}',
'├─2 text "2"',
'│ data: {"id":"c"}',
'├─3 text "3"',
'│ data: {"id":"d"}',
'├─4 text "4"',
'│ data: {"id":"e"}',
'├─5 text "5"',
'│ data: {"id":"f"}',
'├─6 text "6"',
'│ data: {"id":"g"}',
'├─7 text "7"',
'│ data: {"id":"h"}',
'├─8 text "8"',
'│ data: {"id":"i"}',
'├─9 text "9"',
'│ data: {"id":"j"}',
'└─10 text "10"',
' data: {"id":"k"}'
].join('\n'),
'should align and indent large numbers'
)

t.equal(
strip(
inspect({
Expand Down

0 comments on commit 42b5370

Please sign in to comment.