-
Notifications
You must be signed in to change notification settings - Fork 0
Methods
Returns the total number of the element in a table.
parameter | type |
---|---|
tbl | table |
Returns: number
print(table.count({'bunch', 'of', 'strings'})) --> 3
Returns the total number of the element in a table recursively.
parameter | type |
---|---|
tbl | table |
Returns: number
print(table.deepCount({'bunch', 'of', 'string', {'and', 'even', 'more', 'string'}})) --> 7
Determines whether the table contains the passed value
, returning true
or false
as appropriate.
parameter | type |
---|---|
tbl | table |
value | any |
Returns: boolean
print(table.includes({'Bob', 'Jeff', 'Andrew'}, 'Andrew')) --> true
Determines if the table has index
on it, returning true
or false
as appropriate.
parameter | type |
---|---|
tbl | table |
index | any |
Returns: boolean
local t = {
data = {
value = 0,
info = 'cool'
},
users = {
'878973176219', '7287643892364'
}
}
print(table.has(t, 'data')) --> true
Determines if the table is not empty. Returns true
or false
, as appropriate.
parameter | type |
---|---|
tbl | table |
Returns: boolean
print(table.isEmpty({})) --> true
print(table.isEmpty({2, 3, 0, 1})) --> false
Determines if the table is a array-like table.
parameter | type |
---|---|
tbl | table |
Returns: boolean
print(table.isArray({1, 2, 3})) --> true
print(table.isArray({something = 'Rick'})) --> false
Determines if the table is a dictionary-like table.
A dictionary is defined as a table containing keys that are not positive integers. (e.g. { [19] = 'random text', debug = true }
)
parameter | type |
---|---|
tbl | table |
Returns: boolean
print(table.isDictionary({1, 9, 8, 7})) --> false
print(table.isDictionary({something = 'Astley'})) --> true
Returns a new copy of the table, one layer deep.
parameter | type |
---|---|
tbl | table |
Returns: table
local original = {'Itadori Yuuji', 'Nobara Kugisaki'}
local duplicate = table.copy(original)
table.insert(duplicate, 'Ryomen Sukuna')
print(duplicate) --> {'Itadori Yuuji', 'Nobara Kugisaki', 'Ryomen Sukuna'}
Returns a copy of table, recursively. If a table is encountered, it is recursively deep-copied. Metatables are not copied.
parameter | type |
---|---|
tbl | table |
Returns: table
local data = {
Cash = 69,
Gems = 420,
Inventory = {
'very cool item #1',
'very cool item #2'
}
}
local duplicate = table.deepCopy(data)
duplicate.Cash = 100
duplicate.Gems = 500
table.insert(data.Inventory, 'epic item')
print(duplicate) --> { Cash = 100, Gems = 500, Inventory = { 'very cool item #1', 'very cool item #1', 'epic item' } }
Reverses the element of an array-like table in place.
parameter | type |
---|---|
tbl | table |
Returns: nil
local array = {1, 2, 3, 4, 5}
table.reverse(array)
print(array) --> {5, 4, 3, 2, 1}
Returns a copy of an array-like table with its element in reverse order. The original remains unchanged.
parameter | type |
---|---|
tbl | table |
Returns: nil
local array = {1, 2, 3, 4, 5}
local reversed = table.reversed(array)
print(reversed) --> {5, 4, 3, 2, 1}
Returns a new array-like table where all of its values are the keys of the original table.
parameter | type |
---|---|
tbl | table |
Returns: table
local dict = {
Player1 = {10, 300, 40},
Player2 = {50, 1000, 30},
}
print(table.keys(dict)) --> {'Player1', 'Player2'}
Returns a new array-like table where all of its values are the values of the original table.
parameter | type |
---|---|
tbl | table |
Returns: table
local dict = {
Player1 = {10, 300, 40},
Player2 = {50, 1000, 30},
}
print(table.values(dict)) --> {{10, 300, 40}, {50, 1000, 30}}
Returns a random (index, value) pair from an array-like table.
parameter | type |
---|---|
tbl | table |
Returns: number, any
local array = {'Samsung', 'ASUS', 'Lenovo', 'HP'}
print(table.randomIpair(array)) --> 2 ASUS
Returns a random (key, value) pair from a table.
parameter | type |
---|---|
tbl | table |
Returns: any, any
local dict = {
Player1 = 'John Wick',
Player2 = 'Joe Mama',
Player3 = 'Peter Griffin',
Player4 = 'Barry',
}
print(table.randomPair(array)) --> Player3 Peter Griffin
Returns a copy of an array-like table sorted using Lua's table.sort
.
parameter | type |
---|---|
tbl | table |
Returns: table
local rates = {1, 3, 2, 6, 4, 5, 7, 5, 3}
local sorted = table.sorted(rates, function(a, b) return a < b end)
print(sorted) --> {1, 2, 3, 3, 4, 5, 5, 6, 7}
Returns a new array-table that is a slice of the original, defined by the start and stop bounds and the step size. Default start, stop, and step values are 1, #tbl, and 1 respectively.
parameter | type | optional |
---|---|---|
tbl | table | |
start | number | ✔️ |
stop | number | ✔️ |
step | number | ✔️ |
Returns: table
local strings = {'when', 'the', 'imposter', 'is', 'sus'}
local duplicate = table.slice(strings, 1, 5, 2)
print(duplicate) --> {'when', 'imposter', 'sus'}
Iterates through a table until a value satisfies the test function.
The value
is returned if one of the value satisfies the test function. Otherwise, nil
is returned.
parameter | type |
---|---|
tbl | table |
testFn | function |
parameter | type | optional |
---|---|---|
value | any | |
key | any | ✔️ |
Returns: any
local strings = {'when', 'the', 'imposter', 'is', 'sus'}
local testFn = function(str) return #str > 5
print(table.findValue(strings, testFn)) --> 'imposter'
Iterates through a table until a index satisfies the test function.
The key
is returned if one of the index satisfies the test function. Otherwise, nil
is returned.
parameter | type |
---|---|
tbl | table |
testFn | function |
parameter | type | optional |
---|---|---|
value | any | |
key | any | ✔️ |
Returns: any
local strings = {'when', 'the', 'imposter', 'is', 'sus'}
local testFn = function(str) return #str > 5
local index = table.findIndex(strings, testFn)
print(index) --> 3
print(strings[index]) --> imposter
Returns a new table containing all elements of the calling table for which provided filtering function returns true
.
parameter | type |
---|---|
tbl | table |
filterFn | function |
parameter | type | optional |
---|---|---|
value | any | |
key | any | ✔️ |
Returns: table
local scores = {Paul = 75, John = 70, Walker = 90, Bruce = 70, Clark = 69, Stark = 100, Steve = 85}
local function filter(score)
return score >= 80
end
print(table.filter(scores, filter)) --> {Walker = 90, Stark = 100, Steve = 85}
Returns a new table containing the results of calling a function on every element in this table.
parameter | type |
---|---|
tbl | table |
mapFn | function |
parameter | type | optional |
---|---|---|
value | any | |
key | any | ✔️ |
Returns: table
local array = {2, 4, 6, 8, 10}
local function mapFn(n)
return n * 2
end
print(table.map(array, mapFn)) --> {4, 8, 12, 16, 20}
Iterates through the table and run the test function to to every element to check if it satisfies the check. If all elements satisfies the check, it returns true
. Otherwise it returns false
.
parameter | type |
---|---|
tbl | table |
testFn | function |
parameter | type | optional |
---|---|---|
value | any | |
key | any | ✔️ |
Returns: boolean
local array = {'John', 'Paul', 'Bob', 'Walker', 'Steve', 'Alex'}
local function strCheck(str)
return type(str) == 'string'
end
print(table.every(array, strCheck)) --> true
Returns a new table that is the table joined with other table(s).
parameter | type |
---|---|
...tbl | table |
Returns: table
local team_A= {'John', 'Paul', 'Bob'}
local team_B = {'Walker', 'Steve', 'Alex'}
local all_players = table.merge(team_A, team_B)
print(all_players) --> {'John', 'Paul', 'Bob', 'Walker', 'Steve', 'Alex'}
Changes all elements in an array-like table to a static value from start index (default: 1
) to an end index (default: #tbl
). It returns the modified array-like table.
parameter | type |
---|---|
tbl | table |
Returns: table
print(table.fill({1, 2, 3, 4, 5, 6}, 0)) --> {0, 0, 0, 0, 0, 0}
print(table.fill({1, 2, 3, 4, 5, 6}, 6, 1, 3)) --> {1, 6, 6, 4, 5, 6}
print(table.fill({1, 2, 3, 4, 5, 6}, 69, 2, 5)) --> {1, 69, 69, 69, 69, 6}
Returns a new copy of the original array-like table and removes duplicate elements that exists on that table.
parameter | type |
---|---|
tbl | table |
Returns: table
local messy_array = {1, 1, 2, 3, 5, 5, 4, 'A', 'A', 'B', 'C', 'A', 'B', 'A'}
local clean_array = table.removeDupes(messyArray)
print(clean_array) --> {1, 2, 3, 5, 4, 'A', 'B', 'C'}