Skip to content

Commit

Permalink
fix: prevent array from creating invalid length (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredLunde authored Apr 25, 2022
1 parent 0646312 commit f26f228
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class Tree<NodeData = {}> {
if (nodeToRemoveParent?.nodes) {
let found = 0;
const nextNodes: number[] = new Array(
nodeToRemoveParent.nodes.length - 1
Math.max(nodeToRemoveParent.nodes.length - 1, 0)
);

for (let i = 0; i < nodeToRemoveParent.nodes.length; i++) {
Expand Down Expand Up @@ -205,7 +205,9 @@ export class Tree<NodeData = {}> {
// Parent may have changed in the meantime
if (node.parent === initialParent) {
if (initialParent?.nodes) {
const nextNodes: number[] = new Array(initialParent.nodes.length - 1);
const nextNodes: number[] = new Array(
Math.max(initialParent.nodes.length - 1, 0)
);
let found = 0;

for (let i = 0; i < initialParent.nodes.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/use-virtualize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export function useVirtualize<Meta>(
Math.ceil((scrollPosition.scrollTop + overscan) / totalNodeHeight)
);
const length = stopIndex - index;
const children: React.ReactElement[] = new Array(length);
const children: React.ReactElement[] = new Array(Math.max(length, 0));

for (; index < stopIndex; index++) {
const nodeId = visibleNodes[index];
Expand Down

0 comments on commit f26f228

Please sign in to comment.