mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2026-02-05 10:01:24 +01:00
refactor: improve code readability by standardizing string quotes and simplifying function calls
This commit is contained in:
parent
5f5695b74c
commit
309b386919
3 changed files with 80 additions and 54 deletions
|
|
@ -1,28 +1,28 @@
|
||||||
import { expect, describe, it } from 'vitest'
|
import { describe, expect, it } from "vitest";
|
||||||
import { getLongitude, getLatitude, getCoordinates } from './commonUtils'
|
import { getCoordinates, getLatitude, getLongitude } from "./commonUtils";
|
||||||
|
|
||||||
describe('getLongitude', () => {
|
describe("getLongitude", () => {
|
||||||
const mapCoordinates = { lonW: -10, lonT: 20 };
|
const mapCoordinates = { lonW: -10, lonT: 20 };
|
||||||
const graphWidth = 1000;
|
const graphWidth = 1000;
|
||||||
|
|
||||||
it('should calculate longitude at the left edge (x=0)', () => {
|
it("should calculate longitude at the left edge (x=0)", () => {
|
||||||
expect(getLongitude(0, mapCoordinates, graphWidth, 2)).toBe(-10);
|
expect(getLongitude(0, mapCoordinates, graphWidth, 2)).toBe(-10);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should calculate longitude at the right edge (x=graphWidth)', () => {
|
it("should calculate longitude at the right edge (x=graphWidth)", () => {
|
||||||
expect(getLongitude(1000, mapCoordinates, graphWidth, 2)).toBe(10);
|
expect(getLongitude(1000, mapCoordinates, graphWidth, 2)).toBe(10);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should calculate longitude at the center (x=graphWidth/2)', () => {
|
it("should calculate longitude at the center (x=graphWidth/2)", () => {
|
||||||
expect(getLongitude(500, mapCoordinates, graphWidth, 2)).toBe(0);
|
expect(getLongitude(500, mapCoordinates, graphWidth, 2)).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should respect decimal precision', () => {
|
it("should respect decimal precision", () => {
|
||||||
// 333/1000 * 20 = 6.66, -10 + 6.66 = -3.34
|
// 333/1000 * 20 = 6.66, -10 + 6.66 = -3.34
|
||||||
expect(getLongitude(333, mapCoordinates, graphWidth, 4)).toBe(-3.34);
|
expect(getLongitude(333, mapCoordinates, graphWidth, 4)).toBe(-3.34);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle different map coordinate ranges', () => {
|
it("should handle different map coordinate ranges", () => {
|
||||||
const wideMap = { lonW: -180, lonT: 360 };
|
const wideMap = { lonW: -180, lonT: 360 };
|
||||||
expect(getLongitude(500, wideMap, graphWidth, 2)).toBe(0);
|
expect(getLongitude(500, wideMap, graphWidth, 2)).toBe(0);
|
||||||
expect(getLongitude(0, wideMap, graphWidth, 2)).toBe(-180);
|
expect(getLongitude(0, wideMap, graphWidth, 2)).toBe(-180);
|
||||||
|
|
@ -30,68 +30,109 @@ describe('getLongitude', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getLatitude', () => {
|
describe("getLatitude", () => {
|
||||||
const mapCoordinates = { latN: 60, latT: 40 };
|
const mapCoordinates = { latN: 60, latT: 40 };
|
||||||
const graphHeight = 800;
|
const graphHeight = 800;
|
||||||
|
|
||||||
it('should calculate latitude at the top edge (y=0)', () => {
|
it("should calculate latitude at the top edge (y=0)", () => {
|
||||||
expect(getLatitude(0, mapCoordinates, graphHeight, 2)).toBe(60);
|
expect(getLatitude(0, mapCoordinates, graphHeight, 2)).toBe(60);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should calculate latitude at the bottom edge (y=graphHeight)', () => {
|
it("should calculate latitude at the bottom edge (y=graphHeight)", () => {
|
||||||
expect(getLatitude(800, mapCoordinates, graphHeight, 2)).toBe(20);
|
expect(getLatitude(800, mapCoordinates, graphHeight, 2)).toBe(20);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should calculate latitude at the center (y=graphHeight/2)', () => {
|
it("should calculate latitude at the center (y=graphHeight/2)", () => {
|
||||||
expect(getLatitude(400, mapCoordinates, graphHeight, 2)).toBe(40);
|
expect(getLatitude(400, mapCoordinates, graphHeight, 2)).toBe(40);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should respect decimal precision', () => {
|
it("should respect decimal precision", () => {
|
||||||
// 60 - (333/800 * 40) = 60 - 16.65 = 43.35
|
// 60 - (333/800 * 40) = 60 - 16.65 = 43.35
|
||||||
expect(getLatitude(333, mapCoordinates, graphHeight, 4)).toBe(43.35);
|
expect(getLatitude(333, mapCoordinates, graphHeight, 4)).toBe(43.35);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle equator-centered maps', () => {
|
it("should handle equator-centered maps", () => {
|
||||||
const equatorMap = { latN: 45, latT: 90 };
|
const equatorMap = { latN: 45, latT: 90 };
|
||||||
expect(getLatitude(400, equatorMap, graphHeight, 2)).toBe(0);
|
expect(getLatitude(400, equatorMap, graphHeight, 2)).toBe(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getCoordinates', () => {
|
describe("getCoordinates", () => {
|
||||||
const mapCoordinates = { lonW: -10, lonT: 20, latN: 60, latT: 40 };
|
const mapCoordinates = { lonW: -10, lonT: 20, latN: 60, latT: 40 };
|
||||||
const graphWidth = 1000;
|
const graphWidth = 1000;
|
||||||
const graphHeight = 800;
|
const graphHeight = 800;
|
||||||
|
|
||||||
it('should return [longitude, latitude] tuple', () => {
|
it("should return [longitude, latitude] tuple", () => {
|
||||||
const result = getCoordinates(500, 400, mapCoordinates, graphWidth, graphHeight, 2);
|
const result = getCoordinates(
|
||||||
|
500,
|
||||||
|
400,
|
||||||
|
mapCoordinates,
|
||||||
|
graphWidth,
|
||||||
|
graphHeight,
|
||||||
|
2,
|
||||||
|
);
|
||||||
expect(result).toEqual([0, 40]);
|
expect(result).toEqual([0, 40]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should calculate coordinates at top-left corner', () => {
|
it("should calculate coordinates at top-left corner", () => {
|
||||||
const result = getCoordinates(0, 0, mapCoordinates, graphWidth, graphHeight, 2);
|
const result = getCoordinates(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
mapCoordinates,
|
||||||
|
graphWidth,
|
||||||
|
graphHeight,
|
||||||
|
2,
|
||||||
|
);
|
||||||
expect(result).toEqual([-10, 60]);
|
expect(result).toEqual([-10, 60]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should calculate coordinates at bottom-right corner', () => {
|
it("should calculate coordinates at bottom-right corner", () => {
|
||||||
const result = getCoordinates(1000, 800, mapCoordinates, graphWidth, graphHeight, 2);
|
const result = getCoordinates(
|
||||||
|
1000,
|
||||||
|
800,
|
||||||
|
mapCoordinates,
|
||||||
|
graphWidth,
|
||||||
|
graphHeight,
|
||||||
|
2,
|
||||||
|
);
|
||||||
expect(result).toEqual([10, 20]);
|
expect(result).toEqual([10, 20]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should respect decimal precision for both coordinates', () => {
|
it("should respect decimal precision for both coordinates", () => {
|
||||||
const result = getCoordinates(333, 333, mapCoordinates, graphWidth, graphHeight, 4);
|
const result = getCoordinates(
|
||||||
|
333,
|
||||||
|
333,
|
||||||
|
mapCoordinates,
|
||||||
|
graphWidth,
|
||||||
|
graphHeight,
|
||||||
|
4,
|
||||||
|
);
|
||||||
expect(result[0]).toBe(-3.34); // longitude
|
expect(result[0]).toBe(-3.34); // longitude
|
||||||
expect(result[1]).toBe(43.35); // latitude
|
expect(result[1]).toBe(43.35); // latitude
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use default precision of 2 decimals', () => {
|
it("should use default precision of 2 decimals", () => {
|
||||||
const result = getCoordinates(333, 333, mapCoordinates, graphWidth, graphHeight);
|
const result = getCoordinates(
|
||||||
|
333,
|
||||||
|
333,
|
||||||
|
mapCoordinates,
|
||||||
|
graphWidth,
|
||||||
|
graphHeight,
|
||||||
|
);
|
||||||
expect(result[0]).toBe(-3.34);
|
expect(result[0]).toBe(-3.34);
|
||||||
expect(result[1]).toBe(43.35);
|
expect(result[1]).toBe(43.35);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle global map coordinates', () => {
|
it("should handle global map coordinates", () => {
|
||||||
const globalMap = { lonW: -180, lonT: 360, latN: 90, latT: 180 };
|
const globalMap = { lonW: -180, lonT: 360, latN: 90, latT: 180 };
|
||||||
const result = getCoordinates(500, 400, globalMap, graphWidth, graphHeight, 2);
|
const result = getCoordinates(
|
||||||
|
500,
|
||||||
|
400,
|
||||||
|
globalMap,
|
||||||
|
graphWidth,
|
||||||
|
graphHeight,
|
||||||
|
2,
|
||||||
|
);
|
||||||
expect(result).toEqual([0, 0]); // center of the world
|
expect(result).toEqual([0, 0]); // center of the world
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -391,7 +391,14 @@ declare global {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Global variables defined in main.js
|
// Global variables defined in main.js
|
||||||
var mapCoordinates: { latT?: number; latN?: number; latS?: number; lonT?: number; lonW?: number; lonE?: number };
|
var mapCoordinates: {
|
||||||
|
latT?: number;
|
||||||
|
latN?: number;
|
||||||
|
latS?: number;
|
||||||
|
lonT?: number;
|
||||||
|
lonW?: number;
|
||||||
|
lonE?: number;
|
||||||
|
};
|
||||||
var graphWidth: number;
|
var graphWidth: number;
|
||||||
var graphHeight: number;
|
var graphHeight: number;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -231,12 +231,7 @@ import {
|
||||||
} from "./commonUtils";
|
} from "./commonUtils";
|
||||||
|
|
||||||
window.clipPoly = (points: [number, number][], secure?: number) =>
|
window.clipPoly = (points: [number, number][], secure?: number) =>
|
||||||
clipPoly(
|
clipPoly(points, graphWidth, graphHeight, secure);
|
||||||
points,
|
|
||||||
graphWidth,
|
|
||||||
graphHeight,
|
|
||||||
secure,
|
|
||||||
);
|
|
||||||
window.getSegmentId = getSegmentId;
|
window.getSegmentId = getSegmentId;
|
||||||
window.debounce = debounce;
|
window.debounce = debounce;
|
||||||
window.throttle = throttle;
|
window.throttle = throttle;
|
||||||
|
|
@ -248,28 +243,11 @@ window.link = link;
|
||||||
window.isCtrlClick = isCtrlClick;
|
window.isCtrlClick = isCtrlClick;
|
||||||
window.generateDate = generateDate;
|
window.generateDate = generateDate;
|
||||||
window.getLongitude = (x: number, decimals?: number) =>
|
window.getLongitude = (x: number, decimals?: number) =>
|
||||||
getLongitude(
|
getLongitude(x, mapCoordinates, graphWidth, decimals);
|
||||||
x,
|
|
||||||
mapCoordinates,
|
|
||||||
graphWidth,
|
|
||||||
decimals,
|
|
||||||
);
|
|
||||||
window.getLatitude = (y: number, decimals?: number) =>
|
window.getLatitude = (y: number, decimals?: number) =>
|
||||||
getLatitude(
|
getLatitude(y, mapCoordinates, graphHeight, decimals);
|
||||||
y,
|
|
||||||
mapCoordinates,
|
|
||||||
graphHeight,
|
|
||||||
decimals,
|
|
||||||
);
|
|
||||||
window.getCoordinates = (x: number, y: number, decimals?: number) =>
|
window.getCoordinates = (x: number, y: number, decimals?: number) =>
|
||||||
getCoordinates(
|
getCoordinates(x, y, mapCoordinates, graphWidth, graphHeight, decimals);
|
||||||
x,
|
|
||||||
y,
|
|
||||||
mapCoordinates,
|
|
||||||
graphWidth,
|
|
||||||
graphHeight,
|
|
||||||
decimals,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Initialize prompt when DOM is ready
|
// Initialize prompt when DOM is ready
|
||||||
if (document.readyState === "loading") {
|
if (document.readyState === "loading") {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue