Sandbox: Lua: Table - ov-studio/Vital.sandbox GitHub Wiki

━ What's the Objective?

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.

━ APIs

ℹ️ Note:

Our extensions are written with backwards compatibility in mind; Expect your previous code to work as usual! However we recommend upgrading to newer syntax.

━ table.length() (Shared)

@Objective: Retrieves length of specified table.
--Note: This takes nil/false values into consideration too!
local table: result = table.length(
  table: baseTable
)

━ table.pack() (Shared)

@Objective: Packs all specified values into an ordered table.
--Note: This preserves even nil/false values!
local table: result = table.pack(
  ~: ...values
)

━ table.unpack() (Shared)

@Objective: Unpacks all ordered values from an existing table.
--Note: This unpacks even nil/false values!
local ~: ...values = table.unpack(
  table: baseTable
)

━ table.encode() (Shared)

@Objective: Encodes specified table into a valid string.
local string: baseString = table.encode(
  table: baseTable
  string: encoding --(Optional): If left unspecified, it'd falllback to vcl. Supported encodings: vcl & json.
)

━ table.decode() (Shared)

@Objective: Decodes a valid encoded string into a table.
local table: baseTable = table.decode(
  table: baseString,
  string: encoding --(Optional): If left unspecified, it'd falllback to vcl. Supported encodings: vcl & json.
)

━ table.clone() (Shared)

@Objective: Clones an existing table.
local table: result = table.clone(
  table: baseTable,
  bool: isRecursive --(Optional) Enabling this recursively clones all deeply nested tables. (If any)
)

━ table.inspect() (Shared)

@Objective: Retrieves human readable format of the table.
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.
)

━ table.print() (Shared)

@Objective: Prints human readable format of the table.
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.
)

━ table.keys() (Shared)

@Objective: Retrieves a list of keys an existing table.
--Note: This retrieves keys even the ones allocated w/ nil/false values!
local table: result = table.keys(
  table: baseTable
)

━ table.insert() (Shared)

@Objective: Inserts specified value into an existing table.
--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.
)

━ table.remove() (Shared)

@Objective: Removes specified index from an existing table.
local bool: result = 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.
)

━ table.forEach() (Shared)

@Objective: Loops throughout the numeric indexes of the table while retrieving its value.
--Note: This works even for the indexes allocated w/ nil/false values!
local bool: result = table.forEach(
  table: baseTable,
  function: exec(int: index, ~: value)
)