Welcome to the Umamusume Wiki! If you want to contribute, please read the guidelines.

Module:Utils

From Umamusume Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Utils/doc

--[[
    !! THIS PAGE IS MANAGED BY GITLAB !!
    ANY EDITS TO PAGE CONTENT WILL BE OVERWRITTEN
    TO MAKE CHANGES, PLEASE SUBMIT A MERGE REQUEST AT https://gitlab.com/umamusume-wiki/lua-modules
]]

local Utils = {}

---Get keys from a table
---@param tbl table
---@return table
function Utils.getKeys(tbl)
    local keys = {}
    for key, _ in pairs(tbl) do
        table.insert(keys, key)
    end
    return keys
end

---Remove duplicated values from a table
---@param values table
---@return table
function Utils.dedupe(values)
    local set = {}
    for _, value in ipairs(values) do
        set[value] = true
    end
    local deduped = {}
    for k, _ in pairs(set) do
        table.insert(deduped, k)
    end
    return deduped
end

---Split text by the newline character
---@param text string
---@return string[] split
function Utils.splitLines(text)
    local lines = {}
    local start = 1
    local from, to = string.find(text, "\n", start)
    while from do
        table.insert(lines, string.sub(text, start, from - 1))
        start = to + 1
        from, to = string.find(text, "\n", start)
    end
    table.insert(lines, string.sub(text, start))
    return lines
end

---Normalize and trim input text/number into a string
---@param text string|number
---@return string
function Utils.normalString(text)
    return mw.text.trim(tostring(text))
end

---Split the first argument of the given frame object into links to comma-separated pages
---@param frame table
---@return string
function Utils.splitLinks(frame)
    local values = frame.args[1]
    local split = mw.text.split(values, ', ', true)
    local newText = {}
    for _, s in ipairs(split) do
        table.insert(newText, '[[' .. s .. ']]')
    end
    return table.concat(newText, ', ')
end

---Group two lists of objects by matching keys. (i.e. Can be used to group by ID)
---The first list given will be the primary list, any values present in the second
---without being in the first will not appear in the result.
---@generic V, W
---@param list1 V[] the first list
---@param key1 string key of objects in the first list to use for matching
---@param list2 W[] the second list
---@param key2 string key of objects in the second list to use for matching
---@param allowNil boolean|nil allow values in the second list to be nil if they do not have a match
---@return [V, W][] pairs pairs of objects
function Utils.pairObjects(list1, key1, list2, key2, allowNil)
    local merged = {}
    for _, value1 in ipairs(list1) do
        local matched2 = nil
        for _, value2 in ipairs(list2) do
            if tostring(value1[key1]) == tostring(value2[key2]) then
                matched2 = value2
                break
            end
        end

        if matched2 ~= nil or allowNil then
            table.insert(merged, { value1, matched2 })
        end
    end
    return merged
end

return Utils