Skip to content

Commit 58984f5

Browse files
authored
Merge pull request #2 from streamich/fix-null-ending
fix: πŸ› use correct symbol for lists ending with null
2 parents dfe6259 + 5e70fd4 commit 58984f5

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

β€Žsrc/__tests__/printTree.spec.tsβ€Ž

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,22 @@ Array
108108
└─ item 2`,
109109
);
110110
});
111+
112+
test('last child is angle, when end of list is "null"', () => {
113+
// prettier-ignore
114+
const str = 'Array' + printTree('', [
115+
(tab) => '[0]:' + printTree(tab + ' ', [
116+
(tab) => 'item 1',
117+
null,
118+
]),
119+
null,
120+
null,
121+
]);
122+
123+
expect('\n' + str).toBe(
124+
`
125+
Array
126+
└─ [0]:
127+
└─ item 1`,
128+
);
129+
});

β€Žsrc/printTree.tsβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type {PrintChild} from './types';
22

33
export const printTree = (tab = '', children: (PrintChild | null)[]): string => {
44
let str = '';
5-
const length = children.length;
6-
const last = length - 1;
7-
for (let i = 0; i < length; i++) {
5+
let last = children.length - 1;
6+
for (; last >= 0; last--) if (children[last]) break;
7+
for (let i = 0; i <= last; i++) {
88
const fn = children[i];
99
if (!fn) continue;
1010
const isLast = i === last;

0 commit comments

Comments
Β (0)