FANDOM


local lib = {}
 
function lib.mergeFrames(frame, parent)
    local args = {}
    if frame then
        for k,v in pairs(frame.args) do
            args[tonumber(k) or mw.text.trim(k)] = mw.text.trim(v)
        end
    end
    if parent then
        for k,v in pairs(parent.args) do
            args[tonumber(k) or mw.text.trim(k)] = mw.text.trim(v)
        end
    end
    return args
end
function lib.arguments(origArgs)
    local args = {}
    for k, v in pairs(origArgs) do
        if type(v) == 'string' then v = mw.text.trim(v) end
        if v ~= '' then args[k] = v end
    end
    return args
end
function lib.table2array(tab, addkey)
    addkey = not not addkey
    local keys, res = {}, {}
    for k,v in pairs(tab) do keys[#keys+1] = k end
    table.sort(keys, function(a,b)
        if type(a) == type(b) then
            return a < b
        else
            return type(a) < type(b)
        end
    end)
    for i,k in ipairs(keys) do
        res[#res+1] = tab[k]
        if addkey and type(res[#res]) == 'table' then res[#res]._k = k end
    end
    return res
end
function lib.getSortedKeys(tab)
    local keys = {}
    for k,_ in pairs(tab) do keys[#keys+1] = k end
    table.sort(keys, function(a,b)
        if type(a) == type(b) then
            return a < b
        else
            return type(a) < type(b)
        end
    end)
    return keys
end
function lib.getSortedKeysStr(tab)
    local keys = {}
    for k,_ in pairs(tab) do keys[#keys+1] = tostring(k) end
    table.sort(keys)
    return keys
end
function lib.groupedArguments(args, numeric)
    if numeric == nil then numeric = true end
    numeric = not not numeric
 
    local base = {}
    local groups = {}
 
    for k, v in pairs(args) do
        v = mw.text.trim(v) or ''
        if v ~= '' then
            if type(k) == 'string' then
                k = mw.text.trim(k) or ''
                if k ~= '' then
                    local split = mw.text.split(k, ':')
                    if #split == 1 then base[k] = v
                    else
                        local group = mw.text.trim(split[1]) or ''
                        local key = mw.text.trim(table.concat(split, ':', 2)) or ''
                        if key ~= '' and group ~= '' then
                            if numeric then
                                group = tonumber(group)
                                if group ~= nil then
                                    if groups[group] == nil then groups[group] = {} end
                                    groups[group][key] = v
                                else
                                    base[k] = v
                                end
                            else
                                if groups[group] == nil then groups[group] = {} end
                                groups[group][key] = v
                            end
                        else
                            base[k] = v
                        end
                    end
                end
            elseif v ~= '' then
                base[k] = v
            end
        end
    end
    return base, groups
end
function lib.ternary(cond, T, F) if cond then return T else return F end end
 
 
 
-- Zwraca zrzut wszystkich globalnych zmiennych
-- {{#invoke:Funkcje|globals}}
function lib.globals(frame)
    local obj = _G
    local prop = mw.text.trim(frame.args[1] or '')
    if prop ~= '' then
        prop = mw.text.split(prop, '%.')
        for i,v in ipairs(prop) do
            obj = obj[v]
        end
    end
    local keys = lib.getSortedKeys(obj)
    local list = ''
    for i,k in ipairs(keys) do
        list = list .. '\n<u>' .. mw.text.nowiki(k) .. '</u> (' .. type(obj[tonumber(k) or k]) .. ') = ' .. tostring(obj[tonumber(k) or k])
    end
    return '<pre>' .. list  .. '</pre>'
end
 
-- Zwraca zrzut wszystkich podanych parametró
-- {{#invoke:Funkcje|dump}}
function lib.dump(frame)
    local getArgs = require('Dev:Arguments').getArgs
    local args = getArgs(frame)
    local keys = lib.getSortedKeys(args)
    local list = ''
    for i,k in ipairs(keys) do
        list = list .. '\n<u>' .. mw.text.nowiki(k) .. '</u> = ' .. mw.text.nowiki(tostring(args[tonumber(k) or k]))
    end
    return '<pre>' .. list  .. '</pre>'
end
 
-- Zwraca i czyści bufor mw.log()
-- {{#invoke:Funkcje|log}}
function lib.log(frame)
    local buffer = mw.getLogBuffer()
    mw.clearLogBuffer()
    if frame.args[1] then return buffer end
    return '<pre>' .. mw.text.nowiki(buffer)  .. '</pre>'
end
 
return lib

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.