Editing Module:Paramtest

Jump to navigation Jump to search
Warning: You are not logged in. Once you make an edit, a temporary account will be created for you. Learn more. Log in or create an account to continue receiving notifications after this account expires, and to access other features.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision Your text
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 26: Line 9:
--
--


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


Line 34: Line 21:
--
--


function p.default_to(arg, default)
function p.default_to(param, default)
if string.find(arg or '', '%S') then
if param and param:find('%S') then
return arg
return param
else
else
return default
return default
Line 43: Line 30:


--
--
-- Returns a list of paramaters if it has any content, or the default
-- Tests if the parameter has content
-- The same as !is_empty, but this is more readily clear
--
--
function p.defaults(...)
 
local ret = {}
function p.has_content( arg )
for i, v in ipairs(...) do
if not arg or not arg:find('%S') then
if string.find(v[1] or '', '%S') then
return false
table.insert(ret,v[1])
else
-- or false, because nil is removed
table.insert(ret,v[2] or false)
end
end
end
return unpack(ret)
 
return true
end
end


--
--
-- Tests if the parameter has content
-- Tests if the parameter has been defined (regardless of content)
-- The same as !is_empty, but this is more readily clear
--
 
function p.is_defined( arg )
if not arg then
return false
else
return true
end
end
 
--
-- Removes leading and trailing whitespace from the parameter
-- Which is unfortunately not removed when passed through invoke
--
--


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


Line 71: Line 67:
--
--


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 85: Line 81:
--
--


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
Please note that all contributions to The Deadlock Wiki are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see Deadlock:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template

Page included on this page: