Module:Utilities: Difference between revisions
From International Robin Hood Bibliography
(Created page with "UtilCategories.lua: module("UtilCategories") local upper = mw.ustring.upper local gsub = mw.ustring.gsub function ucSanitize(cat) cat = upper(cat) cat = gsub(cat, "'"...") |
mNo edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local module = {} | |||
module | |||
local upper = mw.ustring.upper | local upper = mw.ustring.upper | ||
local gsub = mw.ustring.gsub | local gsub = mw.ustring.gsub | ||
function ucSanitize(cat) | function module.ucSanitize(cat) | ||
cat = upper(cat) | cat = upper(cat) | ||
cat = gsub(cat, "'", "'") | cat = gsub(cat, "'", "'") | ||
return cat | return cat | ||
end | 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(frm) | |||
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 |
Latest revision as of 17:09, 25 May 2022
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(frm) 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