From ab713ad9c59c51e794259216f1b39bd87a11bca2 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 23 May 2023 15:38:35 -0700 Subject: [PATCH] refactor(algo): cleanup utils (#630) --- src/algorithms/utils.test.ts | 16 ++++++-------- src/algorithms/utils.ts | 42 ++++++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/algorithms/utils.test.ts b/src/algorithms/utils.test.ts index 1673a216..f22fce37 100644 --- a/src/algorithms/utils.test.ts +++ b/src/algorithms/utils.test.ts @@ -47,9 +47,9 @@ describe("distanceBetweenPoints", () => { describe("extendPixelBounds", () => { test("is correct", () => { - const northEast = { x: 0, y: 0 } as google.maps.Point; - const southWest = { x: 0, y: 0 } as google.maps.Point; - expect(extendPixelBounds({ northEast, southWest }, 1)).toEqual({ + const northEast = new google.maps.Point(0, 0); + const southWest = new google.maps.Point(0, 0); + expect(extendPixelBounds({ northEast, southWest }, 1)).toMatchObject({ northEast: { x: 1, y: -1, @@ -64,15 +64,11 @@ describe("extendPixelBounds", () => { describe("pixelBoundsToLatLngBounds", () => { test("is correct", () => { - const northEast = { x: 1, y: 1 } as google.maps.Point; - const southWest = { x: -1, y: -1 } as google.maps.Point; + const northEast = new google.maps.Point(1, 1); + const southWest = new google.maps.Point(-1, -1); const projection = new MapCanvasProjection(); - const bounds = pixelBoundsToLatLngBounds( - { northEast, southWest }, - projection - ); + pixelBoundsToLatLngBounds({ northEast, southWest }, projection); expect(projection.fromDivPixelToLatLng).toHaveBeenCalledWith(northEast); expect(projection.fromDivPixelToLatLng).toHaveBeenCalledWith(southWest); - expect(bounds.extend).toHaveBeenCalledTimes(2); }); }); diff --git a/src/algorithms/utils.ts b/src/algorithms/utils.ts index ee24bf1a..761a69e6 100644 --- a/src/algorithms/utils.ts +++ b/src/algorithms/utils.ts @@ -16,16 +16,25 @@ import { MarkerUtils } from "../marker-utils"; +/** + * Returns the markers visible in a padded map viewport + * + * @param map + * @param mapCanvasProjection + * @param markers The list of marker to filter + * @param viewportPaddingPixels The padding in pixel + * @returns The list of markers in the padded viewport + */ export const filterMarkersToPaddedViewport = ( map: google.maps.Map, mapCanvasProjection: google.maps.MapCanvasProjection, markers: Marker[], - viewportPadding: number + viewportPaddingPixels: number ): Marker[] => { const extendedMapBounds = extendBoundsToPaddedViewport( map.getBounds(), mapCanvasProjection, - viewportPadding + viewportPaddingPixels ); return markers.filter((marker) => extendedMapBounds.contains(MarkerUtils.getPosition(marker)) @@ -33,12 +42,12 @@ export const filterMarkersToPaddedViewport = ( }; /** - * Extends a bounds by a number of pixels in each direction. + * Extends a bounds by a number of pixels in each direction */ export const extendBoundsToPaddedViewport = ( bounds: google.maps.LatLngBounds, projection: google.maps.MapCanvasProjection, - pixels: number + numPixels: number ): google.maps.LatLngBounds => { const { northEast, southWest } = latLngBoundsToPixelBounds( bounds, @@ -46,12 +55,14 @@ export const extendBoundsToPaddedViewport = ( ); const extendedPixelBounds = extendPixelBounds( { northEast, southWest }, - pixels + numPixels ); return pixelBoundsToLatLngBounds(extendedPixelBounds, projection); }; /** + * Returns the distance between 2 positions. + * * @hidden */ export const distanceBetweenPoints = ( @@ -77,6 +88,8 @@ type PixelBounds = { }; /** + * Converts a LatLng bound to pixels. + * * @hidden */ const latLngBoundsToPixelBounds = ( @@ -90,17 +103,19 @@ const latLngBoundsToPixelBounds = ( }; /** + * Extends a pixel bounds by numPixels in all directions. + * * @hidden */ export const extendPixelBounds = ( { northEast, southWest }: PixelBounds, - pixels: number + numPixels: number ): PixelBounds => { - northEast.x += pixels; - northEast.y -= pixels; + northEast.x += numPixels; + northEast.y -= numPixels; - southWest.x -= pixels; - southWest.y += pixels; + southWest.x -= numPixels; + southWest.y += numPixels; return { northEast, southWest }; }; @@ -112,8 +127,7 @@ export const pixelBoundsToLatLngBounds = ( { northEast, southWest }: PixelBounds, projection: google.maps.MapCanvasProjection ): google.maps.LatLngBounds => { - const bounds = new google.maps.LatLngBounds(); - bounds.extend(projection.fromDivPixelToLatLng(northEast)); - bounds.extend(projection.fromDivPixelToLatLng(southWest)); - return bounds; + const sw = projection.fromDivPixelToLatLng(southWest); + const ne = projection.fromDivPixelToLatLng(northEast); + return new google.maps.LatLngBounds(sw, ne); };