mirror of
https://github.com/pds-nest/nest.git
synced 2024-11-25 14:34:19 +00:00
37 lines
814 B
JavaScript
37 lines
814 B
JavaScript
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)}`
|
|
}
|
|
}
|