19
return items[0];
20
}
22
>
let i = 0;
23
>
/**
24
>
* Reads nodes of same height and concatenates them to a single node.
25
>
*/
26
>
function readNode(): AstNode | null {
27
>
if (i >= items.length) {
28
>
return null;
29
>
}
30
>
const start = i;
31
>
const height = items[start].listHeight;
32
>
33
>
i++;
34
>
while (i < items.length && items[i].listHeight === height) {
35
i++;
36
}
38
>
if (i - start >= 2) {
39
return concat23TreesOfSameHeight(start === 0 && i === items.length ? items : items.slice(start, i), false);
41
return items[start];
42
}
44
>
45
>
// The items might not have the same height.
46
>
// We merge all items by using a binary concat operator.
47
>
let first = readNode()!; // There must be a first item
48
>
let second = readNode();
49
>
if (!second) {
50
return first;
51
}