Module array
Array utilities module
Functions
concat (arr1, arr2) | Concat arr2 into arr1 |
contains (arr, elem) | Check if an array contains an element |
mergeTables (arr, keys) | Merge keys of tables into a simple array |
allOf (arr, fn, ...) | Ensure every element of arr produce a true return of fn |
anyOf (arr, fn, ...) | Check if one of arr’s element produce a true return of fn |
noneOf (arr, fn, ...) | Check if all of arr’s element produce a false return of fn |
forEach (arr, fn, ...) | Execute a function with all elements of an array |
join (arr, separator) | Join array elements together into a string |
Functions
- concat (arr1, arr2)
-
Concat arr2 into arr1
Parameters:
- arr1 table The first array to concat the second into
- arr2 table The second array to concat into the first
Returns:
-
table # A new array with arr2 concat into arr1
Raises:
error if arr1 is not a table
error if arr2 is not a tableUsage:
local arr1 = { "one", "three" } local arr2 = { "two", 4 } local arr3 = concat(arr1, arr2) -- arr3 -> { "one", "three", "two", 4 }
- contains (arr, elem)
-
Check if an array contains an element
Parameters:
- arr table The array to search the element in
- elem any The element to search in the array. This cannot be nil
Returns:
-
boolean #
true
if arr contains elem,false
otherwiseRaises:
error if arr is not a table
error if elem is nilUsage:
local arr = { 1, 2, 4 } if not contains(arr, 3) then print("Array does not contains a 3") else print("Array contains a 3") end
- mergeTables (arr, keys)
-
Merge keys of tables into a simple array
Parameters:
- arr table The array containing tables to merge keys from
- keys
any|table The keys to build the new array from values
You can submit one or multiple keys through a table.
If multiple keys are submitted, the first found one will be used, others will be used as fallbacks
Raises:
error if arr is not a table
error if keys is nilUsage:
local arr = { { name = "Component1", description = "This is the description of the Component1", alias = "C1", }, { name = "Component2", description = "This is the description of the Component2", alias = "C2", }, { name = "Component3", description = "This is the description of the Component3", }, } local test1 = mergeTables(arr, "name") -- { "Component1", "Component2", "Component3" } local test2 = mergeTables(arr, { "name", "alias" }) -- { "Component1", "Component2", "Component3" } local test3 = mergeTables(arr, { "alias", "name" }) -- { "C1", "C2", "Component3" }
- allOf (arr, fn, ...)
-
Ensure every element of arr produce a true return of fn
Parameters:
- arr table The array to test element with fn
- fn
function The function to call with arr elements.
This function should return a boolean and take an element of arr as first argument - ... any Additional arguments to pass to fn
Returns:
-
boolean # true if all call of fn with each of arr’s elements returned true, false otherwise
Raises:
error if arr is not a table
error if fn is not a function
error if fn returned something else than a booleanUsage:
local arr = { 1, 2, 3 } if allOf(arr, function(elem) return type(elem) == "number" end) print("All array elements are numbers") else print("One or more array element(s) is not a number") end
- anyOf (arr, fn, ...)
-
Check if one of arr’s element produce a true return of fn
Parameters:
- arr table The array to test element with fn
- fn
function The function to call with arr elements.
This function should return a boolean and take an element of arr as first argument - ... any Additional arguments to pass to fn
Returns:
-
boolean # true if one of the calls of fn with each of arr’s elements returned true, false otherwise
Raises:
error if arr is not a table
error if fn is not a function
error if fn returned something else than a booleanUsage:
local arr = { 1, 2, "hello" } if anyOf(arr, function(elem) return type(value) == "string" end) print("One or more of the array element(s) is a string") else print("None of the array element is a string") end
- noneOf (arr, fn, ...)
-
Check if all of arr’s element produce a false return of fn
Parameters:
- arr table The array to test element with fn
- fn
function The function to call with arr elements.
This function should return a boolean and take an element of arr as first argument - ... any Additional arguments to pass to fn
Returns:
-
boolean # true if none of the calls of fn with each of arr’s elements returned true, false otherwise
Raises:
error if arr is not a table
error if fn is not a function
error if fn returned something else than a booleanUsage:
local arr = { 1, 2, 3 } if noneOf(arr, function(elem) return type(elem) == "string" end) print("The array does not contains string values") end
- forEach (arr, fn, ...)
-
Execute a function with all elements of an array
Parameters:
- arr table The array to test element with fn
- fn
function The function to call with arr elements.
This function should take an element of arr as first argument - ... any Additional arguments to pass to fn
Raises:
error if arr is not a table
error if fn is not a functionUsage:
local arr = { 1, 2, 3 } forEach(arr, print) -- 1 -- 2
- join (arr, separator)
-
Join array elements together into a string
Parameters:
- arr table The array to join elements of
- separator ? string The string to insert between element
Raises:
error if arr is not a table
error if separator is not a stringUsage:
local arr = { 1, 2, 3 } print(join(arr, ", ")) -- "1, 2, 3"