ListToCsv.Key (list_to_csv v1.1.1) View Source

ListToCsv.Key contains types and utilities for keys.

Link to this section Summary

Functions

build prefix keys with trailing :N

Returns a list of keys duplicated n times. And replace first :N with current 1 base index.

Returns a new list created by replacing occurrences of from in subject with to. Only the first occurrence is replaced.

Returns true if keys starts with the given prefix list; otherwise returns false.

Link to this section Types

Specs

many() :: [t()] | t() | {function(), many()}

Specs

t() :: String.t() | atom() | integer() | function()

Link to this section Functions

Specs

build_prefix(many()) :: many()

build prefix keys with trailing :N

Examples

iex> build_prefix(:name)
[:name, :N]

iex> build_prefix([:item, :name])
[:item, :name, :N]

Specs

duplicate([many()], integer()) :: [many()]

Returns a list of keys duplicated n times. And replace first :N with current 1 base index.

Examples

iex> duplicate([[:name, :N]], 2)
[[:name, 1], [:name, 2]]

iex> duplicate([[:name, :N, :item, :N]], 2)
[[:name, 1, :item, :N], [:name, 2, :item, :N]]

iex> duplicate([{&(&1 + &2), [[:item, :N, :quantity], :capacity]}], 2)
[{&(&1 + &2), [[:item, 1, :quantity], :capacity]},
 {&(&1 + &2), [[:item, 2, :quantity], :capacity]}]
Link to this function

replace_first(subject, from, to)

View Source

Specs

replace_first([t()], t(), t()) :: [t()]

Returns a new list created by replacing occurrences of from in subject with to. Only the first occurrence is replaced.

Examples

iex> replace_first(:item, :N, 1)
:item

iex> replace_first([:item], :N, 1)
[:item]

iex> replace_first([:item, :N, :name], :N, 1)
[:item, 1, :name]

iex> replace_first([:item, :N, :name, :N], :N, 2)
[:item, 2, :name, :N]

iex> replace_first({&(&1 + &2), [[:item, :N, :quantity], :capacity]}, :N, 3)
{&(&1 + &2), [[:item, 3, :quantity], :capacity]}
Link to this function

starts_with?(keys, prefix)

View Source

Specs

starts_with?(many(), [t()]) :: boolean()

Returns true if keys starts with the given prefix list; otherwise returns false.

Note that :N can match with integer.

Examples

iex> starts_with?(:name, [:item, :N])
false

iex> starts_with?([:item, :N, :name], [:item, :N])
true

iex> starts_with?([:name], [:item, :N])
false

iex> starts_with?([:item, 1, :name, :N, :first], [:item, :N, :name, :N])
true

iex> starts_with?([:packages, :N, :name], [:item, :N])
false

iex> starts_with?({&(&1 + &2), [[:item, :N, :quantity], :capacity]}, [:item, :N])
true