table library
Extensions of the table library and the shorthand helpers that go with it.
Adds functional helpers (table.map, table.select, table.reduce, table.partition and
others), filtering with table.keep_if and table.delete_if, comparison, conversion
between hashes and arrays, and serialization through SFS with a fallback to JSON. a turns
a table into an array that has the table library as its methods, w and wk build arrays
and hashes out of words, and print_table replaces PrintTable. table.Merge is replaced
with a version that leaves the class field of objects alone.
Summary
Functions
__index of both tables alone and skips the class key and any field of from that refers to from itself.Functions
Removes the entries for which the callback returns true from the table itself.
Parameters
- tab Map/List table to modify
- callback Function callback(key, value), returns true to remove the entry
Returns
- Map/List the same table
See also
Returns a copy of the table without the entries for which the callback returns true. The entries keep their keys and the original table is not modified.
Parameters
- tab Map/List
- callback Function callback(key, value), returns true to leave the entry out
Returns
- Map/List the filtered copy
See also
Converts a string back into a table. Uses SFS at first, if it fails it falls back to JSON.
Parameters
- data String string to convert
Returns
- Map decoded table; an empty table if data is not a string, nil if it is neither valid SFS nor valid JSON
Checks whether two tables have the same contents. Nested tables are compared recursively.
Parameters
- tab1 Map/List
- tab2 Map/List
Returns
- Boolean true if the contents are equal, false if not or if either one is not a table
Returns the first element of an array.
Parameters
- tab List
Returns
- Any the first element, or nil if the array is empty
Flattens the tables nested inside of a table into a single array.
Tables that have a class field (objects) are treated as values.
Parameters
- tab Map/List
Returns
- List the flattened values
Returns the memory address of a table, which identifies it uniquely while it exists.
Parameters
- tab Map/List
Returns
- String the address without the 'table: 0x' prefix
- Number 1 if the prefix was removed
Concatenates all values of a table into a single string, including those of nested tables.
Parameters
- tab Map/List
- sep String optional, defaults to
''separator to put between the values
Returns
- String the concatenated values
Removes the entries for which the callback does not return true from the table itself.
Parameters
- tab Map/List table to modify
- callback Function callback(key, value), returns true to keep the entry
Returns
- Map/List the same table
See also
Returns a copy of the table that only has the entries for which the callback returns true. The entries keep their keys and the original table is not modified.
Parameters
- tab Map/List
- callback Function callback(key, value), returns true to keep the entry
Returns
- Map/List the filtered copy
See also
Returns the last element of an array.
Parameters
- tab List
Returns
- Any the last element, or nil if the array is empty
Creates an array out of the values a callback returns for every value of a table. Values for which the callback returns nil are left out.
local names = table.map(player.GetAll(), function(v) return v:name() end)
Parameters
- t Map/List table to go through
- c Function callback(value), returns the value to store or nil to skip it
Returns
- List values returned by the callback
See also
Creates an array out of the values a callback returns for every key-value pair of a table. Pairs for which the callback returns nil are left out.
local lines = table.map_kv({ a = 1, b = 2 }, function(k, v) return k..' = '..v end)
Parameters
- t Map/List table to go through
- c Function callback(key, value), returns the value to store or nil to skip it
Returns
- List values returned by the callback
Recursively merges the source table into the destination table, overwriting existing keys.
Replaces the built-in table.Merge. Unlike the built-in, a table stored under the class
key is assigned by reference instead of being merged.
Parameters
- dest Map table to merge into, modified in place
- source Map table to take the values from
Returns
- Map dest
Checks whether the callback returns a truthy value for none of the entries of the table.
Parameters
- tab Map/List
- callback Function callback(key, value)
Returns
- Boolean
Checks whether the callback returns a truthy value for exactly one entry of the table.
Parameters
- tab Map/List
- callback Function callback(key, value)
Returns
- Boolean
Splits the values of a table into two arrays: those for which the callback returns a truthy value and all the others.
local admins, others = table.partition(player.GetAll(), function(k, v) return v:IsAdmin() end)
Parameters
- tab Map/List
- callback Function callback(key, value)
Returns
- List values the callback accepted
- List values it did not
Creates an array of consecutive integers.
Parameters
- from Number first number
- to Number last number, inclusive
Returns
- List<Number>
Combines all elements of an array into a single value, by applying an operator to the running result and each element in order. The running result starts at 0.
table.reduce({ 1, 2, 3 }, '+') -- 6
table.reduce({ 1, 5, 3 }, function(result, v) return math.max(result, v) end) -- 5
Parameters
- tab List
- op String/Function one of '+', '-', '*', '/', '**', '^' and '%', or a callback(result, value) that returns the new result
Returns
- Any the final result, a Number for the built-in operators
Removes all functions from a table and from the tables nested in it. Modifies the table.
Parameters
- obj Any table to clean up, anything else is returned as-is
Returns
- Any the same object
Merges a table into another one like table.Merge, but leaves __index of both tables
alone and skips the class key and any field of from that refers to from itself.
Parameters
- to Map table to merge into, modified in place
- from Map table to take the values from
Returns
- Map to
Filters the values of a table with a callback, or collects a single field from every table inside of it.
local alive = table.select(player.GetAll(), function(v, k) return v:Alive() end)
local names = table.select({ { name = 'a' }, { name = 'b' } }, 'name') -- { 'a', 'b' }
Variants
table.select(t, what)- t Map/List table to filter
- what Function callback(value, key), the value is kept unless it returns false
table.select(t, what)- t Map/List table that consists of tables
- what String/Number key to read from every value that is a table
Returns
- List selected values
Converts a table into the string format.
Parameters
- tab Map/List table to convert
Returns
- String hex-encoded SFS data
- JSON if SFS fails, or an empty string if both fail or tab is not a table
Returns the part of an array between two indexes, both inclusive.
Parameters
- t List
- from Number first index
- to Number last index
Returns
- List the elements between from and to
Adds up all elements of an array.
Parameters
- tab List<Number>
Returns
- Number the sum, 0 for an empty array
Converts a hash into an array of { key, value } pairs.
table.to_array({ a = 1, b = 2 }) -- { { 'a', 1 }, { 'b', 2 } }, in no particular order
Parameters
- tab Map
Returns
- List<List> key-value pairs
See also
Converts an array of { key, value } pairs into a hash.
table.to_hash({ { 'a', 1 }, { 'b', 2 } }) -- { a = 1, b = 2 }
Parameters
- tab List<List> key-value pairs
Returns
- Map
See also
Returns a copy of the table without duplicate values. The values keep their original keys, so the copy of an array can have gaps in it.
Parameters
- tab Map/List
- condition Function optional, defaults to
nilcallback(value) that returns true for the values to leave out, used instead of the duplicate check
Returns
- Map/List table of unique values