2021-05-21 14:37:30 +00:00
|
|
|
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}`)
|
|
|
|
}
|
2021-05-21 17:52:56 +00:00
|
|
|
const { lat, lng } = match.groups
|
2021-05-21 14:37:30 +00:00
|
|
|
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)}`
|
|
|
|
}
|
|
|
|
}
|