Anonymous

Module:CommonFunctions: Difference between revisions

From Elwiki
m
change fillTemplate fallback
No edit summary
m (change fillTemplate fallback)
 
(25 intermediate revisions by 2 users not shown)
Line 110: Line 110:
     end
     end
     return result;
     return result;
end
-- Implement templating string with slots.
function fillTemplate(tpl, data)
    tpl = tpl or ''
    data = data or {}
    return string.gsub(tpl, "%{(%d+)%}", (function(slotIndex)
        return data[tonumber(slotIndex)] or ('{' .. slotIndex .. '}')
    end))
end
end


Line 211: Line 221:
end
end


function inspect_dump(tbl, frame)
function inspect_dump(frame, tbl)
     return frame:preprocess("<pre><nowiki>" .. inspect(tbl) .. "</nowiki></pre>")
     return frame:preprocess("<pre><nowiki>" .. inspect(tbl) .. "</nowiki></pre>")
end
function sortPassives(s)
    local passives = {}
    for passive in s:gmatch("_passive%d+") do
        table.insert(passives, passive)
    end
    if #passives == 0 then
        return s
    end
    table.sort(passives)
    local base = s:gsub("_passive%d+", "")
    return base .. table.concat(passives)
end
function link(page, text, prefix, suffix, dolink)
    local suffixString = ((suffix and suffix ~= "") and (' ' .. suffix) or '')
    local prefixString = ((prefix and prefix ~= "") and (prefix .. ' ') or '')
    if dolink == false then
        return prefixString .. (text or page) .. suffixString
    end
    return prefixString .. '[[' .. page .. '|' .. (text or page) .. ']]' .. suffixString
end
local function generateCombinations(passives, n, combo, combos)
    if n == 0 then
        table.insert(combos, combo)
    else
        for i = 1, #passives do
            local newCombo = {}
            for j = 1, #combo do
                table.insert(newCombo, combo[j])
            end
            table.insert(newCombo, passives[i])
            generateCombinations(passives, n - 1, newCombo, combos)
        end
    end
end
function formatDamage(number)
    local formattedDamage = number > 0 and (formatnum(math.round(number, 2)) .. '%') or 'N/A'
    return formattedDamage
end
function doVariables(frame, input, prefix)
    local s = ''
    for mode, mode_content in pairs(input) do
        for damage_name, damage_number in pairs(mode_content) do
            damage_number = doRangeText(damage_number)
            local is_range = string.find(damage_number, '~')
            s = s .. '{{#vardefine:' .. (prefix and prefix .. '_' or '') .. (is_range and 'range_' or '') .. damage_name .. (mode ~= 'PvE' and ('_' .. string.lower(mode)) or '') .. '|' .. damage_number .. '}}'
        end
    end
    return frame:preprocess(s)
end
function table.fuse(t1, t2)
    for k, v in pairs(t2) do
        t1[k] = v
    end
    return t1
end
end