Capstone.Vendor.Sourceror.Code.Keyword (Capstone v0.33.3)

Copy Markdown View Source

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

get_key(zipper, key)

Moves the zipper to the value of key in a keyword list.

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("[foo: 1, bar: 2]") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> {:ok, result} = Capstone.Vendor.Sourceror.Code.Keyword.get_key(zipper, :bar)
iex> match?({:__block__, _, [2]}, result.node)
true

See also keyword_has_path?/2.

keyword_has_path?(zipper, list)

@spec keyword_has_path?(Capstone.Vendor.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 = Capstone.Vendor.Sourceror.parse_string!("[foo: [bar: 1]]") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> Capstone.Vendor.Sourceror.Code.Keyword.keyword_has_path?(zipper, [:foo, :bar])
true
iex> Capstone.Vendor.Sourceror.Code.Keyword.keyword_has_path?(zipper, [:foo, :baz])
false

See also get_key/2.

keywordify(list, value)

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

Puts into nested keyword lists represented by path.

Examples

iex> Capstone.Vendor.Sourceror.Code.Keyword.keywordify([:foo, :bar], 1)
[{{:__block__, [format: :keyword], [:foo]}, {:__block__, [], [[{{:__block__, [format: :keyword], [:bar]}, {:__block__, [], [1]}}]]}}]

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

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 = Capstone.Vendor.Sourceror.parse_string!("[foo: 1]") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> {:ok, zipper} = Capstone.Vendor.Sourceror.Code.Keyword.put_in_keyword(zipper, [:bar], 2)
iex> Capstone.Vendor.Sourceror.to_string(zipper.node) |> String.contains?("bar:")
true

See also set_keyword_key/4.

remove_keyword_key(zipper, key)

@spec remove_keyword_key(Capstone.Vendor.Sourceror.Zipper.t(), atom()) ::
  {:ok, Capstone.Vendor.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 = Capstone.Vendor.Sourceror.parse_string!("[foo: 1, bar: 2]") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> {:ok, zipper} = Capstone.Vendor.Sourceror.Code.Keyword.remove_keyword_key(zipper, :foo)
iex> Capstone.Vendor.Sourceror.to_string(zipper.node) |> String.contains?("foo:")
false

See also set_keyword_key/4.

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

Puts a key into a keyword, calling updater on the zipper at the value if the key is already present.

Examples

iex> zipper = Capstone.Vendor.Sourceror.parse_string!("[foo: 1]") |> Capstone.Vendor.Sourceror.Zipper.zip()
iex> {:ok, zipper} = Capstone.Vendor.Sourceror.Code.Keyword.set_keyword_key(zipper, :bar, 2)
iex> Capstone.Vendor.Sourceror.to_string(zipper.node) |> String.contains?("bar:")
true

See also put_in_keyword/4.