Module:Outils

De Vouiquipèdia, l’enciclopèdia abada.

La documentation pour ce module peut être créée à Module:Outils/doc

local Outils = { }


--[[
	trim netèye un paramètro pas apelâ (enléve los èspâços et retôrns legne u comencement et a la fin)
	retôrne na lista voueda se lo tèxto est vouedo est gins de tèxto. Los nombros sont PAS considèrâs
	coment de tèxto.
]]
function Outils.trim( texto )
	if type( texto ) == 'string' and texto ~= '' then
		texto = texto:gsub( '^%s*(%S?.-)%s*$', '%1' )
		if texto ~= '' then
			return texto
		end
	end
	return nil
end


--[[
	validTextArg renvoit le premier paramètre chaine non vide
	Paramètre :
		1 - tableau contenant tous paramètres
		2, ... - les noms des paramètres qui doivent êtres testés.
]]
function Outils.validTextArg( args, name, ... )
	local texto = Outils.trim( args[name] )
	if texto then
		return texto
	end
	if select( '#', ... ) > 0 then
		return Outils.validTextArg( args, ... )
	end
	return nil
end


--[[
	notEmpty renvoie le premier paramètre non vide ou nul.
	Paramètre :
		1, ... - les variables qui doivent êtres testés.
]]
function Outils.notEmpty( var, ... )
	local texto = Outils.trim( var )
	if texto then
		return texto
	end

	local tvar = type( var )

	if tvar == 'table' then
		local nextFunc = pairs( var )   -- n'utilise pas next car non défini par mw.loadData
		if nextFunc( var ) ~= nil then
			return var
		end
	elseif var == true or ( tvar == 'number' and var ~= 0 ) or tvar == 'function' then
		return var
	end

	if select( '#', ... ) > 0 then
		return Outils.notEmpty( ... )
	end
end


--[[
	extractArgs permet de récupérer les arguments du modèle,
	ou la table transmise à la fonction par une autre fonction d'un module
	Paramètre :
		1 - un objet frame ou une table contenant les paramètres
]]
function Outils.extractArgs ( frame )
	if type( frame.getParent ) == 'function' then
		local args = frame:getParent().args
		for k,v in pairs( frame.args ) do
			args[k] = v;
		end
		return args
	else
		return frame
	end
end


return Outils