Packages

An OTP-native coding agent SDK. Build, orchestrate, and observe AI coding agents with Elixir supervision trees, streaming events, and a JSON-RPC 2.0 interface.

Current section

Files

Jump to
opal lib opal util map.ex
Raw

lib/opal/util/map.ex

defmodule Opal.Util.Map do
@moduledoc "Shared map-building helpers."
@doc """
Strips nil and empty string values from a map.
iex> Opal.Util.Map.compact(%{a: 1, b: nil, c: ""})
%{a: 1}
"""
@spec compact(map()) :: map()
def compact(map), do: Map.reject(map, fn {_, v} -> v == nil or v == "" end)
@doc """
Puts `value` into `map` under `key` unless `value` is nil.
iex> Opal.Util.Map.put_non_nil(%{a: 1}, :b, nil)
%{a: 1}
iex> Opal.Util.Map.put_non_nil(%{a: 1}, :b, 2)
%{a: 1, b: 2}
"""
@spec put_non_nil(map(), term(), term()) :: map()
def put_non_nil(map, _key, nil), do: map
def put_non_nil(map, key, val), do: Map.put(map, key, val)
end