Module:Utilities

From International Robin Hood Bibliography
Revision as of 17:05, 25 May 2022 by Henryfunk (talk | contribs)

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

local module = {}

local upper = mw.ustring.upper
local gsub = mw.ustring.gsub

function module.ucSanitize(cat)
	cat = upper(cat)
	cat = gsub(cat, "'", "'")
    return cat
end

function module.splitString(s, sep)
    local fields = {}
    local pattern = string.format("([^%s]+)", sep)
    string.gsub(s, pattern, function(c) fields[#fields + 1] = c end)
    return fields
end

function module.trim(s)
   return (s:gsub("^%s*(.-)%s*$", "%1"))
end

function module.regexEscape(text)
    return text:gsub("([^%w])", "%%%1")
end

function module.getFrameParam(paramName, frm)
	local paramVal = nil
	if module.isValidParam(paramName)
		then paramVal = frm.args[paramName] 
			if paramVal == '' then paramVal = nil end
	end
	return paramVal
end

function module.getFrameCategoryParam(catName, frm)
	local val = module.getFrameParam(catName, frm)
	if not module.isValidParam(val) then 
		val = module.getFrameParam(catName .. 'Uc', frm)
		if val then val = module.ucSanitize(val) end
	end
	return val
end


function module.isValidParam(param)
	return param ~= nil and param ~= ''
end


function module.setTestState()
	local test = module.getFrameParam('Test', frm)
	local testState = false
	if module.isValidParam(test) and test == 'true' then
		testState = true
	end
	return testState
end

return module