Skip to content

Commit

Permalink
Slight improvement to readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
sapierens committed Jan 2, 2018
1 parent 4b9ff35 commit 5dfba6e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
8 changes: 2 additions & 6 deletions src/lazyload-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ export function isVisible(element: HTMLElement, threshold = 0, _window: Window,

if (scrollContainer) {
const scrollContainerBounds = Rect.fromElement(scrollContainer);
if (scrollContainerBounds.intersectsWith(windowBounds)) {
const intersection = scrollContainerBounds.getIntersectionWith(windowBounds);
return elementBounds.intersectsWith(intersection);
} else {
return false;
}
const intersection = scrollContainerBounds.getIntersectionWith(windowBounds);
return elementBounds.intersectsWith(intersection);
} else {
return elementBounds.intersectsWith(windowBounds);
}
Expand Down
8 changes: 7 additions & 1 deletion src/rect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export class Rect {
static empty: Rect = new Rect(0, 0, 0, 0);

left: number;
top: number;
right: number;
Expand Down Expand Up @@ -40,6 +42,10 @@ export class Rect {
const right = Math.min(this.right, rect.right);
const bottom = Math.min(this.bottom, rect.bottom);

return new Rect(left, top, right, bottom);
if (right >= left && bottom >= top) {
return new Rect(left, top, right, bottom);
} else {
return Rect.empty;
}
}
}
15 changes: 15 additions & 0 deletions test/rect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,5 +342,20 @@ describe('Rect', () => {
is(result.bottom, 20);
is(result.left, 10);
});

it('Should return an empty Rect if two Rect\'s don\'t intersect', () => {
// Arrange
const rectA = new Rect(0, 0, 20, 20);
const rectB = new Rect(30, 30, 50, 50);

// Act
const result = rectA.getIntersectionWith(rectB);

// Assert
is(result.top, 0);
is(result.right, 0);
is(result.bottom, 0);
is(result.left, 0);
});
});
});

0 comments on commit 5dfba6e

Please sign in to comment.