La documentation pour ce module peut être créée à Module:Table/doc
local p = {}
function p.search( f )
local args = p.trimAll( p.getArgs( f ) )
if args.index then
for i, j in pairs( args ) do
if args.object == j then
return i
end
end
else
local k = 1
repeat
if args.object == args[k] then
return k
end
k = k + 1
until not args[k]
end
return nil
end
-- Get parameters of the template and the article from the frame (usually f)
function p.getArgs( f )
local args = {}
if f.args then
-- Add template parameters
for i, j in pairs( f.args ) do
args[i] = j
end
-- Add transclusion parameters (parent), overwrite template parameters
for k, l in pairs( f:getParent().args ) do
args[k] = l
end
else
args = f
end
return args
end
-- Do mw.text.trim to all values in a table
function p.trimAll( f, toRemove, keepEmptyParams )
local g = {}
for index, object in pairs( f ) do
if type( object ) == 'string' then
-- Trim
if object ~= nil and mw.text.trim( object ) ~= '' then
if toRemove then
g[index] = mw.text.trim( object, toRemove )
else
g[index] = mw.text.trim( object )
end
end
-- Remove empty values ?
if mw.text.trim( object ) == '' and not keepEmptyParams then
g[index] = nil
end
else
-- Do nothing if the content is not a string
g[index] = object
end
end
return g
end
function p.contains( t, e, c )
if c then
for _, v in pairs( t ) do
if v[c] == e[c] then
return true
end
end
else
for _, v in pairs( t ) do
if v == e then
return true
end
end
end
return false
end
-- Do string.gsub to all values in a table
function p.gsub( t, pattern, repl )
local result = {}
for k, v in pairs( t ) do
if type( v ) == 'string' then
result[k] = string.gsub( v, pattern, repl )
else
result[k] = v
end
end
return result
end
-- Do string.gsub to all indexes in a table
function p.igsub( t, pattern, repl )
local result = {}
for k, v in pairs( t ) do
result[string.gsub( k, pattern, repl )] = v
end
return result
end
function p.key( t, e )
for k, v in pairs( t ) do
if v == e then
return k
end
end
return 0
end
function p.map( t, f )
local result = {}
for k, v in pairs( t ) do
result[k] = f( v )
end
return result
end
function p.merge( ... )
local result = {}
for _, table in ipairs( arg ) do
for k, v in pairs( table ) do
result[k] = v
end
end
return result
end
function p.trim( t, toRemove, keepEmptyParams )
local result = {}
for k, v in pairs( t ) do
if type( v ) == 'string' then
result[k] = mw.text.trim( v ) ~= '' and ( toRemove and mw.text.trim( v, toRemove ) or mw.text.trim( v ) ) or keepEmptyParams and ''
else
result[k] = v
end
end
return result
end
function p.unique( t, c )
local result = {}
for _, v in ipairs( t ) do
if not p.contains( result, v, c ) then
result[#result + 1] = v
end
end
return result
end
return p