Skip to content
Snippets Groups Projects
Commit fecf722b authored by Adrian Orłów's avatar Adrian Orłów
Browse files

feat(map): improve readability and tests of point offset util

parent b64ca935
No related branches found
No related tags found
2 merge requests!223reset the pin numbers before search results are fetch (so the results will be...,!36feat(map): add render components and tests
/* eslint-disable no-magic-numbers */ /* eslint-disable no-magic-numbers */
import { ZodError } from 'zod'; import { ZodError } from 'zod';
import { getPointOriginAndShifted } from './getPointOriginAndShifted'; import { getPointOffset } from './getPointOffset';
describe('getPointOriginAndShifted - util', () => { describe('getPointOffset - util', () => {
describe('when all args are valid', () => { describe('when all args are valid', () => {
const validPoint = { const validPoint = {
x: 256, x: 256,
y: 256, y: 256 * 2,
}; };
const validMapSize = { const validMapSize = {
...@@ -18,22 +18,16 @@ describe('getPointOriginAndShifted - util', () => { ...@@ -18,22 +18,16 @@ describe('getPointOriginAndShifted - util', () => {
}; };
const results = { const results = {
pointOrigin: { x: -115.2,
x: 128, y: -102.4,
y: 128,
},
pointShifted: {
x: 12.8,
y: 12.8,
},
}; };
it('should return valid point origin and shifted values', () => { 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', () => { 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', () => { ...@@ -53,7 +47,7 @@ describe('getPointOriginAndShifted - util', () => {
}; };
it('should throw error', () => { it('should throw error', () => {
expect(() => getPointOriginAndShifted(validPoint, invalidMapSize)).toThrow(ZodError); expect(() => getPointOffset(validPoint, invalidMapSize)).toThrow(ZodError);
}); });
}); });
}); });
...@@ -3,17 +3,13 @@ import { VALID_MAP_SIZE_SCHEMA } from '@/constants/map'; ...@@ -3,17 +3,13 @@ import { VALID_MAP_SIZE_SCHEMA } from '@/constants/map';
import { MapSize } from '@/redux/map/map.types'; import { MapSize } from '@/redux/map/map.types';
import { Point } from '@/types/map'; import { Point } from '@/types/map';
export const getPointOriginAndShifted = ( export const getPointOffset = (point: Point, mapSize: MapSize): Point => {
point: Point,
mapSize: MapSize,
): Record<'pointOrigin' | 'pointShifted', Point> => {
// parse throws error if map size may lead to invalid results // parse throws error if map size may lead to invalid results
VALID_MAP_SIZE_SCHEMA.parse(mapSize); VALID_MAP_SIZE_SCHEMA.parse(mapSize);
const longestSide = Math.max(mapSize.width, mapSize.height);
const minZoomShifted = mapSize.minZoom * 2 ** mapSize.minZoom; const minZoomShifted = mapSize.minZoom * 2 ** mapSize.minZoom;
const zoomFactor = const zoomFactor = longestSide / (mapSize.tileSize / minZoomShifted);
// eslint-disable-next-line no-bitwise
Math.max(mapSize.width, mapSize.height) / (mapSize.tileSize / minZoomShifted);
const pointOrigin: Point = { const pointOrigin: Point = {
x: mapSize.tileSize / 2, x: mapSize.tileSize / 2,
...@@ -25,5 +21,5 @@ export const getPointOriginAndShifted = ( ...@@ -25,5 +21,5 @@ export const getPointOriginAndShifted = (
y: point.y / zoomFactor, y: point.y / zoomFactor,
}; };
return { pointOrigin, pointShifted }; return { x: pointShifted.x - pointOrigin.x, y: pointShifted.y - pointOrigin.y };
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment