From fecf722bc639d93985b68f7b91099af5b11e9505 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrian=20Or=C5=82=C3=B3w?= <adrian.orlow@fishbrain.com>
Date: Tue, 17 Oct 2023 23:17:04 +0200
Subject: [PATCH] feat(map): improve readability and tests of point offset util

---
 ...Shifted.test.ts => getPointOffset.test.ts} | 22 +++++++------------
 ...tOriginAndShifted.ts => getPointOffset.ts} | 12 ++++------
 2 files changed, 12 insertions(+), 22 deletions(-)
 rename src/utils/map/{getPointOriginAndShifted.test.ts => getPointOffset.test.ts} (60%)
 rename src/utils/map/{getPointOriginAndShifted.ts => getPointOffset.ts} (64%)

diff --git a/src/utils/map/getPointOriginAndShifted.test.ts b/src/utils/map/getPointOffset.test.ts
similarity index 60%
rename from src/utils/map/getPointOriginAndShifted.test.ts
rename to src/utils/map/getPointOffset.test.ts
index 0ba48908..2528c7dd 100644
--- a/src/utils/map/getPointOriginAndShifted.test.ts
+++ b/src/utils/map/getPointOffset.test.ts
@@ -1,12 +1,12 @@
 /* eslint-disable no-magic-numbers */
 import { ZodError } from 'zod';
-import { getPointOriginAndShifted } from './getPointOriginAndShifted';
+import { getPointOffset } from './getPointOffset';
 
-describe('getPointOriginAndShifted - util', () => {
+describe('getPointOffset - util', () => {
   describe('when all args are valid', () => {
     const validPoint = {
       x: 256,
-      y: 256,
+      y: 256 * 2,
     };
 
     const validMapSize = {
@@ -18,22 +18,16 @@ describe('getPointOriginAndShifted - util', () => {
     };
 
     const results = {
-      pointOrigin: {
-        x: 128,
-        y: 128,
-      },
-      pointShifted: {
-        x: 12.8,
-        y: 12.8,
-      },
+      x: -115.2,
+      y: -102.4,
     };
 
     it('should return valid point origin and shifted values', () => {
-      expect(getPointOriginAndShifted(validPoint, validMapSize)).toMatchObject(results);
+      expect(getPointOffset(validPoint, validMapSize)).toMatchObject(results);
     });
 
     it('should not throw error', () => {
-      expect(() => getPointOriginAndShifted(validPoint, validMapSize)).not.toThrow(ZodError);
+      expect(() => getPointOffset(validPoint, validMapSize)).not.toThrow(ZodError);
     });
   });
 
@@ -53,7 +47,7 @@ describe('getPointOriginAndShifted - util', () => {
     };
 
     it('should throw error', () => {
-      expect(() => getPointOriginAndShifted(validPoint, invalidMapSize)).toThrow(ZodError);
+      expect(() => getPointOffset(validPoint, invalidMapSize)).toThrow(ZodError);
     });
   });
 });
diff --git a/src/utils/map/getPointOriginAndShifted.ts b/src/utils/map/getPointOffset.ts
similarity index 64%
rename from src/utils/map/getPointOriginAndShifted.ts
rename to src/utils/map/getPointOffset.ts
index 00253c74..9c4e01fe 100644
--- a/src/utils/map/getPointOriginAndShifted.ts
+++ b/src/utils/map/getPointOffset.ts
@@ -3,17 +3,13 @@ import { VALID_MAP_SIZE_SCHEMA } from '@/constants/map';
 import { MapSize } from '@/redux/map/map.types';
 import { Point } from '@/types/map';
 
-export const getPointOriginAndShifted = (
-  point: Point,
-  mapSize: MapSize,
-): Record<'pointOrigin' | 'pointShifted', Point> => {
+export const getPointOffset = (point: Point, mapSize: MapSize): Point => {
   // parse throws error if map size may lead to invalid results
   VALID_MAP_SIZE_SCHEMA.parse(mapSize);
 
+  const longestSide = Math.max(mapSize.width, mapSize.height);
   const minZoomShifted = mapSize.minZoom * 2 ** mapSize.minZoom;
-  const zoomFactor =
-    // eslint-disable-next-line no-bitwise
-    Math.max(mapSize.width, mapSize.height) / (mapSize.tileSize / minZoomShifted);
+  const zoomFactor = longestSide / (mapSize.tileSize / minZoomShifted);
 
   const pointOrigin: Point = {
     x: mapSize.tileSize / 2,
@@ -25,5 +21,5 @@ export const getPointOriginAndShifted = (
     y: point.y / zoomFactor,
   };
 
-  return { pointOrigin, pointShifted };
+  return { x: pointShifted.x - pointOrigin.x, y: pointShifted.y - pointOrigin.y };
 };
-- 
GitLab