Modul:Utils
Penampilan
Pendokumenan untuk modul ini boleh diciptakan di Modul:Utils/doc
--- Modul mit Utility-Funktionen, die in diversen anderen Modulen verwendet werden können
local utils = {}
--- Gibt <code>true</code> zurück, dann und genau dann wenn String <code>String</code> mit <code>Start</code> beginnt.
--
-- @param String der zu testende String
-- @param End Anfang des Strings
function utils.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
--- Gibt <code>true</code> zurück, dann und genau dann wenn String <code>String</code> mit <code>End</code> endet.
--
-- @param String der zu testende String
-- @param End Ende des Strings
function utils.ends(String,End)
return End=='' or string.sub(String,-string.len(End))==End
end
--- Extracts text behind <code><nowiki><nowiki></nowiki></code> tags.
--
-- @param code Code mit nowiki strip markers
function utils.unstripNoWiki(code)
local result = mw.text.unstrip(code)
-- change the eintities for '<', '>', '&' and ' ' to their Unicode symbols
-- (this is necessary because otherwise tags like %lt;math> inside the code will not be expanded)
result = string.gsub(result, '<', '<')
result = string.gsub(result, '>', '>')
result = string.gsub(result, '&', '&')
return result
end
function utils.to_iterator(obj, item_arg, list_arg)
list_arg = list_arg or "liste"
item_arg = item_arg or "element"
if type(obj) == "function" then
-- obj is already an iterator
return obj
elseif type(obj) == "table" then
if obj.args and obj.args[list_arg] then
-- obj is frame with list argument
local list_code = mw.text.trim(obj.args[list_arg], "*# ")
return mw.text.gsplit(list_code, "[\n\r]+[*#]")
elseif obj.args and obj.args[item_arg .. "1"] then
-- obj is frame with each element in different argument
local i = 0
return function()
i = i + 1
return obj.args[item_arg .. tostring(i)]
end
else
-- obj is a table -> return iterator over table
local iter, obj, i = ipairs(obj)
return function()
obj, i = iter(obj, i)
return obj
end
end
else
error("given object is not iterable")
end
end
function utils.to_table(obj, item_arg, list_arg)
list_arg = list_arg or "liste"
item_arg = item_arg or "element"
if type(obj) == "function" then
local result = {}
local i = 0
for item in obj do
i = i + 1
result[i] = item
end
return result
elseif type(obj) == "table" then
if obj.args then
-- obj seems to be a frame
return utils.to_table( utils.to_iterator(obj, item_arg, list_arg) )
else
-- obj is a normal table
return obj
end
else
error("given object is not iterable")
end
end
return utils