Module:Paramtest: Difference between revisions

osrsw>IN008
No edit summary
osrsw>Spineweilder
No edit summary
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 10: Line 27:


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


Line 17: Line 34:
--
--


function p.default_to(param, default)
function p.default_to(arg, default)
if param and param:find('%S') then
if string.find(arg or '', '%S') then
return param
return arg
else
else
return default
return default
Line 31: Line 48:
local ret = {}
local ret = {}
for i, v in ipairs(...) do
for i, v in ipairs(...) do
if v[1] and v[1]:find('%S') then
if string.find(v[1] or '', '%S') then
table.insert(ret,v[1])
table.insert(ret,v[1])
else
else