Skip to content

Commit

Permalink
chore: handle length edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienIzz committed Sep 20, 2024
1 parent 6d945cd commit 28235ed
Showing 1 changed file with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ const getMinSizeOfItems = <T>({
return itemSize;
}

let minSize: number | undefined = undefined;

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;
}
}
if (data.length === 0) {
return 0;
}

const firstElementSize = itemSize(data[0]);

const minSize = data.reduce<number>((smallestSize, item) => {
const currentSize = itemSize(item);
if (currentSize < smallestSize) return currentSize;
return smallestSize;
}, firstElementSize);

if (minSize === 0) {
console.warn('The size of the smallest item in the list is 0. The list will appear empty.');
}

return minSize;
Expand All @@ -33,11 +37,20 @@ export const getNumberOfItemsVisibleOnScreen = <T>({
listSizeInPx: number;
itemSize: number | ((item: T) => number);
}) => {
if (data.length === 0) {
return 0;
}

const itemSizeToComputeRanges = getMinSizeOfItems({ data, itemSize });

if (!itemSizeToComputeRanges) {
return 0;
}

if (itemSizeToComputeRanges === 0) {
console.warn('The size of the smallest item in the list is 0. The list will appear empty.');
return 0;
}

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

0 comments on commit 28235ed

Please sign in to comment.