Skip to content

Library: Table

ᴏᴠ ━ ᴀɴɪꜱᴀ edited this page Dec 12, 2024 · 24 revisions

» Overview

The Lua programming language relies on Tables to simulate Arrays, Objects, and Classes, highlighting their significant utility. Our modifications to the inherent table module were necessary to address challenges encountered when inserting, removing, or unpacking indices associated with nil values. While this issue has been resolved in more recent Lua versions, MTA remains constrained to an earlier version for compatibility purposes.

» Importing

It's already imported by default whenever you import assetify. You need to declare the below globally only once:

loadstring(exports.assetify_library:import())()

» APIs

ℹ️ Our extensions are designed with backward compatibility in mind; your existing code is expected to function as intended. Nonetheless, we recommend transitioning to newer syntax.

  • table.length() - shared

    Returns the length of a specified table.

    local int: result = table.length(
       table: baseTable
    )
  • table.pack() - shared

    Encapsulates all specified values into an ordered table.
    Retains nil values as well!

    local table: result = table.pack(
       ~: ...values
    )
  • table.unpack() - shared

    Extracts all ordered values from a specified table.
    Retains nil values as well!

    local ~: ...values = table.unpack(
       table: baseTable,
       int: length --Optional
    )
  • table.encode() - shared

    Encodes specified table into a valid string format.

    local string: baseString = table.encode(
       table: baseTable,
       string: encoding --Optional: Defaults to vcl if unspecified. Supported encodings: vcl & json
    )
  • table.decode() - shared

    Decodes a valid encoded string back into a table.

    local table: baseTable = table.decode(
       table: baseString,
       string: encoding --Optional: Defaults to vcl if unspecified. Supported encodings: vcl & json
    )
  • table.clone() - shared

    Creates a copy of an existing table.

    local table: result = table.clone(
       table: baseTable,
       bool: isRecursive --Optional: When enabled, this recursively clones all nested tables
    )
  • table.inspect() - shared

    Obtains a human-readable representation of the table.

    local string: result = table.inspect(
       table: baseTable,
       bool: showHidden, --Optional: When enabled, this recursively displays all available data
       int: limit --Optional: Sets a recursive limit, defaulting to 1
    )
  • table.print() - shared

    Outputs a human-readable representation of the table.

    local string: result = table.print(
       table: baseTable,
       bool: showHidden, --Optional: When enabled, this recursively displays all available metadata (if any)
       int: limit --Optional: Sets a recursive limit, defaulting to 1
    )
  • table.keys() - shared

    Retrieves a list of keys from an existing table.

    --Note: This retrieves keys, including those assigned nil/false values!
    local table: result = table.keys(
       table: baseTable
    )
  • table.insert() - shared

    Inserts a specified value into an existing table.

    --Syntax #1:
    local bool: result = table.insert(
       table: baseTable,
       int: index,
       ~: value, --Can be of any type, including nil/false. All adjacent elements will be shifted to maintain order
       bool: isForced --Optional: When enabled, it forces the value to be inserted
    )
    
    --Syntax #2:
    --Note: This syntax appends values at the end of the table by default.
    local bool: result = table.insert(
       table: baseTable,
       ~: value --Can be of any type, including nil/false. All adjacent elements will be shifted to maintain order
    )
  • table.remove() - shared

    Removes a specified index from an existing table.

    local ~: value = table.remove(
       table: baseTable,
       int: index --Operates on indices with any value type, including nil/false. All adjacent elements will be pulled up to maintain order
    )
  • table.forEach() - shared

    Iterates through the numeric indices of the table while retrieving their values.

    --Note: This works even for indices assigned nil/false values!
    local bool: result = table.forEach(
       table: baseTable,
       function: exec(int: index, ~: value)
    )