https://www.wikimanche.fr/index.php?title=Module:Outils&feed=atom&action=historyModule:Outils - Historique des versions2024-03-29T06:48:06ZHistorique des versions pour cette page sur le wikiMediaWiki 1.41.0https://www.wikimanche.fr/index.php?title=Module:Outils&diff=365536&oldid=prevMomo50 : Création2019-03-04T22:52:06Z<p>Création</p>
<p><b>Nouvelle page</b></p><div>local Outils = { }<br />
<br />
<br />
--[[<br />
trim nettoie un paramètre non nommé (supprime les espaces et retours ligne au début et à la fin)<br />
retourne nil si le texte est vide ou n'est pas du texte. Les nombres ne sont PAS considérés <br />
comme du texte.<br />
]]<br />
function Outils.trim( texte )<br />
if type( texte ) == 'string' and texte ~= '' then<br />
texte = texte:gsub( '^%s*(%S?.-)%s*$', '%1' )<br />
if texte ~= '' then<br />
return texte<br />
end<br />
end<br />
return nil<br />
end<br />
<br />
<br />
--[[<br />
validTextArg renvoit le premier paramètre chaine non vide<br />
Paramètre : <br />
1 - tableau contenant tous paramètres<br />
2, ... - les noms des paramètres qui doivent êtres testés.<br />
]]<br />
function Outils.validTextArg( args, name, ... ) <br />
local texte = Outils.trim( args[name] )<br />
if texte then<br />
return texte<br />
end<br />
if select( '#', ... ) > 0 then<br />
return Outils.validTextArg( args, ... )<br />
end<br />
return nil<br />
end<br />
<br />
<br />
--[[<br />
notEmpty renvoie le premier paramètre non vide ou nul. <br />
Paramètre : <br />
1, ... - les variables qui doivent êtres testés.<br />
]]<br />
function Outils.notEmpty( var, ... )<br />
local texte = Outils.trim( var )<br />
if texte then<br />
return texte<br />
end<br />
<br />
local tvar = type( var )<br />
<br />
if tvar == 'table' then<br />
local nextFunc = pairs( var ) -- n'utilise pas next car non défini par mw.loadData<br />
if nextFunc( var ) ~= nil then<br />
return var<br />
end <br />
elseif var == true or ( tvar == 'number' and var ~= 0 ) or tvar == 'function' then<br />
return var<br />
end<br />
<br />
if select( '#', ... ) > 0 then<br />
return Outils.notEmpty( ... )<br />
end<br />
end<br />
<br />
<br />
--[[<br />
extractArgs permet de récupérer les arguements du modèle, <br />
ou la table transmise à la fonction par une autre fonction d'un module<br />
Paramètres : <br />
1 - un objet frame ou une table contenant les paramètre<br />
2, ... - une liste de nom de paramètre pour déterminé si les paramètres sont transmis <br />
par #invoke. Le premier paramètre de frame sera systématiquement testé.<br />
]]<br />
function Outils.extractArgs ( frame, ... )<br />
if type( frame ) == 'table' then<br />
if type( frame.getParent ) == 'function' then<br />
if Outils.notEmpty( frame.args.invokeArgsOnly ) then<br />
return frame.args<br />
else<br />
local args = frame:getParent().args;<br />
for k,v in pairs( frame.args ) do<br />
args[k] = v;<br />
end<br />
return args<br />
end<br />
else<br />
return frame <br />
end<br />
else<br />
return { frame, ... }<br />
end<br />
end<br />
<br />
<br />
return Outils</div>Momo50