1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-22 04:54:18 +00:00

💥 Refactor more code

This commit is contained in:
Steffo 2021-05-22 04:08:39 +02:00
parent 2737e8c1a2
commit 9c0356c8e4
Signed by: steffo
GPG key ID: 6965406171929D01
7 changed files with 47 additions and 54 deletions

View file

@ -58,7 +58,7 @@
</list> </list>
</option> </option>
</inspection_tool> </inspection_tool>
<inspection_tool class="RegExpAnonymousGroup" enabled="true" level="WEAK WARNING" enabled_by_default="true" /> <inspection_tool class="RegExpAnonymousGroup" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
<inspection_tool class="RegExpEscapedMetaCharacter" enabled="true" level="WEAK WARNING" enabled_by_default="true" /> <inspection_tool class="RegExpEscapedMetaCharacter" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
<inspection_tool class="RegExpOctalEscape" enabled="true" level="WEAK WARNING" enabled_by_default="true" /> <inspection_tool class="RegExpOctalEscape" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
<inspection_tool class="RegExpRedundantEscape" enabled="true" level="WEAK WARNING" enabled_by_default="true" /> <inspection_tool class="RegExpRedundantEscape" enabled="true" level="WEAK WARNING" enabled_by_default="true" />

View file

@ -3,6 +3,7 @@ import ReactDOM from "react-dom"
import "./index.css" import "./index.css"
import App from "./App" import App from "./App"
import reportWebVitals from "./reportWebVitals" import reportWebVitals from "./reportWebVitals"
import "./prototypes"
ReactDOM.render( ReactDOM.render(

View file

@ -14,6 +14,20 @@ export default class Coordinates {
this.lng = lng 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} * @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() { toGeolib() {
return { return {
@ -39,4 +53,13 @@ export default class Coordinates {
longitude: this.lng, longitude: this.lng,
} }
} }
/**
* Transform this object in a 2-ple.
*
* @returns {[Number, Number]}
*/
toArray() {
return [this.lat, this.lng]
}
} }

View file

@ -1,4 +1,5 @@
import {getDistance} from "geolib" import {getDistance} from "geolib"
import osmZoomLevels from "../utils/osmZoomLevels"
/** /**
@ -17,6 +18,20 @@ export default class MapArea {
this.center = center 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} * @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} * @returns {string}
*/ */

View file

@ -1,20 +1,10 @@
// Wow, JS, davvero? Date.prototype.toAwareISOString = function() {
// Davvero tutte le date.toISOString() sono considerate UTC? if(this.toString() === "Invalid Date") {
// 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") {
throw new Error("Data non valida ricevuta come parametro.") throw new Error("Data non valida ricevuta come parametro.")
} }
// Create a timezone naive ISO string // Create a timezone naive ISO string
const naive = date.toISOString() const naive = this.toISOString()
// Find the local timezone // Find the local timezone
const tz = -new Date().getTimezoneOffset() const tz = -new Date().getTimezoneOffset()
@ -29,4 +19,4 @@ export default function convertToLocalISODate(date) {
// Replace the naive part with the aware part // Replace the naive part with the aware part
return naive.replace("Z", `${tz < 0 ? "-" : "+"}${tzHours}${tzMinutes}`) return naive.replace("Z", `${tz < 0 ? "-" : "+"}${tzHours}${tzMinutes}`)
} }

1
nest_frontend/prototypes/index.js vendored Normal file
View file

@ -0,0 +1 @@
import "./Date"

View file

@ -1,37 +0,0 @@
export const locationRegex = /[{](?<lat>[0-9.]+),(?<lng>[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)}`
}
}