prop/lib/microwaveprop_web/live_helpers.ex
Graham McIntire 2defa3db7a
refactor: deduplicate helpers and simplify Scorer
- Unify haversine_km / deg_to_rad: Geo is the canonical home (atan2
  form for antipodal stability); Radio, common_volume, rain_scatter,
  ionosphere, terrain/srtm, terrain/elevation_client now delegate.
- Drop safe_min/safe_max wrappers in favor of Enum.min/max defaults.
- Move parse_float / parse_int to MicrowavepropWeb.LiveHelpers; eme,
  path, and rover LiveViews import from there.
- Extract MicrowavepropWeb.LocationResolver from the eme/path
  resolve_source / resolve_location duplicate. Standardize the kind
  key and "Invalid grid square" error message.
- Convert Scorer cond chains (humidity, td_depression, sky, wind,
  rain, pwat, pressure) to multi-clause guarded defs, matching the
  existing classify_time_period style.
- extract_profile_fields uses a named map accumulator instead of a
  6-tuple; build_path_conditions guards on %{temps: []} / %{dewpoints: []}.
- Collapse scores_file clamp/3 to max |> min.
- humidity_effect_label lives in BandConfig; map_live and path_live
  both use it.
2026-04-28 14:14:34 -05:00

37 lines
1 KiB
Elixir

defmodule MicrowavepropWeb.LiveHelpers do
@moduledoc """
Small helpers shared across LiveView modules. Use sparingly — most
view-specific logic belongs in its own LiveView.
"""
@doc """
Parses a value into a float, returning `default` for nil, empty, or
unparseable input. Accepts strings, numbers, or any value with a
`String.Chars` impl.
"""
@spec parse_float(term(), float()) :: float()
def parse_float(nil, default), do: default
def parse_float("", default), do: default
def parse_float(value, default) do
case value |> to_string() |> Float.parse() do
{n, _} -> n
:error -> default
end
end
@doc """
Parses a value into an integer, returning `default` for nil, empty,
or unparseable input.
"""
@spec parse_int(term(), integer()) :: integer()
def parse_int(nil, default), do: default
def parse_int("", default), do: default
def parse_int(value, default) do
case value |> to_string() |> Integer.parse() do
{n, _} -> n
:error -> default
end
end
end