diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 90c84e8..e5d68bf 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -58,7 +58,7 @@ - + diff --git a/nest_frontend/index.js b/nest_frontend/index.js index c9b7cd1..b931256 100644 --- a/nest_frontend/index.js +++ b/nest_frontend/index.js @@ -3,6 +3,7 @@ import ReactDOM from "react-dom" import "./index.css" import App from "./App" import reportWebVitals from "./reportWebVitals" +import "./prototypes" ReactDOM.render( diff --git a/nest_frontend/objects/Coordinates.js b/nest_frontend/objects/Coordinates.js index 9f4868a..10413f2 100644 --- a/nest_frontend/objects/Coordinates.js +++ b/nest_frontend/objects/Coordinates.js @@ -14,6 +14,20 @@ export default class Coordinates { this.lng = lng } + /** + * Create a new {@link Coordinates} from the format used by the backend. + * + * @param str - The string to create the object from. + * @returns {Coordinates} + */ + static fromCrawlerString(str) { + const match = /[{]([0-9.]+),([0-9.]+)[}]/.exec(str) + if(!match) { + throw new Error(`Invalid location string: ${str}`) + } + return new Coordinates(match[0], match[1]) + } + /** * @returns {string} */ @@ -31,7 +45,7 @@ export default class Coordinates { } /** - * Transform the object in a Geolib compatible-one. + * Transform this object in a Geolib compatible-one. */ toGeolib() { return { @@ -39,4 +53,13 @@ export default class Coordinates { longitude: this.lng, } } + + /** + * Transform this object in a 2-ple. + * + * @returns {[Number, Number]} + */ + toArray() { + return [this.lat, this.lng] + } } diff --git a/nest_frontend/objects/MapArea.js b/nest_frontend/objects/MapArea.js index 9bd5b9e..a222742 100644 --- a/nest_frontend/objects/MapArea.js +++ b/nest_frontend/objects/MapArea.js @@ -1,4 +1,5 @@ import {getDistance} from "geolib" +import osmZoomLevels from "../utils/osmZoomLevels" /** @@ -17,6 +18,20 @@ export default class MapArea { this.center = center } + /** + * Create a new {@link MapArea} from the [zoom level of OpenStreetMaps][1], assuming the window is + * ~400 pixels large. + * + * [1]: https://wiki.openstreetmap.org/wiki/Zoom_levels + * + * @param zoom + * @param center + * @returns {MapArea} + */ + static fromZoomLevel(zoom, center) { + return new MapArea(osmZoomLevels[zoom], center) + } + /** * @returns {string} */ @@ -25,7 +40,7 @@ export default class MapArea { } /** - * Render the MapArea as an human-readable string. + * Render the {@link MapArea} as an human-readable string. * * @returns {string} */ diff --git a/nest_frontend/utils/convertToLocalISODate.js b/nest_frontend/prototypes/Date.js similarity index 59% rename from nest_frontend/utils/convertToLocalISODate.js rename to nest_frontend/prototypes/Date.js index 8282621..79e80cf 100644 --- a/nest_frontend/utils/convertToLocalISODate.js +++ b/nest_frontend/prototypes/Date.js @@ -1,20 +1,10 @@ -// Wow, JS, davvero? -// Davvero tutte le date.toISOString() sono considerate UTC? -// Wow. - -/** - * Convert a {@link Date} object to a timezone aware ISO String, using the user's local timezone. - * - * @param date - * @returns {string} - */ -export default function convertToLocalISODate(date) { - if(date.toString() === "Invalid Date") { +Date.prototype.toAwareISOString = function() { + if(this.toString() === "Invalid Date") { throw new Error("Data non valida ricevuta come parametro.") } // Create a timezone naive ISO string - const naive = date.toISOString() + const naive = this.toISOString() // Find the local timezone const tz = -new Date().getTimezoneOffset() @@ -29,4 +19,4 @@ export default function convertToLocalISODate(date) { // Replace the naive part with the aware part return naive.replace("Z", `${tz < 0 ? "-" : "+"}${tzHours}${tzMinutes}`) -} \ No newline at end of file +} diff --git a/nest_frontend/prototypes/index.js b/nest_frontend/prototypes/index.js new file mode 100644 index 0000000..aff132e --- /dev/null +++ b/nest_frontend/prototypes/index.js @@ -0,0 +1 @@ +import "./Date" \ No newline at end of file diff --git a/nest_frontend/utils/location.js b/nest_frontend/utils/location.js deleted file mode 100644 index 561f3d0..0000000 --- a/nest_frontend/utils/location.js +++ /dev/null @@ -1,37 +0,0 @@ -export const locationRegex = /[{](?[0-9.]+),(?[0-9.]+)[}]/ - - -export class Location { - lat - lng - - constructor(lat, lng) { - this.lat = lat - this.lng = lng - } - - static fromString(locString) { - const match = locationRegex.exec(locString) - if(!match) { - throw new Error(`Invalid location string: ${locString}`) - } - const { lat, lng } = match.groups - return new Location(lat, lng) - } - - static fromTweet(tweet) { - if(tweet.location === null) { - throw new Error(`Tweet has no location: ${tweet}`) - } - - return Location.fromString(tweet.location) - } - - toArray() { - return [this.lat, this.lng] - } - - toString() { - return `${this.lat.toFixed(3)} ${this.lng.toFixed(3)}` - } -}