Skylords Reborn
No edit summary
No edit summary
Line 37: Line 37:
 
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 type(v) == 'string' then t = frame.args end break end
 
end
 
end
if not t and frame:getParent() and frame:getParent().args then
+
if not 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
 
for _,v in pairs(frame:getParent().args) do if type(v) == 'string' then t = frame:getParent().args end break end
 
end
 
end
  +
 
 
if t then
 
if t then
 
res = {}
 
res = {}

Revision as of 19:18, 7 May 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

    if frame[1] or frame.dbg or frame.name then
        t = frame
    end
	if not t and frame.args then
        for _,v in pairs(frame.args) do if type(v) == 'string' then t = frame.args end break end
    end
    if not 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
    	res = {}
        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
    
    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