View Source Sourceror.Code.Keyword (Sourceror v1.11.0)
Utilities for working with keyword.
Summary
Functions
Moves the zipper to the value of key in a keyword list.
Returns true if the node is a nested keyword list containing a value at the given path.
Puts into nested keyword lists represented by path.
Puts a value at a path into a keyword, calling updater on the zipper at the value if the key is already present.
Removes a key from a keyword list if present. Returns :error only if the node is not a list.
Puts a key into a keyword, calling updater on the zipper at the value if the key is already present.
Functions
@spec get_key(Sourceror.Zipper.t(), atom()) :: {:ok, Sourceror.Zipper.t()} | :error
Moves the zipper to the value of key in a keyword list.
Examples
iex> zipper = Sourceror.parse_string!("[foo: 1, bar: 2]") |> Sourceror.Zipper.zip()
iex> {:ok, result} = Sourceror.Code.Keyword.get_key(zipper, :bar)
iex> match?({:__block__, _, [2]}, result.node)
trueSee also keyword_has_path?/2.
@spec keyword_has_path?(Sourceror.Zipper.t(), [atom()]) :: boolean()
Returns true if the node is a nested keyword list containing a value at the given path.
Examples
iex> zipper = Sourceror.parse_string!("[foo: [bar: 1]]") |> Sourceror.Zipper.zip()
iex> Sourceror.Code.Keyword.keyword_has_path?(zipper, [:foo, :bar])
true
iex> Sourceror.Code.Keyword.keyword_has_path?(zipper, [:foo, :baz])
falseSee also get_key/2.
Puts into nested keyword lists represented by path.
Examples
iex> Sourceror.Code.Keyword.keywordify([:foo, :bar], 1)
[{{:__block__, [format: :keyword], [:foo]}, {:__block__, [], [[{{:__block__, [format: :keyword], [:bar]}, {:__block__, [], [1]}}]]}}]
@spec put_in_keyword( Sourceror.Zipper.t(), [atom()], term(), (Sourceror.Zipper.t() -> {:ok, Sourceror.Zipper.t()} | :error) | nil ) :: {:ok, Sourceror.Zipper.t()} | :error
Puts a value at a path into a keyword, calling updater on the zipper at the value if the key is already present.
Examples
iex> zipper = Sourceror.parse_string!("[foo: 1]") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.Keyword.put_in_keyword(zipper, [:bar], 2)
iex> Sourceror.to_string(zipper.node) |> String.contains?("bar:")
trueSee also set_keyword_key/4.
@spec remove_keyword_key(Sourceror.Zipper.t(), atom()) :: {:ok, Sourceror.Zipper.t()} | :error
Removes a key from a keyword list if present. Returns :error only if the node is not a list.
Examples
iex> zipper = Sourceror.parse_string!("[foo: 1, bar: 2]") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.Keyword.remove_keyword_key(zipper, :foo)
iex> Sourceror.to_string(zipper.node) |> String.contains?("foo:")
falseSee also set_keyword_key/4.
@spec set_keyword_key( Sourceror.Zipper.t(), atom(), term(), (Sourceror.Zipper.t() -> {:ok, Sourceror.Zipper.t()} | :error) | nil ) :: {:ok, Sourceror.Zipper.t()} | :error
Puts a key into a keyword, calling updater on the zipper at the value if the key is already present.
Examples
iex> zipper = Sourceror.parse_string!("[foo: 1]") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.Keyword.set_keyword_key(zipper, :bar, 2)
iex> Sourceror.to_string(zipper.node) |> String.contains?("bar:")
trueSee also put_in_keyword/4.