Module:Helper
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Helper/doc
-- ----------------------------------------
-- <pre> Module:Helper
-- Collection of small helper functions
-- ----------------------------------------
local Self = {} -- table of functions
local ustr = mw.ustring -- quick/short access to ustring table
-- local Encoding = require("Module:Encoding")
-- ----------------------------------------
-- Variables
-- ----------------------------------------
local g_IsDesktop = nil
-- ----------------------------------------
-- Helper Functions
-- ----------------------------------------
function Self.GetArgs(frame)
local f
for k, v in pairs(frame.args) do
return frame.args
end
return frame:getParent().args
end
function Self.EscapeMagicCharacters(item)
item = ustr.gsub(item, "&#(%d+);", function(n)
return ustr.char(n)
end)
item = ustr.gsub(item, "%%", "%%%%")
item = ustr.gsub(item, "^%^", "%%^")
item = ustr.gsub(item, "%$$", "%%$")
item = ustr.gsub(item, "%(", "%%(")
item = ustr.gsub(item, "%)", "%%)")
item = ustr.gsub(item, "%.", "%%.")
item = ustr.gsub(item, "%[", "%%[")
item = ustr.gsub(item, "%]", "%%]")
item = ustr.gsub(item, "%*", "%%*")
item = ustr.gsub(item, "%+", "%%+")
item = ustr.gsub(item, "%-", "%%-")
item = ustr.gsub(item, "%?", "%%?")
return item
end
function Self.IfDesktop(a, b)
if g_IsDesktop == nil then
g_IsDesktop = mw.loadData("Module:Helper/IsDesktop").IsDesktop
end
if a ~= nil then
return g_IsDesktop and a or (b or "")
else
return g_IsDesktop
end
end
function Self.IfMobile(a, b)
if g_IsDesktop == nil then
g_IsDesktop = mw.loadData("Module:Helper/IsDesktop").IsDesktop
end
if a ~= nil then
return not g_IsDesktop and a or (b or "")
else
return not g_IsDesktop
end
end
function Self.Exp(frame)
return Self.Explode(frame.args[1], frame, frame.args[2])
end
function Self.Explode(text, frame, skippre)
-- adds a middot between each letter to allow seeing what we are dealing with
frame = frame or mw.getCurrentFrame()
if not skippre then
text = frame:preprocess(text or "")
end
local str = ""
for l in ustr.gmatch(text, ".") do
str = str .. "·" .. l
end
return "Code: " .. str
end
function Self.Dump(o)
-- Dumps the contents of a variable into a <pre>
local str = ""
local serialize
serialize = function(o, s)
local t = type(o)
if o == nil then
str = str .. "nil"
elseif t == "number" then
str = str .. o
elseif t == "string" then
str = str .. ustr.format("%q", o)
elseif t == "boolean" then
str = str .. (o and "true" or "false")
elseif t == "table" then
str = str .. "{\n"
local l = s
s = s .. " "
for k, v in pairs(o) do
str = str .. s .. "["
serialize(k, s)
str = str .. "] = "
serialize(v, s)
str = str .. ",\n"
end
str = str .. l .. "}"
else
error("cannot serialize a " .. type(v))
end
end
serialize(o, "")
return "<pre>" .. str .. "</pre>"
end
function Self.HashArgs(...)
local a = {}
local Hash
Hash = function(key, value)
local t = type(value)
if t == "table" then
for k, v in pairs(value) do
Hash(key .. tostring(k) .. "=", v)
end
else
table.insert(a, tostring(key) .. tostring(value) .. t)
end
end
Hash("", {
...
})
table.sort(a)
return table.concat(a, ",")
end
function Self.Map(array, callbackFn)
local ret = {}
for i, x in ipairs(array) do
ret[i] = callbackFn(x)
end
return ret
end
function Self.Filter(array, callbackFn)
local ret = {}
for _, x in ipairs(array) do
if callbackFn(x) then
table.insert(ret, x)
end
end
return ret
end
function Self.StartsWith(str, prefix)
if (str) then
return str:sub(1, prefix:len()) == prefix
else
end
end
function Self.ExpandTemplate(title, args)
return mw.getCurrentFrame():expandTemplate{
title = title,
args = args
}
end
function Self.ExpandTemplateDebug(title, args)
local ret = "{{" .. title
i = 1
while args[i] ~= nil do
ret = ret .. "|" .. args[i]
i = i + 1
end
for k, v in pairs(args) do
if type(k) ~= "number" or k > i then
ret = ret .. "|" .. k .. "=" .. v
end
end
return ret .. "}}"
end
function Self.LazyLoad(moduleName)
local wannaDecode = false
if (Self.StartsWith(moduleName, "Module:AssetEncodedEnglish")) then
wannaDecode = true
else
wannaDecode = false
end
local loaded = nil
local function doLoad()
if loaded == nil then
loaded = mw.loadData(moduleName)
end
return loaded
end
local meta = {}
meta.__index = function(_table, key)
if (wannaDecode) then
-- return Encoding.decode(doLoad()[key])
else
return doLoad()[key]
end
end
meta.__pairs = function(_table)
return pairs(doLoad())
end
meta.__ipairs = function(_table)
return ipairs(doLoad())
end
local ret = {}
setmetatable(ret, meta)
return ret
end
-- ----------------------------------------
-- Required for Modules to function
-- ----------------------------------------
return Self