Module:Utils

From Fractured Wiki
Jump to navigation Jump to search
Documentation

local mw = mw
local string = string
local table = table

local mw_makeTitle = mw.title.makeTitle

-- Define small functions inline
local p = {
    ["lower"] = function(str)
        return string.lower(str or '')
    end,

    -- Check if page exists
    --
    -- page: name of wiki page
    -- namespace: leave nil for Main namespace
    --
    ["pageExists"] = function(page, namespace)
        local title = mw_makeTitle(namespace or "", page)
        return title and title:getContent()
    end,

    ["strlen"] = function(str)
        return string.len(tostring(str))
    end,

    ["sprintf"] = string.format,

    ["startsWith"] = function(str, substr)
        return string.sub(str, 1, string.len(substr)) == substr
    end,

    ["tableSort"] = function(tbl)
        table.sort(tbl)
        return tbl
    end,

    ["upper"] = function(str)
        return string.upper(str or '')
    end,
}

-- Add commas to a number
function p.addCommas(number)
    local n = tonumber(number)
    if n == nil then return number end

    number = string.format("%f", number)
    -- Remove trailing zeros
    number = string.match(number, "^(.-)%.?0*$")

    local k

    while true do
        number, k = string.gsub(number, "^(-?%d+)(%d%d%d)", "%1,%2")
        if k == 0 then break end
    end

    return number
end

-- Format number according to users locale
function p.formatNum(num, frame)
    if num == nil then return end
    if frame == nil then return num end
    
    -- Format according to how the formatnum parser function wants it.
    -- https://www.mediawiki.org/wiki/Help:Magic_words#Formatting
    num = string.gsub(tostring(num), ",", ".")

    -- Make sure we have what appears to be a valid number
    if string.match(num, "^%d*(?:%.%d+)?$") then
        num = frame:callParserFunction('formatnum', num)
    end

    return num
end

-- Flatten an array
function p.flatten(arr)
    local res = {}

    local function f(_arr)
        for _, v in ipairs(_arr) do
            if type(v) == 'table' then
                f(v)
            else
                table.insert(res, v)
            end
        end
    end

    f(arr)
    return res
end

-- Get index for a table value
function p.tableIndex(t, val)
    local i = 1
    for _, v in ipairs(t) do
        if v == val then return i end
        i = i + 1
    end

    return nil
end

-- Sum all numbers in array
function p.sumArray(arr)
    if type(arr) ~= 'table' then return end
    local s = 0

    for _, v in ipairs(arr) do
        if type(v) == 'string' then
            v = tonumber(v)
        end

        if type(v) == 'number' then
            s = s + v
        end
    end

    return s
end

-- Map function applies func() to all elements in tbl
-- Note that tbl has to be a list/array, not a dictionary
-- A new table is returned; the original table is unmodified
function p.map(func, in_tbl)
    local out_tbl = {}
    local in_tbl_len = #in_tbl

    for i = 1, in_tbl_len do
        out_tbl[i] = func(in_tbl[i])
    end

    return out_tbl
end

------------------------------
return p