From c90a76df04bc27b0fd98bdcc24d2f1238bc97b5f Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Thu, 23 Apr 2026 15:03:02 -0500 Subject: [PATCH] refactor: CoordinateUtils.get_coordinates_from_mic_e uses guarded clauses - get_coordinates_from_mic_e: replace two inline `if direction == ...` rebinds with a reusable apply_hemisphere_sign/3 helper; the final range check becomes maybe_return_coords/2 with a guarded clause that yields {lat, lon} or {nil, nil}. - has_position_data?: the boolean short-circuit collapses from an explicit if/else to a single `or` expression. All 37 CoordinateUtils tests (4 properties) still pass. --- .../live/shared/coordinate_utils.ex | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/aprsme_web/live/shared/coordinate_utils.ex b/lib/aprsme_web/live/shared/coordinate_utils.ex index 333ce6c..1b329fc 100644 --- a/lib/aprsme_web/live/shared/coordinate_utils.ex +++ b/lib/aprsme_web/live/shared/coordinate_utils.ex @@ -33,13 +33,31 @@ defmodule AprsmeWeb.Live.Shared.CoordinateUtils do """ @spec get_coordinates_from_mic_e(MicE.t()) :: {number() | nil, number() | nil} def get_coordinates_from_mic_e(mic_e) do - lat = mic_e.lat_degrees + mic_e.lat_minutes / 60.0 + mic_e.lat_fractional / 6000.0 - lat = if mic_e.lat_direction == :south, do: -lat, else: lat - lon = mic_e.lon_degrees + mic_e.lon_minutes / 60.0 + mic_e.lon_fractional / 6000.0 - lon = if mic_e.lon_direction == :west, do: -lon, else: lon - if lat >= -90 && lat <= 90 && lon >= -180 && lon <= 180, do: {lat, lon}, else: {nil, nil} + lat = + apply_hemisphere_sign( + mic_e.lat_degrees + mic_e.lat_minutes / 60.0 + mic_e.lat_fractional / 6000.0, + mic_e.lat_direction, + :south + ) + + lon = + apply_hemisphere_sign( + mic_e.lon_degrees + mic_e.lon_minutes / 60.0 + mic_e.lon_fractional / 6000.0, + mic_e.lon_direction, + :west + ) + + maybe_return_coords(lat, lon) end + # Negate the value when the hemisphere indicator matches the negative axis. + defp apply_hemisphere_sign(value, direction, negative) when direction == negative, do: -value + defp apply_hemisphere_sign(value, _direction, _negative), do: value + + defp maybe_return_coords(lat, lon) when lat >= -90 and lat <= 90 and lon >= -180 and lon <= 180, do: {lat, lon} + + defp maybe_return_coords(_lat, _lon), do: {nil, nil} + @doc """ Check if packet has position data in any format. """ @@ -48,11 +66,7 @@ defmodule AprsmeWeb.Live.Shared.CoordinateUtils do lat = Map.get(packet, :lat) || Map.get(packet, "lat") lon = Map.get(packet, :lon) || Map.get(packet, "lon") - if has_direct_coordinates?(lat, lon) do - true - else - has_position_in_data_extended?(packet) - end + has_direct_coordinates?(lat, lon) or has_position_in_data_extended?(packet) end @doc """