From 7ca7ae49a598e8d3bbd506e5deb6180c83003e0c Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 22 May 2021 03:50:04 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20Coordinates=20class=20and=20s?= =?UTF-8?q?ome=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nest_frontend/objects/Condition.test.js | 6 ++-- nest_frontend/objects/Coordinates.js | 42 +++++++++++++++++++++++++ nest_frontend/objects/MapArea.js | 39 ++++++++++++++++------- nest_frontend/objects/MapArea.test.js | 13 ++++++++ 4 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 nest_frontend/objects/Coordinates.js create mode 100644 nest_frontend/objects/MapArea.test.js diff --git a/nest_frontend/objects/Condition.test.js b/nest_frontend/objects/Condition.test.js index 7fe651b..ee838af 100644 --- a/nest_frontend/objects/Condition.test.js +++ b/nest_frontend/objects/Condition.test.js @@ -1,7 +1,7 @@ -import "@testing-library/jest-dom/extend-expect" import { Condition, ConditionHashtag, ConditionLocation, ConditionTime, ConditionUser } from "./Condition" import TimeRay from "./TimeRay" import MapArea from "./MapArea" +import Coordinates from "./Coordinates" test("Condition can be constructed", () => { @@ -28,7 +28,7 @@ test("ConditionTime can be constructed", () => { }) test("ConditionLocation can be constructed", () => { - const mapArea = new MapArea(1000, 0.000, 0.000) + const mapArea = new MapArea(1000, new Coordinates(0.000, 0.000)) expect(new ConditionLocation(mapArea)).toBeTruthy() expect(new ConditionLocation(mapArea, 1)).toBeTruthy() @@ -50,7 +50,7 @@ test("ConditionTime has the correct type", () => { }) test("ConditionLocation has the correct type", () => { - const mapArea = new MapArea(1000, 0.000, 0.000) + const mapArea = new MapArea(1000, new Coordinates(0.000, 0.000)) expect(new ConditionLocation(mapArea).type).toBe(3) }) diff --git a/nest_frontend/objects/Coordinates.js b/nest_frontend/objects/Coordinates.js new file mode 100644 index 0000000..9f4868a --- /dev/null +++ b/nest_frontend/objects/Coordinates.js @@ -0,0 +1,42 @@ +/** + * A pair of coordinates, latitude `lat` and longitude `lng`. + */ +export default class Coordinates { + lat + lng + + /** + * @param lat - Latitude. + * @param lng - Longitude. + */ + constructor(lat, lng) { + this.lat = lat + this.lng = lng + } + + /** + * @returns {string} + */ + toString() { + return `${this.lat.toFixed(7)} ${this.lng.toFixed(7)}` + } + + /** + * Render the Coordinates as an human-readable string. + * + * @returns {string} + */ + toHumanString() { + return `${this.lat.toFixed(3)} ${this.lng.toFixed(3)}` + } + + /** + * Transform the object in a Geolib compatible-one. + */ + toGeolib() { + return { + latitude: this.lat, + longitude: this.lng, + } + } +} diff --git a/nest_frontend/objects/MapArea.js b/nest_frontend/objects/MapArea.js index 4d23de4..9bd5b9e 100644 --- a/nest_frontend/objects/MapArea.js +++ b/nest_frontend/objects/MapArea.js @@ -1,23 +1,27 @@ +import {getDistance} from "geolib" + + /** - * An area on a map, defined by a latitude `lat`, a longitude `lng` and a radius `rad` in meters. + * An area on a map, defined by a `center` and a `radius` in meters. */ export default class MapArea { + radius + center + /** - * @param rad - Radius of the area in meters. - * @param lat - Latitude of the center of the radius. - * @param lng - Longitude of the center of the radius. + * @param radius - The radius of the area in meters. + * @param center - The center of the area. */ - constructor(rad, lat, lng) { - this.rad = rad - this.lat = lat - this.lng = lng + constructor(radius, center) { + this.radius = radius + this.center = center } /** * @returns {string} */ toString() { - return `${this.rad} ${this.lat.toFixed(7)} ${this.lng.toFixed(7)}` + return `${this.radius} ${this.center.toString()}` } /** @@ -26,9 +30,20 @@ export default class MapArea { * @returns {string} */ toHumanString() { - if(this.rad >= 2000) { - const kmRadius = Math.round(this.rad / 1000) - return `${kmRadius}km ${this.lat.toFixed(3)} ${this.lng.toFixed(3)}` + if(this.radius >= 2000) { + const kmRadius = Math.round(this.radius / 1000) + return `${kmRadius}km ${this.center.toHumanString()}` } + return `${this.radius}m ${this.center.toHumanString()}` + } + + /** + * Check if a pair of coordinates is included in the area. + * + * @param coords - The coordinates to check. + * @returns {boolean} + */ + includes(coords) { + return getDistance(this.center.toGeolib(), coords.toGeolib()) <= this.radius } } diff --git a/nest_frontend/objects/MapArea.test.js b/nest_frontend/objects/MapArea.test.js new file mode 100644 index 0000000..409f015 --- /dev/null +++ b/nest_frontend/objects/MapArea.test.js @@ -0,0 +1,13 @@ +import Coordinates from "./Coordinates" +import MapArea from "./MapArea" + + +test("MapArea can be constructed", () => { + const mapArea = new MapArea(1000, new Coordinates(0.0, 0.0)) + expect(mapArea).toBeTruthy() +}) + +test("MapArea can be rendered to a spec-compatible string", () => { + const mapArea = new MapArea(1000, new Coordinates(0.0, 0.0)) + expect(mapArea.toString()).toBe("1000 0.0000000 0.0000000") +})