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)
true

See also keyword_has_path?/2.

Link to this function

keyword_has_path?(zipper, list)

View Source
@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])
false

See also get_key/2.

@spec keywordify(path :: [atom()], value :: any()) :: any()

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]}}]]}}]
Link to this function

put_in_keyword(zipper, path, value, updater \\ nil)

View Source
@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:")
true

See also set_keyword_key/4.

Link to this function

remove_keyword_key(zipper, key)

View Source
@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:")
false

See also set_keyword_key/4.

Link to this function

set_keyword_key(zipper, key, value, updater \\ nil)

View Source
@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:")
true

See also put_in_keyword/4.