Module:Paramtest: Difference between revisions

osrsw>The Mol Man
m Protected "Module:Paramtest" (‎[edit=sysop] (indefinite))
m 6 revisions imported
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
--[[
{{Helper module
|name=Paramtest
|fname1 = is_empty(arg)
|ftype1 = String
|fuse1 = Returns true if arg is not defined or contains only whitespace
|fname2 = has_content(arg)
|ftype2 = String
|fuse2 = Returns true if arg exists and does not only contain whitespace
|fname3 = default_to(arg1,arg2)
|ftype3 = String, Any value
|fuse3 = If arg1 exists and does not only contain whitespace, the function returns arg1, otherwise returns arg2
|fname4 = defaults{ {arg1,arg2},...}
|ftype4 = {String, Any value}...
|fuse4 = Does the same as <code>default_to()</code> run over every table passed; for technical reasons, all <code>nil</code> are replaced with <code>false</code>
}}
--]]
--
--
-- Tests basic properties of parameters
-- Tests basic properties of parameters
Line 9: Line 26:
--
--


function p.is_empty( arg )
function p.is_empty(arg)
if not arg or not arg:find('%S') then
return not string.find(arg or '', '%S')
return true
end
 
return false
end
end


--
--
-- Tests if the parameter has content
-- Returns the parameter if it has any content, the default (2nd param)
-- The same as !is_empty, but this is more readily clear
--
--


function p.has_content( arg )
function p.default_to(arg, default)
if not arg or not arg:find('%S') then
if string.find(arg or '', '%S') then
return false
return arg
else
return default
end
end
return true
end
end


--
--
-- Tests if the parameter has been defined (regardless of content)
-- Returns a list of paramaters if it has any content, or the default
--
--
 
function p.defaults(...)
function p.is_defined( arg )
local ret = {}
if not arg then
for i, v in ipairs(...) do
return false
if string.find(v[1] or '', '%S') then
else
table.insert(ret,v[1])
return true
else
-- or false, because nil is removed
table.insert(ret,v[2] or false)
end
end
end
return unpack(ret)
end
end


--
--
-- Removes leading and trailing whitespace from the parameter
-- Tests if the parameter has content
-- Which is unfortunately not removed when passed through invoke
-- The same as !is_empty, but this is more readily clear
--
--


function p.trim( arg )
function p.has_content(arg)
return string.gsub( arg, '^%s*(%S+)%s*$', '%1')
return string.find(arg or '', '%S')
end
end


Line 55: Line 71:
--
--


function p.ucfirst( arg )
function p.ucfirst(arg)
if not arg or arg:len() == 0 then
if not arg or arg:len() == 0 then
return nil
return nil
Line 69: Line 85:
--
--


function p.ucflc( arg )
function p.ucflc(arg)
if not arg or arg:len() == 0 then
if not arg or arg:len() == 0 then
return nil
return nil