Skip to content

Commit

Permalink
Check for decimal lat/long before passing to values to fancier parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mo-nathan committed Dec 22, 2024
1 parent a4fc339 commit d5c0e58
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions app/javascript/controllers/geocode_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ export default class extends Controller {
this.getElevationTarget.disabled = true
}

// Using a regular expression
isValidDecimal(str) {
return /^-?\d*\.?\d+$/.test(str);
}

// Convert from human readable and do a rough check if they make sense
validateLatLngInputs(update = false) {
this.verbose("geocode:validateLatLngInputs")
Expand All @@ -331,9 +336,21 @@ export default class extends Controller {
origLng = this.lngInputTarget.value
let lat, lng

lat = parseFloat(origLat)
lng = parseFloat(origLng)

if (this.isValidDecimal(origLat) && this.isValidDecimal(origLng)) {
lat = parseFloat(origLat)
lng = parseFloat(origLng)
} else {
try {
let coords = convert(origLat + " " + origLng)
lat = coords.decimalLatitude,
lng = coords.decimalLongitude
}
// Toss any degree-minute-second notation and just take the first number
catch {
lat = parseFloat(origLat)
lng = parseFloat(origLng)
}
}
if (!lat || !lng)
return false
if (lat > 90 || lat < -90 || lng > 180 || lng < -180)
Expand Down

0 comments on commit d5c0e58

Please sign in to comment.