Skip to content

Commit

Permalink
chore: handle undefined index accesses
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienIzz committed Sep 20, 2024
1 parent 1d07426 commit 6d945cd
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ const getMinSizeOfItems = <T>({
return itemSize;
}

let minSize = itemSize(data[0]); // Initialize with the size of the first item
let minSize: number | undefined = undefined;

for (let i = 1; i < data.length; i++) {
const currentSize = itemSize(data[i]);
if (currentSize < minSize) {
minSize = currentSize;
for (let i = 0; i < data.length; i++) {
const item = data[i];
if (item !== undefined) {
const currentSize = itemSize(item);
if (minSize === undefined || currentSize < minSize) {
minSize = currentSize;
}
}
}

return minSize;
};

export const getNumberOfItemsVisibleOnScreen = <T>({
data,
listSizeInPx,
Expand All @@ -30,5 +34,10 @@ export const getNumberOfItemsVisibleOnScreen = <T>({
itemSize: number | ((item: T) => number);
}) => {
const itemSizeToComputeRanges = getMinSizeOfItems({ data, itemSize });

if (!itemSizeToComputeRanges) {
return 0;
}

return Math.floor(listSizeInPx / itemSizeToComputeRanges);
};

0 comments on commit 6d945cd

Please sign in to comment.