-
-
Notifications
You must be signed in to change notification settings - Fork 2
Library: Table
Aviril edited this page Nov 29, 2024
·
24 revisions
Lua's way to simulate Arrays
, Objects
& Classes
is possible only via Tables
which pretty much explains how much worth it is. This custom modification of the built in table
module was essentially needed for us due to artifacts when trying to insert/remove/unpack/pack indexes with nil/false values. This issue is fixed on newer Lua versions; However MTA is bound to an older version for compatibility reasons.
It gets imported automatically by default whenever you initialize assetify
.
--Declare it globally only once
loadstring(exports.assetify_library:import())()
Our extensions are written with backwards compatibility in mind; Expect your previous code to work as usual! However we recommend upgrading to newer syntax.
local int: result = table.length(
table: baseTable
)
--Note: This preserves even nil/false values!
local table: result = table.pack(
~: ...values
)
--Note: This unpacks even nil/false values!
local ~: ...values = table.unpack(
table: baseTable,
int: length --(Optional).
)
local string: baseString = table.encode(
table: baseTable
string: encoding --(Optional) If left unspecified, it'd fallback to vcl. Supported encodings: vcl & json.
)
local table: baseTable = table.decode(
table: baseString,
string: encoding --(Optional) If left unspecified, it'd fallback to vcl. Supported encodings: vcl & json.
)
local table: result = table.clone(
table: baseTable,
bool: isRecursive --(Optional) Enabling this recursively clones all deeply nested tables. (If any)
)
local string: result = table.inspect(
table: baseTable,
bool: showHidden, --(Optional) Enabling this recursively prints all available metadatas. (If any)
int: limit --(Optional) Recursive limit. By default its restricted to 1.
)
local string: result = table.print(
table: baseTable,
bool: showHidden, --(Optional) Enabling this recursively prints all available metadatas. (If any)
int: limit --(Optional) Recursive limit. By default its restricted to 1.
)
--Note: This retrieves keys even the ones allocated w/ nil/false values!
local table: result = table.keys(
table: baseTable
)
--Syntax #1:
local bool: result = table.insert(
table: baseTable,
int: index,
~: value, --This can be of any type even nil/false! All adjacent siblings will be pushed down the stack to maintain ordering.
bool: isForced --(Optional) Enabling this forces the value to be inserted.
)
--Syntax #2:
--Note: This syntax is used to append values at the end of the stack by default.
local bool: result = table.insert(
table: baseTable,
~: value --This can be of any type even nil/false! All adjacent siblings will be pushed down the stack to maintain ordering.
)
local ~: value = table.remove(
table: baseTable,
int: index --This works with indexes having values of any type even nil/false! All adjacent siblings will be pulled up the stack to maintain ordering.
)
--Note: This works even for the indexes allocated w/ nil/false values!
local bool: result = table.forEach(
table: baseTable,
function: exec(int: index, ~: value)
)