Extra v0.2.0 Keyword.Extra

Extensions to the built-in Keyword module.

Link to this section Summary

Functions

Raises an ArgumentError error if list does not have the provided key

Fetches the values for the specified keys

Gets the values for the specified keys

Raises an ArgumentError error if list contains the provided key

Takes all entries corresponding to the given keys and returns them in a new keyword list in the order that keys arrived

Link to this section Types

Link to this section Functions

Link to this function assert_key!(list, key, opts \\ [])
assert_key!(t, key, keyword) :: :ok | no_return

Raises an ArgumentError error if list does not have the provided key.

Options

  • :message: binary, the message to raise with when the value is missing.
  • :allow_nil_value: boolean, Default: true. Set to true if a nil value should not raise.
Link to this function fetch_keys!(list, keys)
fetch_keys!(t, [key]) :: [value]

Fetches the values for the specified keys.

If any of the keys do not exist, a KeyError is raised.

Examples

iex> Keyword.Extra.fetch_keys!([color: “blue”], [:color]) [“blue”]

Link to this function get_keys(list, keys)
get_keys(t, [key | {key, default :: any}]) :: [value]

Gets the values for the specified keys.

Pass default values for keys using the following format: {key, default}. If any of the keys do not exist, return nil or use the default value if supplied.

Examples

iex> Keyword.Extra.get_keys([ssn: “1234”, age: 41], [:age, :ssn]) [41, “1234”]

iex> Keyword.Extra.get_keys([first_name: “Bob”], [first_name: :missing, last_name: :missing]) [“Bob”, :missing]

Link to this function refute_key!(list, key, message \\ "Key is not allowed to be in list")
refute_key!(t, key, String.t) :: :ok | no_return

Raises an ArgumentError error if list contains the provided key.

Takes an optional argument specifying the error message.

Link to this function take_ordered(kw_list, keys)
take_ordered(t, [key]) :: t
take_ordered(map, [key]) :: t

Takes all entries corresponding to the given keys and returns them in a new keyword list in the order that keys arrived.

Examples

iex> Keyword.Extra.take_ordered([c: 3, b: 2, a: 1], [:a, :b]) [a: 1, b: 2]

iex> Keyword.Extra.take_ordered([x: 1, y: 2], [:x, :y, :z]) [x: 1, y: 2]

iex> location = %{state: “NY”, city: “Manhattan”, zip_code: “10009”} …> Keyword.Extra.take_ordered(location, [:city, :state, :zip_code]) [city: “Manhattan”, state: “NY”, zip_code: “10009”]