Anonymous

Module:CommonFunctions: Difference between revisions

From Elwiki
m
change fillTemplate fallback
No edit summary
m (change fillTemplate fallback)
 
(36 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local inspect = require('Module:Inspect').inspect
-- in Array
-- in Array
function inArray(key, array)
function inArray(key, array)
Line 22: Line 24:
end
end


function inArrayValue(value, array)
function inArrayHasValue(value, array)
     for k, v in pairs(array) do
     for k, v in pairs(array) do
         if v == value then return true end
         if string.find(v, value) then
            return true
        end
     end
     end
end
function indexOf(value, array)
    for i, v in ipairs(array) do
        if v == value then
            return i
        end
    end
    return nil
end
end


Line 81: Line 94:


-- Implement splitting string to a table.
-- Implement splitting string to a table.
function split(s, delimiter)
function split(s, delimiter, skip_empty)
     if not s then return {} end
     if not s then
        return {}
    end
     local i = 1
     local i = 1
     if delimiter == nil then
     if delimiter == nil then
Line 89: Line 104:
     result = {};
     result = {};
     for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
     for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
         table.insert(result, i, match);
         if (skip_empty == true and trim(match) ~= '') or skip_empty == nil then
        i = i + 1
            table.insert(result, i, trim(match));
            i = i + 1
        end
     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 161: Line 188:
     end
     end
     return num / decimals
     return num / decimals
end
function formatnum(amount)
    local formatted = amount
    while true do
        formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
        if (k == 0) then
            break
        end
    end
    return formatted
end
function table.deep_copy(original)
    local copy = {}
    for k, v in pairs(original) do
        if type(v) == "table" then
            copy[k] = table.deep_copy(v)
        else
            copy[k] = v
        end
    end
    return setmetatable(copy, getmetatable(original))
end
function dump(tbl)
    local ret = {}
    for k, v in pairs(tbl) do
        table.insert(ret, k .. ': ' .. inspect(v))
    end
    return frame:preprocess(table.concat(ret, "<br/>"))
end
function inspect_dump(frame, tbl)
    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