The Mol Man (talk | contribs) mNo edit summary |
Spineweilder (talk | contribs) No edit summary Tag: sourceedit |
||
Line 9: | Line 9: | ||
-- |
-- |
||
− | function p.is_empty( |
+ | function p.is_empty(arg) |
− | + | return not arg or not arg:find('%S') |
|
− | return true |
||
⚫ | |||
− | |||
⚫ | |||
end |
end |
||
Line 30: | Line 26: | ||
-- |
-- |
||
− | -- |
+ | -- Returns a list of paramaters if it has any content, or the default |
⚫ | |||
-- |
-- |
||
⚫ | |||
− | |||
+ | local ret = {} |
||
− | function p.has_content( arg ) |
||
− | + | for i, v in ipairs(...) do |
|
+ | if v[1] and v[1]:find('%S') then |
||
− | return false |
||
+ | table.insert(ret,v[1]) |
||
⚫ | |||
+ | -- or false, because nil is removed |
||
+ | table.insert(ret,v[2] or false) |
||
⚫ | |||
end |
end |
||
⚫ | |||
− | |||
− | return true |
||
end |
end |
||
-- |
-- |
||
− | -- Tests if the parameter has |
+ | -- Tests if the parameter has content |
⚫ | |||
− | -- |
||
− | |||
⚫ | |||
− | if not arg then |
||
− | return false |
||
⚫ | |||
− | return true |
||
− | end |
||
− | end |
||
− | |||
− | -- |
||
− | -- Removes leading and trailing whitespace from the parameter |
||
− | -- Which is unfortunately not removed when passed through invoke |
||
-- |
-- |
||
− | function p. |
+ | function p.has_content(arg) |
− | return |
+ | return arg and arg:find('%S') |
end |
end |
||
Line 67: | Line 54: | ||
-- |
-- |
||
− | function p.ucfirst( |
+ | 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 81: | Line 68: | ||
-- |
-- |
||
− | function p.ucflc( |
+ | function p.ucflc(arg) |
if not arg or arg:len() == 0 then |
if not arg or arg:len() == 0 then |
||
return nil |
return nil |
Revision as of 01:49, 3 September 2015
Documentation for this module may be created at Module:Paramtest/doc
--
-- Tests basic properties of parameters
--
local p = {}
--
-- Tests if the parameter is empty, all white space, or undefined
--
function p.is_empty(arg)
return not arg or not arg:find('%S')
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
--
-- Returns a list of paramaters if it has any content, or the default
--
function p.defaults(...)
local ret = {}
for i, v in ipairs(...) do
if v[1] and v[1]:find('%S') then
table.insert(ret,v[1])
else
-- or false, because nil is removed
table.insert(ret,v[2] or false)
end
end
return unpack(ret)
end
--
-- Tests if the parameter has content
-- The same as !is_empty, but this is more readily clear
--
function p.has_content(arg)
return arg and arg:find('%S')
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