Module table
This module provides powerfull methods to lua table injecting it inside that table, it's objective is to make easier to do basics operation over table instantied object.
The development of this module comes for the purpose of facilitate the programmer life giving then a way to write a code not replicating the same functions for every project that they do.
With this module programmer can have their code prettier and focused in whats really metters, that is the creative actions, and not thinking about the basics actions like get the tables size or merge 2 others.
Info:
- Copyright: 2021-2022
- Release: 1.0.0
- License: MIT
- Author: Denys G. Santos
Functions
void () | A void method that you can use to check it your table has this extention. |
info () | Provides some informations about this library. |
get (key) | Get element based on a given key. |
clone () | Create a deep copy of this table. |
merge (t, overwrite) | Merge this table with another one. |
equals (t) | Compare this table with another and return true in case of both are equals, other else return false. |
size () | Obtain the size of the table. |
tostring () | Returns a printable multiline string that map all pairs stored inse this table |
keys () | Obtains the table keyset. |
values () | Obtains the table values list. |
each (fn) | Execute a given function with the parameter 'key' and 'value' followed by any others your choose. |
eachk (fn) | Execute a given function with the parameter 'key' followed by any others your choose. |
eachi (fn) | Execute a given function with the parameter 'value' followed by any others your choose. |
iterator () | Get the iterator object from this table |
del (arg) | Remove the all elements passed by paramenter to this table |
Functions
- void ()
-
A void method that you can use to check it your table has this extention.
Usage:
local table = require "ptable" local tbl = table({}) tbl.void() --it'll do nothing
- info ()
-
Provides some informations about this library.
Returns:
-
string - the library informations
Usage:
local table = require "ptable" local tbl = table({}) tbl.info() --it'll return the information in string format
- get (key)
-
Get element based on a given key. The value returne always is a
table element or nil (in case of key is not present in the table).
Parameters:
- key any - some key
Returns:
-
any - the value stored or nil
Usage:
local table = require "ptable" local tbl = table({a = 1, b = 2, c = 3}) local value = tbl:get(a) -- it should return 1
- clone ()
-
Create a deep copy of this table.
Returns:
-
table - the new created table
Usage:
local table = require "ptable" local tbl = table({a = 1, b = 2, c = 3}) local clone = tbl:clone() -- it should return a new table equals the tbl
- merge (t, overwrite)
-
Merge this table with another one. There are two types of merging, the first one,
that is the default option, overwrite the intersecting elements (replace the
first one by the second), and the second their maintains (ignores the second's
intersection values).
It's important to know that this method doesn't return another table. Different of clone method, this one just actualize this active instance.
Parameters:
- t table - the table to be merged with this one
- overwrite boolean - inform if the data must be overwritten (option - default true)
Usage:
local table = require "ptable" local first_table = table({a = 1, b = 2, c = 3}) local cloned_table = first_table:clone() local other_table = table({b = 4, c = 5, d = 6}) first_table:merge(oth) -- 'first_table' == {a = 1, b = 4, c = 5, d = 6} cloned_table:merge(oth, false) -- 'cloned_table' == {a = 1, b = 2, c = 3, d = 6}
- equals (t)
-
Compare this table with another and return true in case of both are equals,
other else return false.
This function compare unically the key an value of the tables, ignoring completally the overload that both could had received.
Parameters:
- t table - the table to be compared
Returns:
-
boolean - true if both are equals, other else false
Usage:
local table = require "ptable" local tbl = table({a = 1, b = 2, c = 3}) local oth = table({a = 1, b = 2, c = 3}) local equals = tbl:equals(oth) -- it should return true
- size ()
-
Obtain the size of the table. As the lua has no simple way to get the lengh of, this
implementation use the count of all pairs inside that.
This function compare unically the number of pairs inside the tables, ignoring completally the overload that it should have received.
Returns:
-
integer - the size of the table
Usage:
local table = require "ptable" local tbl = table({a = 1, b = 2, c = 3}) local size = tbl:size() -- it must return 3
- tostring ()
-
Returns a printable multiline string that map all pairs stored inse this table
Returns:
-
string - the printable multiline string
Usage:
local table = require "ptable" local tbl = table({a = 1, b = 2, c = 3}) local str = tbl:tostring() -- it must return the printable string
- keys ()
-
Obtains the table keyset.
This function compare unically the data mapped in pairs inside the tables, ignoring completally the overload that it should have received.
Returns:
-
table - A table with the keyset
Usage:
local table = require "ptable" local tbl = table({a = 1, b = 2, c = 3}) local keys = tbl:keys() -- it must return {a, b, c}
- values ()
-
Obtains the table values list.
This function compare unically the data mapped in pairs inside the tables, ignoring completally the overload that it should have received.
Returns:
-
table - A table with the values list
Usage:
local table = require "ptable" local tbl = table({a = 1, b = 2, c = 3}) local keys = tbl:values() -- it must return {1, 2, 3}
- each (fn)
-
Execute a given function with the parameter 'key' and 'value' followed by any others your choose.
Parameters:
- fn function - the function to be executed over each pears
Usage:
local table = require "ptable" local tbl = table({a = 1, b = 2, c = 3}) local keys = tbl:each(print, "hello") -- it must print: -- a 1 hello -- b 2 hello -- c 3 hello
- eachk (fn)
-
Execute a given function with the parameter 'key' followed by any others your choose.
Parameters:
- fn function - the function to be executed over each key
Usage:
local table = require "ptable" local tbl = table({a = 1, b = 2, c = 3}) local keys = tbl:eachk(print, "hello") -- it must print: -- a hello -- b hello -- c hello
- eachi (fn)
-
Execute a given function with the parameter 'value' followed by any others your choose.
Parameters:
- fn function - the function to be executed over each value
Usage:
local table = require "ptable" local tbl = table({a = 1, b = 2, c = 3}) local keys = tbl:eachi(print, "hello") -- it must print: -- 1 hello -- 2 hello -- 3 hello
- iterator ()
-
Get the iterator object from this table
Returns:
-
any - the iterator object
Usage:
local table = require "ptable" local tbl = table({a = 1, b = 2, c = 3}) local it = tbl:iterator()
- del (arg)
-
Remove the all elements passed by paramenter to this table
Parameters:
- arg any the list of elements to be deleted
Usage:
local table = require "ptable" local tbl = table({a = 1, b = 2, c = 3}) tbl:remove(a, b) -- the table will be {c = 3}