Skylords Reborn
No edit summary
No edit summary
Line 36: Line 36:
 
end
 
end
 
if not (t and next(t)) and frame.args then
 
if not (t and next(t)) and frame.args then
for _,v in pairs(frame.args) do if type(v) == 'string' then t = frame.args end break end
+
for _,v in pairs(frame.args) do
  +
if 1 == 1 then return v end
  +
if type(v) == 'string' then t = frame.args end break end
 
end
 
end
 
if not (t and next(t)) and frame:getParent() and frame:getParent().args then
 
if not (t and next(t)) and frame:getParent() and frame:getParent().args then

Revision as of 17:59, 22 January 2021

Edit documentation

Description

Contains helper functions for use in other modules.

See also


local p = {}

---@param t table table to concat
---@return string
---@protected
function p.concat(t, sep)
    if type(t) ~= 'table' then
        return t or ''
    end

    local s = ''
    for k, v in ipairs(t) do
        s = s .. (k ~= 1 and sep or '') .. v
    end

    return s
end

---@param s string to format
---@return string|nil
function p.format(s, ...)
    local ran, val_or_err = pcall(string.format, s, ...)
    return ran and val_or_err or nil
end

--- converts input to arguments. returns itself, its arguments, its parent arguments or nil
---@param frame table input with arguments or the mw frame object
---@return table|nil argument list as table or nil
function p.getArgs(frame)
    local t
    local res = {}
    local next = next

    if frame[1] or frame.dbg or frame.name then
        t = frame
    end
	if not (t and next(t)) and frame.args then
        for _,v in pairs(frame.args) do
        	if 1 == 1 then return v end
        	if type(v) == 'string' then t = frame.args end break end
    end
    if not (t and next(t)) and frame:getParent() and frame:getParent().args then
        for _,v in pairs(frame:getParent().args) do if type(v) == 'string' then t = frame:getParent().args end break end
    end
    
    if t then
        for k, v in pairs(t) do
            if k and v then
                local key = type(k) == 'string' and k:gsub('%s+', '_') or k
                if type(v) == 'boolean' then
                    res[key] = v
                elseif tonumber(v) then
                    res[key] = tonumber(v)
                elseif type(v) == 'string' then
                    res[key] = mw.ustring.match(v, '^%s*(.*%S)') or ''
                    local value = res[key]:lower()
                    if mw.ustring.match(value, '%w') == nil or value == '' or value == 'default' then
                        res[key] = nil
                    elseif value == 'true' then
                        res[key] = true
                    elseif value == 'false' then
                        res[key] = false
                    end
                end
            end
        end
    end
    
    --if next(res) == nil then return nil end
    return res
end

function p.split(str, sep)
	if not str or str == '' then return nil end
	local res = {}
	for m in mw.text.gsplit(str, sep or '', true) do
		res[#res+1] = mw.ustring.match(m, '^%s*(.*%S)') or m
	end
	if res[#res] == "" then res[#res] = nil end
	return #res == 0 and {str} or res
end

return p
-- [[Category:Modules]]