mNo edit summary |
mNo edit summary |
||
Line 54: | Line 54: | ||
return true |
return true |
||
− | end |
||
− | |||
− | -- |
||
− | -- Tests if the parameter has been defined (regardless of content) |
||
− | -- |
||
− | |||
− | 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.trim( arg ) |
||
− | return string.gsub( arg, '^%s*(%S+)%s*$', '%1') |
||
end |
end |
||
Revision as of 16:35, 22 November 2014
Documentation for this module may be created at Module:Paramtest/doc
--[[
{{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
|fuse3 = 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
}}
--]]
--
-- Tests basic properties of parameters
--
local p = {}
--
-- Tests if the parameter is empty, all white space, or undefined
--
function p.is_empty( arg )
if not arg or not arg:find('%S') then
return true
end
return false
end
--
-- Returns the parameter if it has any content, the default (2nd param)
--
function p.default_to(param, default)
if param and param:find('%S') then
return param
else
return default
end
end
--
-- Tests if the parameter has content
-- The same as !is_empty, but this is more readily clear
--
function p.has_content( arg )
if not arg or not arg:find('%S') then
return false
end
return true
end
--
-- uppercases first letter
--
function p.ucfirst( arg )
if not arg or arg:len() == 0 then
return nil
elseif arg:len() == 1 then
return arg:upper()
else
return arg:sub(1,1):upper() .. arg:sub(2)
end
end
--
-- uppercases first letter, lowercases everything else
--
function p.ucflc( arg )
if not arg or arg:len() == 0 then
return nil
elseif arg:len() == 1 then
return arg:upper()
else
return arg:sub(1,1):upper() .. arg:sub(2):lower()
end
end
return p