Module:Paramtest

From 2b2t Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Paramtest/doc

local p = {}

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

function p.default_to(arg, default)
	if string.find(arg or '', '%S') then
		return arg
	else
		return default
	end
end

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

function p.has_content(arg)
	return string.find(arg or '', '%S')
end

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

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