Logo

DIVERSITY

Documentation

Library

Table

Utility functions for manipulating Lua tables.

Shared Functions


Contains

Checks if a value exists in a table.

client.lua
local isContained = dLib.Table.Contains(tbl, value)
ArgumentTypeDescription
tbl
table
The table to search within.
value
any
The value to check for existence in the table.
Returned DataTypeDescription
isContained
boolean
True if the value exists in the table, false otherwise.

Matches

Compares two tables for equality, checking that both tables contain the same key-value pairs.

client.lua
local matches = dLib.Table.Matches(tableOne, tableTwo)
ArgumentTypeDescription
tableOne
table
The first table to compare.
tableTwo
table
The second table to compare.
Returned DataTypeDescription
matches
boolean
True if both tables contain the same key-value pairs, false otherwise.

DeepClone

Creates a deep copy of a table, recursively cloning all nested tables.

client.lua
local clonedTable = dLib.Table.DeepClone(tbl)
ArgumentTypeDescription
tbl
table
The table to be deep-cloned.
Returned DataTypeDescription
clonedTable
table
A new table that is a deep copy of the input table.

Merge

Merges two tables, combining their key-value pairs. Duplicate keys can optionally be added together.

client.lua
local tableOne = dLib.Table.Merge(tableOne, tableTwo)
ArgumentTypeDescription
tableOne
table
The first table; its key-value pairs will be the base for the merge.
tableTwo
table
The second table; its key-value pairs will be merged into the first table.
addDuplicateNumbers?
boolean
If true, numeric values for duplicate keys will be added together instead of overwritten.
Returned DataTypeDescription
tableOne
table
The modified first table after merging in the key-value pairs from the second table.

Shuffle

Randomizes the order of elements in a table.

client.lua
local shuffledTable = dLib.Table.Shuffle(tbl)
ArgumentTypeDescription
tbl
table
The table whose elements will be shuffled.
copy?
boolean
If true, shuffle a copy instead of the original table.
rnd?
function
Optional custom random function; defaults to math.random.
Returned DataTypeDescription
shuffledTable
table
The table with its elements in randomized order.

Map

Transforms each element in a table using a callback function and returns a new table with the results.

client.lua
local mappedTable = dLib.Table.Map(tbl, cb)
ArgumentTypeDescription
tbl
table
The table whose elements will be transformed.
cb
function
Function to apply to each element; receives value and key, returns new value.
Returned DataTypeDescription
mappedTable
table
New table containing the transformed elements.

Count

Returns the number of elements in a table.

client.lua
local count = dLib.Table.Count(tbl)
ArgumentTypeDescription
tbl
table
The table whose elements will be counted.
Returned DataTypeDescription
count
number
Total number of elements in the table.

Was this page helpful?