Module:Paramtest

From the Tesseract Wiki, the wiki for all things Marvel Cinematic Universe
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:Paramtest/doc. [edit] [history] [purge]
Module:Paramtest is required by Module:Category handler.
Module:Paramtest is required by Module:Clean image.
Module:Paramtest is required by Module:References.
Function list
L 11 — p.is_empty
L 19 — p.default_to
L 30 — p.defaults
L 48 — p.has_content
L 56 — p.ucfirst
L 70 — p.ucflc

This module is a helper module to be used by other modules; it may not designed to be invoked directly. See Tesseract:Lua/Helper modules for a full list and more information. For a full list of modules using this helper click here

FunctionTypeUse
is_empty(arg)StringReturns true if arg is not defined or contains only whitespace
has_content(arg)StringReturns true if arg exists and does not only contain whitespace
default_to(arg1,arg2)String, Any valueIf arg1 exists and does not only contain whitespace, the function returns arg1, otherwise returns arg2
defaults{ {arg1,arg2},...}{String, Any value}...Does the same as default_to() run over every table passed; for technical reasons, all nil are replaced with false

--
-- 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 string.find(arg or '', '%S')
end

--
-- Returns the parameter if it has any content, the default (2nd param)
--

function p.default_to(arg, default)
	if string.find(arg or '', '%S') then
		return arg
	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 string.find(v[1] or '', '%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 string.find(arg or '', '%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