FANDOM


-- <nowiki>
--------------------------------------------------------------------------------
-- This module includes a number of functions for dealing with Lua tables.
--
-- @see [[Wikipedia:Module:TableTools]] for a similar module.
--------------------------------------------------------------------------------
local p = {}
 
local checkType = require('libraryUtil').checkType
 
--------------------------------------------------------------------------------
-- Returns a new table with all parameters stored into keys 1, 2, etc. and with
-- a field `n` with the total number of parameters. Note that the resulting
-- table may not be a sequence.
--
-- @see <http://www.lua.org/manual/5.2/manual.html#pdf-table.pack>
--------------------------------------------------------------------------------
function p.pack(...)
    return {n = select('#', ...), ...}
end
 
--------------------------------------------------------------------------------
-- Returns the first `n` arguments in `...`. If `n` is negative, arguments are
-- counted from the end of the table.
--
-- @see [[Lua reference manual/Standard libraries#select]]
-- @see <http://lua-users.org/wiki/VarargTheSecondClassCitizen>
--------------------------------------------------------------------------------
function p.selectFirst(n, ...)
    checkType('Dev:TableTools.selectFirst', 1, n, 'number')
 
    local function err()
        error('bad argument #1 to \'Dev:TableTools.selectFirst\' ' ..
              '(index out of range)')
    end
 
    local function recurse(index, next, ...)
        if index == 0 then
            return
        end
 
        return next, recurse(index - 1, ...)
    end
 
    n = math.modf(n)
 
    local count = select("#", ...)
 
    if -count > n then
        err()
    elseif -1 > n and n >= -count then
        return recurse(count + 1 + n, ...)
    elseif n == -1 then
        return ...
    elseif n == 0 then
        err()
    elseif n == 1 then
        return (...)
    elseif 1 < n and n <= count then
        return recurse(n, ...)
    elseif count < n then
        return ...
    end
end
 
--------------------------------------------------------------------------------
-- Returns `true` if a given table is a sequence.
--
-- @see <http://stackoverflow.com/a/6080274>
--------------------------------------------------------------------------------
function p.isSequence(t)
    checkType('Dev:TableTools.isSequence', 1, t, 'table')
 
    local i = 1
 
    for _ in pairs(t) do
        if t[i] == nil then
            return false
        end
 
        i = i + 1
    end
 
    return true
end
 
--------------------------------------------------------------------------------
-- Returns the number of elements in a table, even if it is not a sequence.
--
-- @see <http://stackoverflow.com/a/2705804>
--------------------------------------------------------------------------------
function p.size(t)
    checkType('Dev:TableTools.size', 1, t, 'table')
 
    local i = 0
 
    for _ in pairs(t) do
        i = i + 1
    end
 
    return i
end
 
--------------------------------------------------------------------------------
-- Returns `true` if a given table contains a certain element.
--
-- @see <http://stackoverflow.com/q/2282444>
--------------------------------------------------------------------------------
function p.includes(t, elm)
    checkType('Dev:TableTools.includes', 1, t, 'table')
 
    for _, v in pairs(t) do
        if v == elm then
            return true
        end
    end
 
    return false
end
 
--------------------------------------------------------------------------------
-- Merges the content of the second table with the content in the first one.
--
-- @see <http://wiki.garrysmod.com/page/table/Merge>
--------------------------------------------------------------------------------
function p.merge(dest, source)
    checkType('Dev:TableTools.merge', 1, dest, 'table')
    checkType('Dev:TableTools.merge', 2, source, 'table')
 
    for k, v in pairs(source) do
        if type(v) == 'table' and type(dest[k]) == 'table' then
            -- Don't overwrite one table with another; instead merge them
            -- recurisvely.
            p.merge(dest[k], v)
        else
            dest[k] = v
        end
    end
 
    return dest
end
 
return p
 
-- </nowiki>
-- (Add categories here.)

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.