Krug.StructUtil (Krug v2.0.27) View Source

Utilitary secure module to provide mechanisms to handle some complex operations on structs.

Link to this section Summary

Functions

Compare 2 lists and return if all elements from first are present on second

Return a key par value from a list of key_par strings in format ["key=value","key=value"].

Return a value from a tuple, ignoring the key.

Verify if a value is present on a list or not.

Verify if one value of a list values is present on a list or not. Return true when find first match. If none value of values is present on list, return false.

Link to this section Functions

Link to this function

contains_all(list_1, list_2)

View Source (since 1.1.31)

Compare 2 lists and return if all elements from first are present on second

  • (if are equals). Return true or false
Link to this function

get_key_par_value_from_list(key, list)

View Source (since 0.2.1)

Return a key par value from a list of key_par strings in format ["key=value","key=value"].

If the key or the list is nil/empty return nil.

Examples

iex > Krug.StructUtil.get_key_par_value_from_list(nil,nil)
nil
iex > Krug.StructUtil.get_key_par_value_from_list("name",nil)
nil
iex > Krug.StructUtil.get_key_par_value_from_list(nil,"name")
nil
iex > Krug.StructUtil.get_key_par_value_from_list([],"name")
nil
iex > list = ["name=Johannes Backend","email=johannes@has.not.email"]
iex > Krug.StructUtil.get_key_par_value_from_list("",list)
nil
iex > list = ["name=Johannes Backend","email=johannes@has.not.email"]
iex > Krug.StructUtil.get_key_par_value_from_list("keyNotExist",list)
nil
iex > list = ["name=Johannes Backend","email=johannes@has.not.email"]
iex > Krug.StructUtil.get_key_par_value_from_list("name",list)
"Johannes Backend"
iex > list = ["name=Johannes Backend","email=johannes@has.not.email"]
iex > Krug.StructUtil.get_key_par_value_from_list("email",list)
"johannes@has.not.email"
iex > list = [" name = Johannes Backend ","email=johannes@has.not.email"]
iex > Krug.StructUtil.get_key_par_value_from_list("email",list)
"Johannes Backend"
Link to this function

get_value_from_tuple(tuple)

View Source

Return a value from a tuple, ignoring the key.

If the tuple is nil or value of key is nil/empty return nil.

If tuple contains only the key, return nil.

Examples

iex > tuple = {:ok}
iex > Krug.StructUtil.get_value_from_tuple(tuple)
nil
iex > tuple = {:ok,nil}
iex > Krug.StructUtil.get_value_from_tuple(tuple)
nil
iex > tuple = {:ok,[1,2,3]}
iex > Krug.StructUtil.get_value_from_tuple(tuple)
[1,2,3]
iex > tuple = {:ok,%{a: 1, b: 2, c: 3}}
iex > Krug.StructUtil.get_value_from_tuple(tuple)
%{a: 1, b: 2, c: 3}
iex > tuple = {:error,"Operation Error"}
iex > Krug.StructUtil.get_value_from_tuple(tuple)
"Operation Error"
Link to this function

list_contains(list, value)

View Source

Verify if a value is present on a list or not.

If the list is nil/empty return false.

Examples

iex > list = []
iex > Krug.StructUtil.list_contains(list,nil)
false
iex > list = nil
iex > Krug.StructUtil.list_contains(list,nil)
false
iex > list = [nil]
iex > Krug.StructUtil.list_contains(list,nil)
true
iex > list = [1,%{a: 1, b: 2},"",nil,[1,2,3],5]
iex > Krug.StructUtil.list_contains(list,"")
true
iex > list = [1,%{a: 1, b: 2},"",nil,[1,2,3],5]
iex > Krug.StructUtil.list_contains(list," ")
false
iex > list = [1,%{a: 1, b: 2},"",nil,[1,2,3],5]
iex > Krug.StructUtil.list_contains(list,%{a: 1, b: 2})
true
iex > list = [1,%{a: 1, b: 2},"",nil,[1,2,3],5]
iex > Krug.StructUtil.list_contains(list,%{a: 1, b: 5})
false
Link to this function

list_contains_one_of(list, values)

View Source

Verify if one value of a list values is present on a list or not. Return true when find first match. If none value of values is present on list, return false.

If the list is nil/empty return false.

Examples

iex > list = []
iex > values = []
iex > Krug.StructUtil.list_contains_one_of(list,values)
false
iex > list = [1,2,4,6]
iex > values = [5,7,8]
iex > Krug.StructUtil.list_contains_one_of(list,values)
false
iex > list = [1,2,4,6]
iex > values = [5,7,8,"A",%{a: 1, b: 3},9,6]
iex > Krug.StructUtil.list_contains_one_of(list,values)
true
iex > list = [1,2,4,6]
iex > values = [5,7,8,"A",%{a: 1, b: 3},9]
iex > Krug.StructUtil.list_contains_one_of(list,values)
false
iex > list = [1,2,4,6,%{a: 1, b: 3}]
iex > values = [5,7,8,"A",%{a: 1, b: 3},9]
iex > Krug.StructUtil.list_contains_one_of(list,values)
true
iex > list = [1,2,4,6,%{a: 1, b: 3}]
iex > values = [5,7,8,"A",%{a: 1, b: 5},9]
iex > Krug.StructUtil.list_contains_one_of(list,values)
false